Skip to content

Commit

Permalink
Add version pkg and struct to print like the cool kids
Browse files Browse the repository at this point in the history
  • Loading branch information
saheljalal committed Sep 27, 2019
1 parent d8994ed commit 900a6d9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 10 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package cmd

import (
"fmt"
"os"

"github.com/pokanop/nostromo/log"
"github.com/pokanop/nostromo/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var ver *version.Info

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "nostromo",
Expand Down Expand Up @@ -36,7 +38,12 @@ func Execute() {

// SetVersion to inject version info
func SetVersion(v, c, d string) {
rootCmd.Version = fmt.Sprintf("[%s %s %s]", v, c, d)
ver = &version.Info{
SemVer: v,
GitCommit: c,
BuildDate: d,
}
rootCmd.Version = ver.Formatted()
}

func init() {
Expand All @@ -58,5 +65,5 @@ func printUsage(cmd *cobra.Command) {
}

func printVersion() {
log.Regularf("nostromo version %s\n", rootCmd.Version)
log.Regularf("Nostromo: %s\n", rootCmd.Version)
}
15 changes: 15 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package version

import "fmt"

// Info identifying version information for releases
type Info struct {
SemVer string
GitCommit string
BuildDate string
}

// Formatted returns version formatted string
func (i *Info) Formatted() string {
return fmt.Sprintf("&version.Version{SemVer:\"%s\", GitCommit:\"%s\", BuildDate:\"%s\"}", i.SemVer, i.GitCommit, i.BuildDate)
}

0 comments on commit 900a6d9

Please sign in to comment.