Skip to content

Commit

Permalink
[devbox] Add version command (#2652)
Browse files Browse the repository at this point in the history
## 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
loreto committed Aug 29, 2022
1 parent 938eb1f commit be97495
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ builds:
binary: devbox
mod_timestamp: "{{ .CommitTimestamp }}" # For reproducible builds
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.CommitDate}}
- -s -w -X go.jetpack.io/devbox/build.Version={{.Version}}
- -s -w -X go.jetpack.io/devbox/build.Commit={{.Commit}}
- -s -w -X go.jetpack.io/devbox/build.CommitDate={{.CommitDate}}
env:
- CGO_ENABLED=0
- GO111MODULE=on
Expand Down
2 changes: 0 additions & 2 deletions boxcli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ func BuildCmd() *cobra.Command {
return command
}

type runFunc func(cmd *cobra.Command, args []string) error

func buildCmdFunc(flags *docker.BuildFlags) runFunc {
return func(cmd *cobra.Command, args []string) error {
path := pathArg(args)
Expand Down
5 changes: 4 additions & 1 deletion boxcli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ func RootCmd() *cobra.Command {
command.AddCommand(AddCmd())
command.AddCommand(BuildCmd())
command.AddCommand(GenerateCmd())
command.AddCommand(InitCmd())
command.AddCommand(PlanCmd())
command.AddCommand(RemoveCmd())
command.AddCommand(InitCmd())
command.AddCommand(ShellCmd())
command.AddCommand(VersionCmd())
return command
}

Expand All @@ -51,3 +52,5 @@ func Main() {
os.Exit(1)
}
}

type runFunc func(cmd *cobra.Command, args []string) error
67 changes: 67 additions & 0 deletions boxcli/version.go
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
}
8 changes: 8 additions & 0 deletions build/build.go
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"
)
4 changes: 3 additions & 1 deletion cmd/devbox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

package main

import "go.jetpack.io/devbox/boxcli"
import (
"go.jetpack.io/devbox/boxcli"
)

func main() {
boxcli.Main()
Expand Down

0 comments on commit be97495

Please sign in to comment.