|
| 1 | +// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. |
| 2 | +// Use of this source code is governed by the license in the LICENSE file. |
| 3 | + |
| 4 | +package boxcli |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "runtime" |
| 9 | + |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "go.jetpack.io/devbox/build" |
| 12 | +) |
| 13 | + |
| 14 | +func VersionCmd() *cobra.Command { |
| 15 | + flags := &versionFlags{} |
| 16 | + command := &cobra.Command{ |
| 17 | + Use: "version", |
| 18 | + Args: cobra.NoArgs, |
| 19 | + RunE: versionCmdFunc(flags), |
| 20 | + } |
| 21 | + |
| 22 | + command.Flags().BoolVarP(&flags.verbose, "verbose", "v", false, // value |
| 23 | + "Verbose: displays additional version information", |
| 24 | + ) |
| 25 | + return command |
| 26 | +} |
| 27 | + |
| 28 | +type versionFlags struct { |
| 29 | + verbose bool |
| 30 | +} |
| 31 | + |
| 32 | +func versionCmdFunc(flags *versionFlags) runFunc { |
| 33 | + return func(cmd *cobra.Command, args []string) error { |
| 34 | + v := getVersionInfo() |
| 35 | + if flags.verbose { |
| 36 | + fmt.Printf("Version: %v\n", v.Version) |
| 37 | + fmt.Printf("Platform: %v\n", v.Platform) |
| 38 | + fmt.Printf("Commit: %v\n", v.Commit) |
| 39 | + fmt.Printf("Commit Time: %v\n", v.CommitDate) |
| 40 | + fmt.Printf("Go Version: %v\n", v.GoVersion) |
| 41 | + } else { |
| 42 | + fmt.Printf("%v\n", v.Version) |
| 43 | + } |
| 44 | + return nil |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +type versionInfo struct { |
| 49 | + Version string |
| 50 | + IsPrerelease bool |
| 51 | + Platform string |
| 52 | + Commit string |
| 53 | + CommitDate string |
| 54 | + GoVersion string |
| 55 | +} |
| 56 | + |
| 57 | +func getVersionInfo() *versionInfo { |
| 58 | + v := &versionInfo{ |
| 59 | + Version: build.Version, |
| 60 | + Platform: fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH), |
| 61 | + Commit: build.Commit, |
| 62 | + CommitDate: build.CommitDate, |
| 63 | + GoVersion: runtime.Version(), |
| 64 | + } |
| 65 | + |
| 66 | + return v |
| 67 | +} |
0 commit comments