diff --git a/docs/reference/manual/hcloud_version.md b/docs/reference/manual/hcloud_version.md index 94a9f2896..729cec4b5 100644 --- a/docs/reference/manual/hcloud_version.md +++ b/docs/reference/manual/hcloud_version.md @@ -10,6 +10,7 @@ hcloud version ``` -h, --help help for version + --long Print more version information (true, false) ``` ### Options inherited from parent commands diff --git a/internal/cmd/version/version.go b/internal/cmd/version/version.go index 48a1d1f30..6899565ed 100644 --- a/internal/cmd/version/version.go +++ b/internal/cmd/version/version.go @@ -1,6 +1,11 @@ package version import ( + "fmt" + "runtime" + "runtime/debug" + "text/tabwriter" + "github.com/spf13/cobra" "github.com/hetznercloud/cli/internal/cmd/util" @@ -16,10 +21,39 @@ func NewCommand(_ state.State) *cobra.Command { DisableFlagsInUseLine: true, RunE: runVersion, } + cmd.Flags().Bool("long", false, "Print more version information (true, false)") return cmd } func runVersion(cmd *cobra.Command, _ []string) error { cmd.Printf("hcloud %s\n", version.Version) + + long, _ := cmd.Flags().GetBool("long") + if long { + tw := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 8, 2, ' ', 0) + fmt.Fprintln(tw) + fmt.Fprintf(tw, "go version:\t%s (%s)\n", runtime.Version(), runtime.Compiler) + fmt.Fprintf(tw, "platform:\t%s/%s\n", runtime.GOOS, runtime.GOARCH) + + if info, ok := debug.ReadBuildInfo(); ok { + rev := getSettingsValue(info.Settings, "vcs.revision", "unknown") + if modified := getSettingsValue(info.Settings, "vcs.modified", "false"); modified == "true" { + rev += " (modified)" + } + + fmt.Fprintf(tw, "revision:\t%s\n", rev) + fmt.Fprintf(tw, "revision date:\t%s\n", getSettingsValue(info.Settings, "vcs.time", "unknown")) + } + return tw.Flush() + } return nil } + +func getSettingsValue(settings []debug.BuildSetting, key, def string) string { + for _, setting := range settings { + if setting.Key == key { + return setting.Value + } + } + return def +}