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

add --version option to the rootCmd #301

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/resonatehq/resonate/cmd/quickstart"
"github.com/resonatehq/resonate/cmd/schedules"
"github.com/resonatehq/resonate/cmd/serve"
"github.com/resonatehq/resonate/cmd/version"
"github.com/resonatehq/resonate/pkg/client"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -42,6 +43,7 @@ func init() {
rootCmd.AddCommand(dst.NewCmd())
rootCmd.AddCommand(serve.ServeCmd())
rootCmd.AddCommand(quickstart.NewCmd())
rootCmd.AddCommand(version.VersionCmd)
Copy link
Contributor

@guergabo guergabo Apr 30, 2024

Choose a reason for hiding this comment

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

the behavior should still remain using the cobra version flag: resonate --version


// Set default output
rootCmd.SetOut(os.Stdout)
Expand Down
27 changes: 27 additions & 0 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package version

import (
"fmt"
"runtime/debug"

"github.com/spf13/cobra"
)

var Version string = "dev"

var VersionCmd = &cobra.Command{
Use: "version",
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to make a whole command out of this a variable should do. check this out: https://www.digitalocean.com/community/tutorials/using-ldflags-to-set-version-information-for-go-applications

Run: func(cmd *cobra.Command, args []string) {
buildInfo, buildInfoRead := debug.ReadBuildInfo()
var commitHash = "dev"
if buildInfoRead {
for _, setting := range buildInfo.Settings {
if setting.Key == "vcs.revision" {
commitHash = setting.Value
break
}
}
}
fmt.Println("resonate version", Version, "("+commitHash+")")
},
}