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 d8696d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 9 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,11 @@ 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,
}
}

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

func printVersion() {
log.Regularf("nostromo version %s\n", rootCmd.Version)
log.Regularf("Nostromo: %s\n", ver.Formatted())
}
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.Info{SemVer:\"%s\", GitCommit:\"%s\", BuildDate:\"%s\"}", i.SemVer, i.GitCommit, i.BuildDate)
}

0 comments on commit d8696d2

Please sign in to comment.