Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(builder): change output of version subcommand. #8994

Merged
merged 5 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

import (
"fmt"
"runtime/debug"

"github.com/spf13/cobra"
)

var (
version = "dev"
version = ""
devoc09 marked this conversation as resolved.
Show resolved Hide resolved
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

Check warning on line 29 in cmd/builder/internal/version.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/version.go#L21-L29

Added lines #L21 - L29 were not covered by tests
}

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
}

Check warning on line 41 in cmd/builder/internal/version.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/version.go#L38-L41

Added lines #L38 - L41 were not covered by tests
cmd.Println(fmt.Sprintf("%s version %s", cmd.Parent().Name(), version))
return nil

Check warning on line 43 in cmd/builder/internal/version.go

View check run for this annotation

Codecov / codecov/patch

cmd/builder/internal/version.go#L43

Added line #L43 was not covered by tests
},
}
}
Loading