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

fix: version command #135

Merged
merged 8 commits into from
Dec 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 53 additions & 3 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,70 @@ package cmd

import (
"fmt"
"runtime/debug"
"time"

"github.com/spf13/cobra"
)

// GoBlueprintVersion is the version of the cli to be overwritten by goreleaser in the CI run with the version of the release in github
var GoBlueprintVersion string

// Go Blueprint needs to be built in a specific way to provide useful version information.
// First we try to get the version from ldflags embedded into GoBlueprintVersion.
// Then we try to get the version from from the go.mod build info.
// If Go Blueprint is installed with a specific version tag or using @latest then that version will be included in bi.Main.Version.
// This won't give any version info when running 'go install' with the source code locally.
// Finally we try to get the version from other embedded VCS info.
func getGoBlueprintVersion() string {
noVersionAvailable := "No version info available for this build, run 'go-blueprint help version' for additional info"

if len(GoBlueprintVersion) != 0 {
return GoBlueprintVersion
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably define this inline and not have a separate variable declared on line 14

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But GoBlueprintVersion needs to be on package level to be able to be set by the ldflag in .goreleaser.yml?
Or am I misunderstanding you?

}

bi, ok := debug.ReadBuildInfo()
if !ok {
return noVersionAvailable
}

// If no main version is available, Go defaults it to (devel)
if bi.Main.Version != "(devel)" {
return bi.Main.Version
}

var vcsRevision string
var vcsTime time.Time
for _, setting := range bi.Settings {
switch setting.Key {
case "vcs.revision":
vcsRevision = setting.Value
case "vcs.time":
vcsTime, _ = time.Parse(time.RFC3339, setting.Value)
}
}

if vcsRevision != "" {
return fmt.Sprintf("%s, (%s)", vcsRevision, vcsTime)
}

return noVersionAvailable
}

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Display application version information.",
Long: `The version command provides information about the application's version.
Use this command to check the current version of the application.`,
Long: `
The version command provides information about the application's version.

Go Blueprint requires version information to be embedded at compile time.
For detailed version information, Go Blueprint needs to be built as specified in the README installation instructions.
If Go Blueprint is built within a version control repository and other version info isn't available,
the revision hash will be used instead.
`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Go Blueprint CLI version %v\n", GoBlueprintVersion)
version := getGoBlueprintVersion()
fmt.Printf("Go Blueprint CLI version: %v\n", version)
},
}
2 changes: 2 additions & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
- SputNikPlop
- Yoquec
- rustafariandev
- limesten
- Ujstor