From 993010478c329afe3b674107059c386a7bf40d21 Mon Sep 17 00:00:00 2001 From: James Peach Date: Tue, 26 Jul 2022 01:13:25 +1000 Subject: [PATCH] Fail when the builder can't load its configuration. (#5731) If the builder can't load its build configuration file, it logs an error and continues. This results in building a collector that is not the one the user specified. Exit with a fatal error instead. Signed-off-by: James Peach --- CHANGELOG.md | 2 ++ cmd/builder/internal/command.go | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52e7fcacac6..c6be29c5588 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ ### 💡 Enhancements 💡 +- `ocb` now exits with an error if it fails to load the build configuration. (#5731) + ### 🧰 Bug fixes 🧰 ## v0.56.0 Beta diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index 33f7611e778..0e9831b165e 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -38,7 +38,9 @@ var ( // Command is the main entrypoint for this application func Command() (*cobra.Command, error) { cmd := &cobra.Command{ - Use: "ocb", + SilenceUsage: true, // Don't print usage on Run error. + SilenceErrors: true, // Don't print errors; main does it. + Use: "ocb", Long: fmt.Sprintf("OpenTelemetry Collector Builder (%s)", version) + ` ocb generates a custom OpenTelemetry Collector binary using the @@ -50,13 +52,11 @@ build configuration given by the "--config" argument. return err } if err := cfg.Validate(); err != nil { - cfg.Logger.Error("invalid configuration", zap.Error(err)) - return err + return fmt.Errorf("invalid configuration: %w", err) } if err := cfg.ParseModules(); err != nil { - cfg.Logger.Error("invalid module configuration", zap.Error(err)) - return err + return fmt.Errorf("invalid module configuration: %w", err) } return builder.GenerateAndCompile(cfg) @@ -86,30 +86,30 @@ build configuration given by the "--config" argument. cmd.AddCommand(versionCommand()) if err := k.Load(posflag.Provider(cmd.Flags(), ".", k), nil); err != nil { - cfg.Logger.Error("failed to load command line arguments", zap.Error(err)) + return nil, fmt.Errorf("failed to load command line arguments: %w", err) } return cmd, nil } func initConfig() error { - cfg.Logger.Info("OpenTelemetry Collector Builder", zap.String("version", version), zap.String("date", date)) + cfg.Logger.Info("OpenTelemetry Collector Builder", + zap.String("version", version), zap.String("date", date)) // load the config file if err := k.Load(file.Provider(cfgFile), yaml.Parser()); err != nil { - cfg.Logger.Error("failed to load config file", zap.String("config-file", cfgFile), zap.Error(err)) + return fmt.Errorf("failed to load configuration file: %w", err) } // handle env variables if err := k.Load(env.Provider("", ".", func(s string) string { return strings.ReplaceAll(s, ".", "_") }), nil); err != nil { - cfg.Logger.Error("failed to load env var", zap.Error(err)) + return fmt.Errorf("failed to load environment variables: %w", err) } if err := k.UnmarshalWithConf("", &cfg, koanf.UnmarshalConf{Tag: "mapstructure"}); err != nil { - cfg.Logger.Error("failed to unmarshal config", zap.Error(err)) - return err + return fmt.Errorf("failed to unmarshal configuration: %w", err) } cfg.Logger.Info("Using config file", zap.String("path", cfgFile))