Skip to content

Commit

Permalink
Improve version printing
Browse files Browse the repository at this point in the history
Always detect the date and git hash at build time. This means they will
appear in the version output even if someone builds from source (e.g.
`go get`).
  • Loading branch information
dansimau committed Dec 28, 2018
1 parent 8085049 commit 54691ae
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
10 changes: 0 additions & 10 deletions astro/cli/astro/cmd/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,6 @@ var planCmd = &cobra.Command{
},
}

var versionCmd = &cobra.Command{
Use: "version",
DisableFlagsInUseLine: true,
Short: "Print astro version",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("astro version %s (%s) built %s\n", version, commit, date)
return nil
},
}

func userVariables() *astro.UserVariables {
values := make(map[string]string)
filters := make(map[string]bool)
Expand Down
4 changes: 0 additions & 4 deletions astro/cli/astro/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import (
)

var (
version = "DEV"
commit = "none"
date = "unknown"

userCfgFile string
trace bool
userVars map[string]string
Expand Down
65 changes: 65 additions & 0 deletions astro/cli/astro/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"fmt"
"os/exec"
"strings"
"time"

"github.com/spf13/cobra"
)

var (
// When a release happens, the value of this variable will be overwritten
// by the linker to match the release version.
version = "dev"
commit = currentGitHash()
date = currentDate()
)

// currentGitHash returns the git hash of the current working directory or an
// empty string if the hash can't be determined (i.e. git is not installed).
func currentGitHash() string {
git := exec.Command("git", "rev-parse", "--short", "HEAD")

output, err := git.Output()
if err != nil {
return ""
}

return strings.TrimSpace(string(output))
}

// currentDate returns the current date in a format suitable for printing in
// the version output.
func currentDate() string {
return time.Now().Format(time.RFC3339)
}

var versionCmd = &cobra.Command{
Use: "version",
DisableFlagsInUseLine: true,
Short: "Print astro version",
RunE: func(cmd *cobra.Command, args []string) error {
versionString := []string{
"astro version",
version,
}

if commit != "" {
versionString = append(versionString, fmt.Sprintf("(%s)", commit))
} else {
versionString = append(versionString, "(hash unknown)")
}

versionString = append(versionString, fmt.Sprintf("built %s", date))

println(strings.Join(versionString, " "))

return nil
},
}

func init() {
rootCmd.AddCommand(versionCmd)
}

0 comments on commit 54691ae

Please sign in to comment.