From f4a19583072cc45c0a34ea1563b7ba1ec9902fa1 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 3 Dec 2022 13:02:04 +0100 Subject: [PATCH] Improve -generate-config handling - do not print client version when running with the -generate-config option - add a YAML document prefix before the raw YAML output - use proper exit codes on errors --- cmd/signerclient/main.go | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/cmd/signerclient/main.go b/cmd/signerclient/main.go index 939d444..4358c56 100644 --- a/cmd/signerclient/main.go +++ b/cmd/signerclient/main.go @@ -18,9 +18,11 @@ limitations under the License. package main import ( + "bytes" "context" "flag" "fmt" + "io" "os" "strings" @@ -54,8 +56,6 @@ func main() { logger.SetOutput(os.Stdout) logger.SetLevel(logrus.InfoLevel) - logger.Infof("cacert-gosignerclient %s (%s) - build %s", version, commit, date) - flag.StringVar(&configFile, "config", defaultConfigFile, "signer client configuration file") flag.BoolVar(&showVersion, "version", false, "show version") flag.BoolVar(&verbose, "verbose", false, "verbose output") @@ -69,27 +69,31 @@ func main() { flag.Parse() - if showVersion { - return - } - parsedLevel, err := logrus.ParseLevel(logLevel) if err != nil { logger.WithError(err).Fatal("could not parse log level") } - logger.SetLevel(parsedLevel) - if generateConfig { if err = generateDefaultConfig(); err != nil { logger.WithError(err).Fatal("could not generate default configuration") } + os.Exit(1) + } + + logger.Infof("cacert-gosignerclient %s (%s) - build %s", version, commit, date) + + if showVersion { return } + logger.SetLevel(parsedLevel) + if err := startClient(configFile, logger); err != nil { logger.WithError(err).Fatal("client failure") + + os.Exit(1) } } @@ -100,18 +104,26 @@ serial: baud: 112500 ` - cfg, err := config.LoadConfiguration(strings.NewReader( - defaultBaseConfiguration)) + cfg, err := config.LoadConfiguration(strings.NewReader(defaultBaseConfiguration)) if err != nil { return fmt.Errorf("could not load empty configuration: %w", err) } - enc := yaml.NewEncoder(os.Stdout) + buf := &bytes.Buffer{} + + buf.WriteString("---\n") + + enc := yaml.NewEncoder(buf) if err = enc.Encode(cfg); err != nil { return fmt.Errorf("could not encode: %w", err) } + _, err = io.Copy(os.Stdout, buf) + if err != nil { + return fmt.Errorf("could not write output: %w", err) + } + return nil }