-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from go-ksplit/laverya/version-command
add version command
- Loading branch information
Showing
9 changed files
with
146 additions
and
7 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,36 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/go-ksplit/ksplit/pkg/version" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func Version() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "version", | ||
Short: "ksplit version information", | ||
Long: `Prints the current version of ksplit`, | ||
SilenceErrors: true, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
viper.BindPFlags(cmd.Flags()) | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
v := viper.GetViper() | ||
if v.GetBool("verbose") { | ||
fmt.Printf("ksplit %s built at %s with sha %s\n", version.Version(), version.BuildTime().Format(time.RFC3339), version.GitSHA()) | ||
} else { | ||
fmt.Printf("ksplit %s\n", version.Version()) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().Bool("verbose", false, "when set, also print build info") | ||
|
||
return cmd | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pkg | ||
package splitter | ||
|
||
import ( | ||
"bytes" | ||
|
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package pkg | ||
package splitter | ||
|
||
import ( | ||
"os" | ||
|
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,7 @@ | ||
package version | ||
|
||
// NOTE: these variables are injected at build time | ||
|
||
var ( | ||
version, gitSHA, buildTime string | ||
) |
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,64 @@ | ||
package version | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
var ( | ||
build Build | ||
hasBuilt = false | ||
) | ||
|
||
// Build holds details about this build of the Ship binary | ||
type Build struct { | ||
Version string `json:"version,omitempty"` | ||
GitSHA string `json:"git,omitempty"` | ||
BuildTime time.Time `json:"buildTime,omitempty"` | ||
TimeFallback string `json:"buildTimeFallback,omitempty"` | ||
} | ||
|
||
// Init sets up the version info from build args | ||
func Init() { | ||
build.Version = version | ||
if len(gitSHA) >= 7 { | ||
build.GitSHA = gitSHA[:7] | ||
} | ||
var err error | ||
build.BuildTime, err = time.Parse(time.RFC3339, buildTime) | ||
if err != nil { | ||
build.TimeFallback = buildTime | ||
} | ||
hasBuilt = true | ||
} | ||
|
||
// GetBuild gets the build | ||
func GetBuild() Build { | ||
if !hasBuilt { | ||
Init() | ||
} | ||
return build | ||
} | ||
|
||
// Version gets the version | ||
func Version() string { | ||
if !hasBuilt { | ||
Init() | ||
} | ||
return build.Version | ||
} | ||
|
||
// GitSHA gets the gitsha | ||
func GitSHA() string { | ||
if !hasBuilt { | ||
Init() | ||
} | ||
return build.GitSHA | ||
} | ||
|
||
// BuildTime gets the build time | ||
func BuildTime() time.Time { | ||
if !hasBuilt { | ||
Init() | ||
} | ||
return build.BuildTime | ||
} |