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
This commit is contained in:
Jan Dittberner 2022-12-03 13:02:04 +01:00
parent a688eb105b
commit f4a1958307

View file

@ -18,9 +18,11 @@ limitations under the License.
package main package main
import ( import (
"bytes"
"context" "context"
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"strings" "strings"
@ -54,8 +56,6 @@ func main() {
logger.SetOutput(os.Stdout) logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.InfoLevel) 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.StringVar(&configFile, "config", defaultConfigFile, "signer client configuration file")
flag.BoolVar(&showVersion, "version", false, "show version") flag.BoolVar(&showVersion, "version", false, "show version")
flag.BoolVar(&verbose, "verbose", false, "verbose output") flag.BoolVar(&verbose, "verbose", false, "verbose output")
@ -69,27 +69,31 @@ func main() {
flag.Parse() flag.Parse()
if showVersion {
return
}
parsedLevel, err := logrus.ParseLevel(logLevel) parsedLevel, err := logrus.ParseLevel(logLevel)
if err != nil { if err != nil {
logger.WithError(err).Fatal("could not parse log level") logger.WithError(err).Fatal("could not parse log level")
} }
logger.SetLevel(parsedLevel)
if generateConfig { if generateConfig {
if err = generateDefaultConfig(); err != nil { if err = generateDefaultConfig(); err != nil {
logger.WithError(err).Fatal("could not generate default configuration") 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 return
} }
logger.SetLevel(parsedLevel)
if err := startClient(configFile, logger); err != nil { if err := startClient(configFile, logger); err != nil {
logger.WithError(err).Fatal("client failure") logger.WithError(err).Fatal("client failure")
os.Exit(1)
} }
} }
@ -100,18 +104,26 @@ serial:
baud: 112500 baud: 112500
` `
cfg, err := config.LoadConfiguration(strings.NewReader( cfg, err := config.LoadConfiguration(strings.NewReader(defaultBaseConfiguration))
defaultBaseConfiguration))
if err != nil { if err != nil {
return fmt.Errorf("could not load empty configuration: %w", err) 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 { if err = enc.Encode(cfg); err != nil {
return fmt.Errorf("could not encode: %w", err) 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 return nil
} }