diff --git a/cmd/builder/internal/command.go b/cmd/builder/internal/command.go index 3af77178727..821f5b48397 100644 --- a/cmd/builder/internal/command.go +++ b/cmd/builder/internal/command.go @@ -123,7 +123,7 @@ configuration is provided, ocb will generate a default Collector. func initConfig(flags *flag.FlagSet) error { cfg.Logger.Info("OpenTelemetry Collector Builder", - zap.String("version", version)) + zap.String("version", version), zap.String("date", date)) var provider koanf.Provider diff --git a/cmd/builder/internal/version.go b/cmd/builder/internal/version.go index 2e1f39e9f44..9ffbbf30955 100644 --- a/cmd/builder/internal/version.go +++ b/cmd/builder/internal/version.go @@ -12,12 +12,21 @@ import ( var ( version = "" + date = "unknown" ) -func init() { - // the second returned value is a boolean, which is true if the binaries are built with module support. - info, _ := debug.ReadBuildInfo() - version = info.Main.Version +// binVersion returns the version of the binary. +// If the version is not set, it attempts to read the build information. +// Returns an error if the build information cannot be read. +func binVersion() (string, error) { + if version != "" { + return version, nil + } + info, ok := debug.ReadBuildInfo() + if !ok { + return "", fmt.Errorf("failed to read build info") + } + return info.Main.Version, nil } func versionCommand() *cobra.Command { @@ -26,6 +35,10 @@ func versionCommand() *cobra.Command { Short: "Version of ocb", Long: "Prints the version of the ocb binary", RunE: func(cmd *cobra.Command, _ []string) error { + version, err := binVersion() + if err != nil { + return err + } cmd.Println(fmt.Sprintf("%s version %s", cmd.Parent().Name(), version)) return nil },