Skip to content

Commit be97495

Browse files
authored
[devbox] Add version command (#2652)
## Summary Add `devbox version`. It prints the version that is set via ldflags (via goreleaser). I tried to make BuildInfo work ... but couldn't. I think for now ldflags are more reliable. That said, I restricted the ldflags to the same subset that we'll get via BuildInfo) so it should later be easy to switch between them. ## How was it tested? Built locally and ran. ## Is this change backwards-compatible? Yes.
1 parent 938eb1f commit be97495

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

.goreleaser.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ builds:
77
binary: devbox
88
mod_timestamp: "{{ .CommitTimestamp }}" # For reproducible builds
99
ldflags:
10-
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}}
10+
- -s -w -X go.jetpack.io/devbox/build.Version={{.Version}}
11+
- -s -w -X go.jetpack.io/devbox/build.Commit={{.Commit}}
12+
- -s -w -X go.jetpack.io/devbox/build.CommitDate={{.CommitDate}}
1113
env:
1214
- CGO_ENABLED=0
1315
- GO111MODULE=on

boxcli/build.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ func BuildCmd() *cobra.Command {
2626
return command
2727
}
2828

29-
type runFunc func(cmd *cobra.Command, args []string) error
30-
3129
func buildCmdFunc(flags *docker.BuildFlags) runFunc {
3230
return func(cmd *cobra.Command, args []string) error {
3331
path := pathArg(args)

boxcli/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ func RootCmd() *cobra.Command {
3333
command.AddCommand(AddCmd())
3434
command.AddCommand(BuildCmd())
3535
command.AddCommand(GenerateCmd())
36+
command.AddCommand(InitCmd())
3637
command.AddCommand(PlanCmd())
3738
command.AddCommand(RemoveCmd())
38-
command.AddCommand(InitCmd())
3939
command.AddCommand(ShellCmd())
40+
command.AddCommand(VersionCmd())
4041
return command
4142
}
4243

@@ -51,3 +52,5 @@ func Main() {
5152
os.Exit(1)
5253
}
5354
}
55+
56+
type runFunc func(cmd *cobra.Command, args []string) error

boxcli/version.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}

build/build.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package build
2+
3+
// Variables in this file are set via ldflags.
4+
var (
5+
Version = "0.0.0-dev"
6+
Commit = "none"
7+
CommitDate = "unknown"
8+
)

cmd/devbox/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
package main
55

6-
import "go.jetpack.io/devbox/boxcli"
6+
import (
7+
"go.jetpack.io/devbox/boxcli"
8+
)
79

810
func main() {
911
boxcli.Main()

0 commit comments

Comments
 (0)