From 0e00e60fe928b8fbc7c276196aca91d3edbe0de3 Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 09:17:27 -0700 Subject: [PATCH 1/7] Add new flag and implementation --- internal/cmd/cmd.go | 2 ++ internal/commands/util.go | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index c526f8d5..6d588143 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -53,6 +53,7 @@ func Run() { zl.RunE(), SyncFlagsCmdFunc, commands.InjectRequestID, + commands.SetForceColorIfNeeded, ), SilenceErrors: true, SilenceUsage: true, @@ -74,6 +75,7 @@ func Run() { rootCmd.PersistentFlags().Bool("skip-version-check", false, "if true, no version check is performed against the server") rootCmd.PersistentFlags().Bool("no-verify-ca", false, "do not attempt to verify the server's certificate chain and host name") rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging") + rootCmd.PersistentFlags().Bool("force-color", false, "force color code output even in non-tty environments") rootCmd.PersistentFlags().String("request-id", "", "optional id to send along with SpiceDB requests for tracing") rootCmd.PersistentFlags().Int("max-message-size", 0, "maximum size *in bytes* (defaults to 4_194_304 bytes ~= 4MB) of a gRPC message that can be sent or received by zed") _ = rootCmd.PersistentFlags().MarkHidden("debug") // This cannot return its error. diff --git a/internal/commands/util.go b/internal/commands/util.go index dadb046d..4cc4576f 100644 --- a/internal/commands/util.go +++ b/internal/commands/util.go @@ -7,8 +7,10 @@ import ( "github.com/TylerBrock/colorjson" "github.com/authzed/authzed-go/pkg/requestmeta" + "github.com/charmbracelet/lipgloss" "github.com/jzelinskie/cobrautil/v2" "github.com/jzelinskie/stringz" + "github.com/muesli/termenv" "github.com/spf13/cobra" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" @@ -95,3 +97,14 @@ func InjectRequestID(cmd *cobra.Command, _ []string) error { return nil } + +// Override lipgloss's autodetection of whether it's in a terminal environment +// and display things in color anyway. This can be nice in CI environments that +// support it. +func SetForceColorIfNeeded(cmd *cobra.Command, _ []string) error { + setForceColor := cobrautil.MustGetBool(cmd, "force-color") + if setForceColor { + lipgloss.SetColorProfile(termenv.ANSI256) + } + return nil +} From 9a21e1bb9c4364e66c887d3f654b445473a03c11 Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 10:31:28 -0700 Subject: [PATCH 2/7] Revert rootcmd change --- internal/cmd/cmd.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 6d588143..c526f8d5 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -53,7 +53,6 @@ func Run() { zl.RunE(), SyncFlagsCmdFunc, commands.InjectRequestID, - commands.SetForceColorIfNeeded, ), SilenceErrors: true, SilenceUsage: true, @@ -75,7 +74,6 @@ func Run() { rootCmd.PersistentFlags().Bool("skip-version-check", false, "if true, no version check is performed against the server") rootCmd.PersistentFlags().Bool("no-verify-ca", false, "do not attempt to verify the server's certificate chain and host name") rootCmd.PersistentFlags().Bool("debug", false, "enable debug logging") - rootCmd.PersistentFlags().Bool("force-color", false, "force color code output even in non-tty environments") rootCmd.PersistentFlags().String("request-id", "", "optional id to send along with SpiceDB requests for tracing") rootCmd.PersistentFlags().Int("max-message-size", 0, "maximum size *in bytes* (defaults to 4_194_304 bytes ~= 4MB) of a gRPC message that can be sent or received by zed") _ = rootCmd.PersistentFlags().MarkHidden("debug") // This cannot return its error. From 36b8404e29a7bd73d26ad65001772955e214f75d Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 10:31:57 -0700 Subject: [PATCH 3/7] Get rid of util that wasn't helping --- internal/commands/util.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/internal/commands/util.go b/internal/commands/util.go index 4cc4576f..dadb046d 100644 --- a/internal/commands/util.go +++ b/internal/commands/util.go @@ -7,10 +7,8 @@ import ( "github.com/TylerBrock/colorjson" "github.com/authzed/authzed-go/pkg/requestmeta" - "github.com/charmbracelet/lipgloss" "github.com/jzelinskie/cobrautil/v2" "github.com/jzelinskie/stringz" - "github.com/muesli/termenv" "github.com/spf13/cobra" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" @@ -97,14 +95,3 @@ func InjectRequestID(cmd *cobra.Command, _ []string) error { return nil } - -// Override lipgloss's autodetection of whether it's in a terminal environment -// and display things in color anyway. This can be nice in CI environments that -// support it. -func SetForceColorIfNeeded(cmd *cobra.Command, _ []string) error { - setForceColor := cobrautil.MustGetBool(cmd, "force-color") - if setForceColor { - lipgloss.SetColorProfile(termenv.ANSI256) - } - return nil -} From 548c864760c9c9c7e02fd4997ebd38b5a16ac577 Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 10:34:12 -0700 Subject: [PATCH 4/7] Thunk usages of lipgloss to ensure renderer changes are picked up --- internal/cmd/validate.go | 75 ++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/internal/cmd/validate.go b/internal/cmd/validate.go index 23da29bc..faa3060b 100644 --- a/internal/cmd/validate.go +++ b/internal/cmd/validate.go @@ -16,6 +16,8 @@ import ( "github.com/authzed/spicedb/pkg/spiceerrors" "github.com/authzed/spicedb/pkg/validationfile" "github.com/charmbracelet/lipgloss" + "github.com/jzelinskie/cobrautil/v2" + "github.com/muesli/termenv" "github.com/authzed/zed/internal/commands" "github.com/authzed/zed/internal/console" @@ -24,21 +26,28 @@ import ( ) var ( - success = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("10")).Render("Success!") - complete = lipgloss.NewStyle().Bold(true).Render("complete") - errorPrefix = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9")).Render("error: ") - warningPrefix = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("3")).Render("warning: ") - errorMessageStyle = lipgloss.NewStyle().Bold(true).Width(80) - linePrefixStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("12")) - highlightedSourceStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) - highlightedLineStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("9")) - codeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("8")) - highlightedCodeStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("15")) - traceStyle = lipgloss.NewStyle().Bold(true) + // NOTE: these need to be set *after* the renderer has been set, otherwise + // the forceColor setting can't work, hence the thunking. + success = func() string { + return lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("10")).Render("Success!") + } + complete = func() string { return lipgloss.NewStyle().Bold(true).Render("complete") } + errorPrefix = func() string { return lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("9")).Render("error: ") } + warningPrefix = func() string { + return lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("3")).Render("warning: ") + } + errorMessageStyle = func() lipgloss.Style { return lipgloss.NewStyle().Bold(true).Width(80) } + linePrefixStyle = func() lipgloss.Style { return lipgloss.NewStyle().Foreground(lipgloss.Color("12")) } + highlightedSourceStyle = func() lipgloss.Style { return lipgloss.NewStyle().Foreground(lipgloss.Color("9")) } + highlightedLineStyle = func() lipgloss.Style { return lipgloss.NewStyle().Foreground(lipgloss.Color("9")) } + codeStyle = func() lipgloss.Style { return lipgloss.NewStyle().Foreground(lipgloss.Color("8")) } + highlightedCodeStyle = func() lipgloss.Style { return lipgloss.NewStyle().Foreground(lipgloss.Color("15")) } + traceStyle = func() lipgloss.Style { return lipgloss.NewStyle().Bold(true) } ) -func registerValidateCmd(rootCmd *cobra.Command) { - rootCmd.AddCommand(validateCmd) +func registerValidateCmd(cmd *cobra.Command) { + validateCmd.Flags().Bool("force-color", false, "force color code output even in non-tty environments") + cmd.AddCommand(validateCmd) } var validateCmd = &cobra.Command{ @@ -64,9 +73,23 @@ var validateCmd = &cobra.Command{ zed validate https://localhost:8443/download`, Args: cobra.ExactArgs(1), ValidArgsFunction: commands.FileExtensionCompletions("zed", "yaml", "zaml"), + PreRunE: validatePreRunE, RunE: validateCmdFunc, } +func validatePreRunE(cmd *cobra.Command, _ []string) error { + // Override lipgloss's autodetection of whether it's in a terminal environment + // and display things in color anyway. This can be nice in CI environments that + // support it. + setForceColor := cobrautil.MustGetBool(cmd, "force-color") + if setForceColor { + lipgloss.SetColorProfile(termenv.ANSI256) + fmt.Println(lipgloss.DefaultRenderer().ColorProfile() == termenv.ANSI256) + } + + return nil +} + func validateCmdFunc(cmd *cobra.Command, args []string) error { // Parse the URL of the validation document to import. u, err := url.Parse(args[0]) @@ -139,14 +162,14 @@ func validateCmdFunc(cmd *cobra.Command, args []string) error { if len(warnings) > 0 { for _, warning := range warnings { - console.Printf("%s%s\n", warningPrefix, warning.Message) + console.Printf("%s%s\n", warningPrefix(), warning.Message) outputForLine(validateContents, uint64(warning.Line), warning.SourceCode, uint64(warning.Column)) // warning.LineNumber is 1-indexed console.Printf("\n") } - console.Print(complete) + console.Print(complete()) } else { - console.Print(success) + console.Print(success()) } console.Printf(" - %d relationships loaded, %d assertions run, %d expected relations validated\n", @@ -158,7 +181,7 @@ func validateCmdFunc(cmd *cobra.Command, args []string) error { } func ouputErrorWithSource(validateContents []byte, errWithSource spiceerrors.ErrorWithSource) { - console.Printf("%s%s\n", errorPrefix, errorMessageStyle.Render(errWithSource.Error())) + console.Printf("%s%s\n", errorPrefix(), errorMessageStyle().Render(errWithSource.Error())) outputForLine(validateContents, errWithSource.LineNumber, errWithSource.SourceCodeString, 0) // errWithSource.LineNumber is 1-indexed os.Exit(1) } @@ -193,7 +216,7 @@ func outputDeveloperErrorsWithLineOffset(validateContents []byte, devErrors []*d } func outputDeveloperError(devError *devinterface.DeveloperError, lines []string, lineOffset int) { - console.Printf("%s %s\n", errorPrefix, errorMessageStyle.Render(devError.Message)) + console.Printf("%s %s\n", errorPrefix(), errorMessageStyle().Render(devError.Message)) errorLineNumber := int(devError.Line) - 1 + lineOffset // devError.Line is 1-indexed for i := errorLineNumber - 3; i < errorLineNumber+3; i++ { if i == errorLineNumber { @@ -204,7 +227,7 @@ func outputDeveloperError(devError *devinterface.DeveloperError, lines []string, } if devError.CheckResolvedDebugInformation != nil && devError.CheckResolvedDebugInformation.Check != nil { - console.Printf("\n %s\n", traceStyle.Render("Explanation:")) + console.Printf("\n %s\n", traceStyle().Render("Explanation:")) tp := printers.NewTreePrinter() printers.DisplayCheckTrace(devError.CheckResolvedDebugInformation.Check, tp, true) tp.PrintIndented() @@ -261,23 +284,23 @@ func renderLine(lines []string, index int, highlight string, highlightLineIndex lineNumberSpacer := strings.Repeat(" ", lineNumberLength-len(lineNumberStr)) if highlightColumnIndex < 0 { - console.Printf(" %s%s %s %s\n", lineNumberSpacer, lineNumberStyle.Render(lineNumberStr), lineDelimiter, lineContentsStyle.Render(lineContents)) + console.Printf(" %s%s %s %s\n", lineNumberSpacer, lineNumberStyle().Render(lineNumberStr), lineDelimiter, lineContentsStyle().Render(lineContents)) } else { console.Printf(" %s%s %s %s%s%s\n", lineNumberSpacer, - lineNumberStyle.Render(lineNumberStr), + lineNumberStyle().Render(lineNumberStr), lineDelimiter, - lineContentsStyle.Render(lineContents[0:highlightColumnIndex]), - highlightedSourceStyle.Render(highlight), - lineContentsStyle.Render(lineContents[highlightColumnIndex+len(highlight):]), + lineContentsStyle().Render(lineContents[0:highlightColumnIndex]), + highlightedSourceStyle().Render(highlight), + lineContentsStyle().Render(lineContents[highlightColumnIndex+len(highlight):]), ) console.Printf(" %s %s %s%s%s\n", noNumberSpaces, lineDelimiter, strings.Repeat(" ", highlightColumnIndex), - highlightedSourceStyle.Render("^"), - highlightedSourceStyle.Render(strings.Repeat("~", highlightLength)), + highlightedSourceStyle().Render("^"), + highlightedSourceStyle().Render(strings.Repeat("~", highlightLength)), ) } } From 632eb5e2ac07aa535239acc4d2b1a01f0a247a6a Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 10:54:43 -0700 Subject: [PATCH 5/7] Remove debug --- internal/cmd/validate.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/cmd/validate.go b/internal/cmd/validate.go index faa3060b..2b329236 100644 --- a/internal/cmd/validate.go +++ b/internal/cmd/validate.go @@ -84,7 +84,6 @@ func validatePreRunE(cmd *cobra.Command, _ []string) error { setForceColor := cobrautil.MustGetBool(cmd, "force-color") if setForceColor { lipgloss.SetColorProfile(termenv.ANSI256) - fmt.Println(lipgloss.DefaultRenderer().ColorProfile() == termenv.ANSI256) } return nil From 15b07e61c88162989aa93d5de3d1b81f2fd0e53d Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 10:56:47 -0700 Subject: [PATCH 6/7] Go mod tidy --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index ff274eb9..6707bd1a 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/jzelinskie/stringz v0.0.3 github.com/mattn/go-isatty v0.0.20 github.com/mitchellh/go-homedir v1.1.0 + github.com/muesli/termenv v0.15.2 github.com/olekukonko/tablewriter v0.0.5 github.com/rodaine/table v1.3.0 github.com/rs/zerolog v1.33.0 @@ -179,7 +180,6 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/mostynb/go-grpc-compression v1.2.3 // indirect github.com/mtibben/percent v0.2.1 // indirect - github.com/muesli/termenv v0.15.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/ngrok/sqlmw v0.0.0-20220520173518-97c9c04efc79 // indirect github.com/onsi/ginkgo/v2 v2.20.2 // indirect From 2b8092c43efc09fe719216a19616de37c7b5eae8 Mon Sep 17 00:00:00 2001 From: Tanner Stirrat Date: Thu, 7 Nov 2024 13:19:03 -0700 Subject: [PATCH 7/7] Add env vars for db request --- .github/workflows/lint.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index cdaaca5f..488fc1d7 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -58,6 +58,9 @@ jobs: format: "table" exit-code: "1" severity: "CRITICAL,HIGH,MEDIUM" + env: + TRIVY_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-db" + TRIVY_JAVA_DB_REPOSITORY: "public.ecr.aws/aquasecurity/trivy-java-db" trivy-image: name: "Analyze Release Image with Trivy"