Skip to content

Commit

Permalink
Fail when the builder can't load its configuration. (open-telemetry#5731
Browse files Browse the repository at this point in the history
)

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 <[email protected]>
  • Loading branch information
jpeach authored Jul 25, 2022
1 parent 57e74cd commit 9930104
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions cmd/builder/internal/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 9930104

Please sign in to comment.