From bae9b6fb3b53a9d73f4c36e7b676beb6dac03476 Mon Sep 17 00:00:00 2001 From: Austin Drenski Date: Fri, 17 Jan 2025 09:54:50 -0500 Subject: [PATCH] fix: Skip flagd banner when non-console logger in use (#1516) ## This PR - Moves the (very nice) flagd console banner behind a flag check to avoid sending the (again, very nice) console banner to JSON log files, sinks, k8s trailers, etc. ### How to test ```console $ go run main.go start -f file:../config/samples/example_flags.flagd.json ______ __ ________ _______ ______ /_____/\ /_/\ /_______/\ /______/\ /_____/\ \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ 2025-01-15T19:41:51.347-0500 info cmd/start.go:124 flagd version: dev (HEAD), built at: unknown {"component": "start"} 2025-01-15T19:41:51.347-0500 info flag-sync/sync_service.go:87 starting flag sync service on port 8015 {"component": "FlagSyncService"} 2025-01-15T19:41:51.347-0500 info file/filepath_sync.go:62 Starting filepath sync notifier {"component": "sync", "sync": "fileinfo"} 2025-01-15T19:41:51.347-0500 info ofrep/ofrep_service.go:58 ofrep service listening at 8016 {"component": "OFREPService"} 2025-01-15T19:41:51.347-0500 info flag-evaluation/connect_service.go:249 metrics and probes listening at 8014 {"component": "service"} 2025-01-15T19:41:51.347-0500 info file/filepath_sync.go:101 watching filepath: ../config/samples/example_flags.flagd.json {"component": "sync", "sync": "fileinfo"} 2025-01-15T19:41:51.347-0500 info flag-evaluation/connect_service.go:229 Flag IResolver listening at [::]:8013 {"component": "service"} ``` ```console $ go run main.go start -f file:../config/samples/example_flags.flagd.json --log-format console ______ __ ________ _______ ______ /_____/\ /_/\ /_______/\ /______/\ /_____/\ \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ 2025-01-15T19:41:39.375-0500 info cmd/start.go:124 flagd version: dev (HEAD), built at: unknown {"component": "start"} 2025-01-15T19:41:39.376-0500 info flag-sync/sync_service.go:87 starting flag sync service on port 8015 {"component": "FlagSyncService"} 2025-01-15T19:41:39.378-0500 info file/filepath_sync.go:62 Starting filepath sync notifier {"component": "sync", "sync": "fileinfo"} 2025-01-15T19:41:39.380-0500 info ofrep/ofrep_service.go:58 ofrep service listening at 8016 {"component": "OFREPService"} 2025-01-15T19:41:39.380-0500 info flag-evaluation/connect_service.go:249 metrics and probes listening at 8014 {"component": "service"} 2025-01-15T19:41:39.380-0500 info flag-evaluation/connect_service.go:229 Flag IResolver listening at [::]:8013 {"component": "service"} 2025-01-15T19:41:39.381-0500 info file/filepath_sync.go:101 watching filepath: ../config/samples/example_flags.flagd.json {"component": "sync", "sync": "fileinfo"} ``` ```console $ go run main.go start -f file:../config/samples/example_flags.flagd.json --log-format json {"level":"info","ts":"2025-01-15T19:41:19.857-0500","caller":"cmd/start.go:124","msg":"flagd version: dev (HEAD), built at: unknown","component":"start"} {"level":"info","ts":"2025-01-15T19:41:19.857-0500","caller":"flag-sync/sync_service.go:87","msg":"starting flag sync service on port 8015","component":"FlagSyncService"} {"level":"info","ts":"2025-01-15T19:41:19.861-0500","caller":"file/filepath_sync.go:62","msg":"Starting filepath sync notifier","component":"sync","sync":"fileinfo"} {"level":"info","ts":"2025-01-15T19:41:19.863-0500","caller":"ofrep/ofrep_service.go:58","msg":"ofrep service listening at 8016","component":"OFREPService"} {"level":"info","ts":"2025-01-15T19:41:19.863-0500","caller":"flag-evaluation/connect_service.go:249","msg":"metrics and probes listening at 8014","component":"service"} {"level":"info","ts":"2025-01-15T19:41:19.864-0500","caller":"file/filepath_sync.go:101","msg":"watching filepath: ../config/samples/example_flags.flagd.json","component":"sync","sync":"fileinfo"} {"level":"info","ts":"2025-01-15T19:41:19.864-0500","caller":"flag-evaluation/connect_service.go:229","msg":"Flag IResolver listening at [::]:8013","component":"service"} ``` Signed-off-by: Austin Drenski Co-authored-by: Michael Beemer --- flagd-proxy/cmd/root.go | 17 +++++++++++++++++ flagd-proxy/main.go | 27 ++++----------------------- flagd/cmd/root.go | 16 ++++++++++++++++ flagd/main.go | 26 ++++---------------------- 4 files changed, 41 insertions(+), 45 deletions(-) diff --git a/flagd-proxy/cmd/root.go b/flagd-proxy/cmd/root.go index b39b1b663..f1824155e 100644 --- a/flagd-proxy/cmd/root.go +++ b/flagd-proxy/cmd/root.go @@ -5,6 +5,8 @@ import ( "log" "os" + "github.com/dimiro1/banner" + "github.com/mattn/go-colorable" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -22,6 +24,21 @@ var rootCmd = &cobra.Command{ Short: "flagd-proxy allows flagd to subscribe to CRD changes without the required permissions.", Long: ``, DisableAutoGenTag: true, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if viper.GetString(logFormatFlagName) == "console" { + banner.InitString(colorable.NewColorableStdout(), true, true, ` + {{ .AnsiColor.BrightRed }} ______ __ ________ _______ ______ + {{ .AnsiColor.BrightRed }} /_____/\ /_/\ /_______/\ /______/\ /_____/\ + {{ .AnsiColor.BrightRed }} \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ + {{ .AnsiColor.BrightRed }} \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ + {{ .AnsiColor.BrightRed }} \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ + {{ .AnsiColor.BrightRed }} \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | + {{ .AnsiColor.BrightRed }} \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ + {{ .AnsiColor.BrightRed }} Kubernetes Proxy +{{ .AnsiColor.Default }} +`) + } + }, } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/flagd-proxy/main.go b/flagd-proxy/main.go index 7f7e8c733..0605273ce 100644 --- a/flagd-proxy/main.go +++ b/flagd-proxy/main.go @@ -1,32 +1,13 @@ package main -import ( - "bytes" - - "github.com/dimiro1/banner" - "github.com/mattn/go-colorable" - "github.com/open-feature/flagd/flagd-proxy/cmd" -) +import "github.com/open-feature/flagd/flagd-proxy/cmd" var ( - version = "dev" - commit = "HEAD" - date = "unknown" - bannerText = ` - {{ .AnsiColor.BrightRed }} ______ __ ________ _______ ______ - {{ .AnsiColor.BrightRed }} /_____/\ /_/\ /_______/\ /______/\ /_____/\ - {{ .AnsiColor.BrightRed }} \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ - {{ .AnsiColor.BrightRed }} \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ - {{ .AnsiColor.BrightRed }} \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ - {{ .AnsiColor.BrightRed }} \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | - {{ .AnsiColor.BrightRed }} \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ - {{ .AnsiColor.BrightRed }} Kubernetes Proxy -{{ .AnsiColor.Default }} -` + version = "dev" + commit = "HEAD" + date = "unknown" ) func main() { - banner.Init(colorable.NewColorableStdout(), true, true, - bytes.NewBufferString(bannerText)) cmd.Execute(version, commit, date) } diff --git a/flagd/cmd/root.go b/flagd/cmd/root.go index a9128601f..54464824b 100644 --- a/flagd/cmd/root.go +++ b/flagd/cmd/root.go @@ -5,6 +5,8 @@ import ( "log" "os" + "github.com/dimiro1/banner" + "github.com/mattn/go-colorable" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -23,6 +25,20 @@ var rootCmd = &cobra.Command{ "It is designed to conform to Open Feature schema for flag definitions.", Long: ``, DisableAutoGenTag: true, + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if viper.GetString(logFormatFlagName) == "console" { + banner.InitString(colorable.NewColorableStdout(), true, true, ` + {{ .AnsiColor.BrightRed }} ______ __ ________ _______ ______ + {{ .AnsiColor.BrightRed }} /_____/\ /_/\ /_______/\ /______/\ /_____/\ + {{ .AnsiColor.BrightRed }} \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ + {{ .AnsiColor.BrightRed }} \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ + {{ .AnsiColor.BrightRed }} \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ + {{ .AnsiColor.BrightRed }} \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | + {{ .AnsiColor.BrightRed }} \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ +{{ .AnsiColor.Default }} +`) + } + }, // Uncomment the following line if your bare application // has an action associated with it: // Run: func(cmd *cobra.Command, args []string) { }, diff --git a/flagd/main.go b/flagd/main.go index 75d70b322..18e1a786d 100644 --- a/flagd/main.go +++ b/flagd/main.go @@ -1,31 +1,13 @@ package main -import ( - "bytes" - - "github.com/dimiro1/banner" - "github.com/mattn/go-colorable" - "github.com/open-feature/flagd/flagd/cmd" -) +import "github.com/open-feature/flagd/flagd/cmd" var ( - version = "dev" - commit = "HEAD" - date = "unknown" - bannerText = ` - {{ .AnsiColor.BrightRed }} ______ __ ________ _______ ______ - {{ .AnsiColor.BrightRed }} /_____/\ /_/\ /_______/\ /______/\ /_____/\ - {{ .AnsiColor.BrightRed }} \::::_\/_\:\ \ \::: _ \ \\::::__\/__\:::_ \ \ - {{ .AnsiColor.BrightRed }} \:\/___/\\:\ \ \::(_) \ \\:\ /____/\\:\ \ \ \ - {{ .AnsiColor.BrightRed }} \:::._\/ \:\ \____\:: __ \ \\:\\_ _\/ \:\ \ \ \ - {{ .AnsiColor.BrightRed }} \:\ \ \:\/___/\\:.\ \ \ \\:\_\ \ \ \:\/.:| | - {{ .AnsiColor.BrightRed }} \_\/ \_____\/ \__\/\__\/ \_____\/ \____/_/ -{{ .AnsiColor.Default }} -` + version = "dev" + commit = "HEAD" + date = "unknown" ) func main() { - banner.Init(colorable.NewColorableStdout(), true, true, - bytes.NewBufferString(bannerText)) cmd.Execute(version, commit, date) }