Print out errors on exit from cmd

This commit is contained in:
Zoltán Papp 2024-08-21 16:46:59 +02:00
parent c057fbd4f4
commit 86f1724efa

View File

@ -64,6 +64,8 @@ var (
Short: "Relay service", Short: "Relay service",
Long: "Relay service for Netbird agents", Long: "Relay service for Netbird agents",
RunE: execute, RunE: execute,
SilenceUsage: true,
SilenceErrors: true,
} }
) )
@ -128,7 +130,7 @@ func execute(cmd *cobra.Command, args []string) error {
err = cfg.Validate() err = cfg.Validate()
if err != nil { if err != nil {
log.Errorf("invalid config: %s", err) log.Errorf("invalid config: %s", err)
os.Exit(1) return err
} }
metricsServer, err := metrics.NewServer(metricsPort, "") metricsServer, err := metrics.NewServer(metricsPort, "")
@ -170,7 +172,7 @@ func execute(cmd *cobra.Command, args []string) error {
log.Infof("server will be available on: %s", srv.InstanceURL()) log.Infof("server will be available on: %s", srv.InstanceURL())
go func() { go func() {
if err := srv.Listen(srvListenerCfg); err != nil { if err := srv.Listen(srvListenerCfg); err != nil {
log.Errorf("failed to bind server: %s", err) log.Fatalf("failed to bind server: %s", err)
} }
}() }()
@ -185,6 +187,7 @@ func execute(cmd *cobra.Command, args []string) error {
shutDownErrors = multierror.Append(shutDownErrors, fmt.Errorf("failed to close server: %s", err)) shutDownErrors = multierror.Append(shutDownErrors, fmt.Errorf("failed to close server: %s", err))
} }
log.Infof("shutting down metrics server")
if err := metricsServer.Close(); err != nil { if err := metricsServer.Close(); err != nil {
shutDownErrors = multierror.Append(shutDownErrors, fmt.Errorf("failed to close metrics server: %v", err)) shutDownErrors = multierror.Append(shutDownErrors, fmt.Errorf("failed to close metrics server: %v", err))
} }
@ -200,8 +203,7 @@ func setupTLSCertManager(letsencryptDataDir string, letsencryptDomains ...string
} }
func main() { func main() {
err := rootCmd.Execute() if err := rootCmd.Execute(); err != nil {
if err != nil { log.Fatalf("%v", err)
os.Exit(1)
} }
} }