Skip to content

Commit

Permalink
refactor(builder): change output of version subcommand. (#8994)
Browse files Browse the repository at this point in the history
With this change, running `builder version` on binaries installed with
`go install` will output the version specified at the suffix.

If it's under development, the following output will be obtained.
```
$ GO111MODULE=on CGO_ENABLED=0 go run . version
ocb version (devel)
```

**Link to tracking Issue:** #8770

---------

Co-authored-by: Bogdan Drutu <[email protected]>
  • Loading branch information
devoc09 and bogdandrutu committed Dec 9, 2023
1 parent 7c58e71 commit b0f618e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .chloggen/output_version_of_binary.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

change_type: enhancement

component: cmd/builder

note: "running builder version on binaries installed with `go install` will output the version specified at the suffix."

issues: [8770]

subtext:

change_logs: [user]
24 changes: 22 additions & 2 deletions cmd/builder/internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,42 @@ package internal // import "go.opentelemetry.io/collector/cmd/builder/internal"

import (
"fmt"
"runtime/debug"

"github.com/spf13/cobra"
)

var (
version = "dev"
version = ""
date = "unknown"
)

// 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 {
return &cobra.Command{
Use: "version",
Short: "Version of ocb",
Long: "Prints the version of the ocb binary",
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
version, err := binVersion()
if err != nil {
return err
}
cmd.Println(fmt.Sprintf("%s version %s", cmd.Parent().Name(), version))
return nil
},
}
}

0 comments on commit b0f618e

Please sign in to comment.