-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[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.
- Loading branch information
Showing
6 changed files
with
85 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2022 Jetpack Technologies Inc and contributors. All rights reserved. | ||
// Use of this source code is governed by the license in the LICENSE file. | ||
|
||
package boxcli | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/spf13/cobra" | ||
"go.jetpack.io/devbox/build" | ||
) | ||
|
||
func VersionCmd() *cobra.Command { | ||
flags := &versionFlags{} | ||
command := &cobra.Command{ | ||
Use: "version", | ||
Args: cobra.NoArgs, | ||
RunE: versionCmdFunc(flags), | ||
} | ||
|
||
command.Flags().BoolVarP(&flags.verbose, "verbose", "v", false, // value | ||
"Verbose: displays additional version information", | ||
) | ||
return command | ||
} | ||
|
||
type versionFlags struct { | ||
verbose bool | ||
} | ||
|
||
func versionCmdFunc(flags *versionFlags) runFunc { | ||
return func(cmd *cobra.Command, args []string) error { | ||
v := getVersionInfo() | ||
if flags.verbose { | ||
fmt.Printf("Version: %v\n", v.Version) | ||
fmt.Printf("Platform: %v\n", v.Platform) | ||
fmt.Printf("Commit: %v\n", v.Commit) | ||
fmt.Printf("Commit Time: %v\n", v.CommitDate) | ||
fmt.Printf("Go Version: %v\n", v.GoVersion) | ||
} else { | ||
fmt.Printf("%v\n", v.Version) | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
type versionInfo struct { | ||
Version string | ||
IsPrerelease bool | ||
Platform string | ||
Commit string | ||
CommitDate string | ||
GoVersion string | ||
} | ||
|
||
func getVersionInfo() *versionInfo { | ||
v := &versionInfo{ | ||
Version: build.Version, | ||
Platform: fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH), | ||
Commit: build.Commit, | ||
CommitDate: build.CommitDate, | ||
GoVersion: runtime.Version(), | ||
} | ||
|
||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package build | ||
|
||
// Variables in this file are set via ldflags. | ||
var ( | ||
Version = "0.0.0-dev" | ||
Commit = "none" | ||
CommitDate = "unknown" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters