From 43b9ced061712842f5c2acdb6361195cfe8fa165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Fri, 25 Sep 2020 22:22:43 +0200 Subject: [PATCH] Print config to stderr (#277) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes a regression when the config file used is printed to stdout even when the list-changed command is run. It is now only printed when --print-config=true. Also, config printing is now done to stderr so the flag can also be used in combination with the list-changed command. Signed-off-by: Reinhard Nägele --- ct/cmd/lintAndInstall.go | 6 +++++- ct/cmd/listChanged.go | 6 +++++- ct/cmd/root.go | 6 +++--- doc/ct_install.md | 2 +- doc/ct_lint-and-install.md | 2 +- doc/ct_lint.md | 2 +- doc/ct_list-changed.md | 4 +++- pkg/config/config.go | 17 ++++++++++------- pkg/util/util.go | 8 +++++++- 9 files changed, 36 insertions(+), 17 deletions(-) diff --git a/ct/cmd/lintAndInstall.go b/ct/cmd/lintAndInstall.go index 413362f..10c48ca 100644 --- a/ct/cmd/lintAndInstall.go +++ b/ct/cmd/lintAndInstall.go @@ -42,7 +42,11 @@ func newLintAndInstallCmd() *cobra.Command { func lintAndInstall(cmd *cobra.Command, args []string) error { fmt.Println("Linting and installing charts...") - configuration, err := config.LoadConfiguration(cfgFile, cmd, true) + printConfig, err := cmd.Flags().GetBool("print-config") + if err != nil { + return err + } + configuration, err := config.LoadConfiguration(cfgFile, cmd, printConfig) if err != nil { return fmt.Errorf("Error loading configuration: %s", err) } diff --git a/ct/cmd/listChanged.go b/ct/cmd/listChanged.go index c1d19f8..134203a 100644 --- a/ct/cmd/listChanged.go +++ b/ct/cmd/listChanged.go @@ -41,7 +41,11 @@ func newListChangedCmd() *cobra.Command { } func listChanged(cmd *cobra.Command, args []string) error { - configuration, err := config.LoadConfiguration(cfgFile, cmd, false) + printConfig, err := cmd.Flags().GetBool("print-config") + if err != nil { + return err + } + configuration, err := config.LoadConfiguration(cfgFile, cmd, printConfig) if err != nil { return fmt.Errorf("Error loading configuration: %s", err) } diff --git a/ct/cmd/root.go b/ct/cmd/root.go index bd9a3b5..76c96f9 100644 --- a/ct/cmd/root.go +++ b/ct/cmd/root.go @@ -71,6 +71,9 @@ func addCommonFlags(flags *pflag.FlagSet) { flags.StringSlice("excluded-charts", []string{}, heredoc.Doc(` Charts that should be skipped. May be specified multiple times or separate values with commas`)) + flags.Bool("print-config", false, heredoc.Doc(` + Prints the configuration to stderr (caution: setting this may + expose sensitive data when helm-repo-extra-args contains passwords)`)) } func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { @@ -94,7 +97,4 @@ func addCommonLintAndInstallFlags(flags *pflag.FlagSet) { flags.Bool("debug", false, heredoc.Doc(` Print CLI calls of external tools to stdout (caution: setting this may expose sensitive data when helm-repo-extra-args contains passwords)`)) - flags.Bool("print-config", false, heredoc.Doc(` - Prints the configuration to stdout (caution: setting this may - expose sensitive data when helm-repo-extra-args contains passwords)`)) } diff --git a/doc/ct_install.md b/doc/ct_install.md index 7e71521..262ac3d 100644 --- a/doc/ct_install.md +++ b/doc/ct_install.md @@ -56,7 +56,7 @@ ct install [flags] -h, --help help for install --namespace string Namespace to install the release(s) into. If not specified, each release will be installed in its own randomly generated namespace - --print-config Prints the configuration to stdout (caution: setting this may + --print-config Prints the configuration to stderr (caution: setting this may expose sensitive data when helm-repo-extra-args contains passwords) --release-label string The label to be used as a selector when inspecting resources created by charts. This is only used if namespace is specified (default "app.kubernetes.io/instance") diff --git a/doc/ct_lint-and-install.md b/doc/ct_lint-and-install.md index b3dc90c..4424217 100644 --- a/doc/ct_lint-and-install.md +++ b/doc/ct_lint-and-install.md @@ -47,7 +47,7 @@ ct lint-and-install [flags] that order --namespace string Namespace to install the release(s) into. If not specified, each release will be installed in its own randomly generated namespace - --print-config Prints the configuration to stdout (caution: setting this may + --print-config Prints the configuration to stderr (caution: setting this may expose sensitive data when helm-repo-extra-args contains passwords) --release-label string The label to be used as a selector when inspecting resources created by charts. This is only used if namespace is specified (default "app.kubernetes.io/instance") diff --git a/doc/ct_lint.md b/doc/ct_lint.md index 82b059c..0aa9685 100644 --- a/doc/ct_lint.md +++ b/doc/ct_lint.md @@ -53,7 +53,7 @@ ct lint [flags] --lint-conf string The config file for YAML linting. If not specified, 'lintconf.yaml' is searched in the current directory, '$HOME/.ct', and '/etc/ct', in that order - --print-config Prints the configuration to stdout (caution: setting this may + --print-config Prints the configuration to stderr (caution: setting this may expose sensitive data when helm-repo-extra-args contains passwords) --remote string The name of the Git remote used to identify changed charts (default "origin") --since string The Git reference used to identify changed charts (default "HEAD") diff --git a/doc/ct_list-changed.md b/doc/ct_list-changed.md index 480677f..be3f768 100644 --- a/doc/ct_list-changed.md +++ b/doc/ct_list-changed.md @@ -20,6 +20,8 @@ ct list-changed [flags] --excluded-charts strings Charts that should be skipped. May be specified multiple times or separate values with commas -h, --help help for list-changed + --print-config Prints the configuration to stderr (caution: setting this may + expose sensitive data when helm-repo-extra-args contains passwords) --remote string The name of the Git remote used to identify changed charts (default "origin") --since string The Git reference used to identify changed charts (default "HEAD") --target-branch string The name of the target branch used to identify changed charts (default "master") @@ -29,4 +31,4 @@ ct list-changed [flags] * [ct](ct.md) - The Helm chart testing tool -###### Auto generated by spf13/cobra on 21-Apr-2020 +###### Auto generated by spf13/cobra on 25-Sep-2020 diff --git a/pkg/config/config.go b/pkg/config/config.go index 59045f6..ec62abe 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -16,6 +16,7 @@ package config import ( "fmt" + "os" "path" "reflect" "strings" @@ -96,7 +97,9 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C return nil, errors.Wrap(err, "Error loading config file") } } else { - fmt.Println("Using config file:", v.ConfigFileUsed()) + if printConfig { + fmt.Fprintln(os.Stderr, "Using config file:", v.ConfigFileUsed()) + } } isLint := strings.Contains(cmd.Use, "lint") @@ -143,7 +146,7 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C } if len(cfg.Charts) > 0 || cfg.ProcessAllCharts { - fmt.Println("Version increment checking disabled.") + fmt.Fprintln(os.Stderr, "Version increment checking disabled.") cfg.CheckVersionIncrement = false } @@ -155,9 +158,9 @@ func LoadConfiguration(cfgFile string, cmd *cobra.Command, printConfig bool) (*C } func printCfg(cfg *Configuration) { - util.PrintDelimiterLine("-") - fmt.Println(" Configuration") - util.PrintDelimiterLine("-") + util.PrintDelimiterLineToWriter(os.Stderr, "-") + fmt.Fprintln(os.Stderr, " Configuration") + util.PrintDelimiterLineToWriter(os.Stderr, "-") e := reflect.ValueOf(cfg).Elem() typeOfCfg := e.Type() @@ -170,10 +173,10 @@ func printCfg(cfg *Configuration) { default: pattern = "%s: %s\n" } - fmt.Printf(pattern, typeOfCfg.Field(i).Name, e.Field(i).Interface()) + fmt.Fprintf(os.Stderr, pattern, typeOfCfg.Field(i).Name, e.Field(i).Interface()) } - util.PrintDelimiterLine("-") + util.PrintDelimiterLineToWriter(os.Stderr, "-") } func findConfigFile(fileName string) (string, error) { diff --git a/pkg/util/util.go b/pkg/util/util.go index 0b078d8..89ba277 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -16,6 +16,7 @@ package util import ( "fmt" + "io" "io/ioutil" "math/rand" "net" @@ -210,12 +211,17 @@ func BreakingChangeAllowed(left string, right string) (bool, error) { return !minor, err } +// Deprecated: To be removed in v4. Use PrintDelimiterLineToWriter instead. func PrintDelimiterLine(delimiterChar string) { + PrintDelimiterLineToWriter(os.Stdout, delimiterChar) +} + +func PrintDelimiterLineToWriter(w io.Writer, delimiterChar string) { delim := make([]string, 120) for i := 0; i < 120; i++ { delim[i] = delimiterChar } - fmt.Println(strings.Join(delim, "")) + fmt.Fprintln(w, strings.Join(delim, "")) } func SanitizeName(s string, maxLength int) string {