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
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
}