Skip to content

Commit

Permalink
Switch to go-version (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h authored Nov 12, 2023
1 parent 7d6497c commit 66d8564
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 25 deletions.
11 changes: 2 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,8 @@ import (
)

var (
version string
buildHash string
buildDate string
debug bool
giltDir string
debug bool
giltDir string
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -61,10 +58,6 @@ https://github.com/retr0h/go-gilt
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(v string, bh string, bd string) {
version = v
buildHash = bh
buildDate = bd

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
77 changes: 61 additions & 16 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,74 @@
package cmd

import (
"encoding/json"
"fmt"
"runtime"

"github.com/spf13/cobra"
goVersion "go.hein.dev/go-version"
)

const debugHeader = `
Date: %s
Build: %s
Version: %s
Git Hash: %s
`

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Display the version of go-gilt",
Run: func(cmd *cobra.Command, args []string) {
goVersion := fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH)
fmt.Printf(debugHeader, buildDate, goVersion, version, buildHash)
},
// Info creates a formattable struct for output.
type Info struct {
Version string `json:"version,omitempty"`
Commit string `json:"commit,omitempty"`
Date string `json:"date,omitempty"`
}

// getVersion return the sensor's VersionInfo details.
func getVersion() *Info {
versionOutput := goVersion.New(version, commit, date)

output := &Info{
Version: versionOutput.Version,
Commit: versionOutput.Commit,
Date: versionOutput.Date,
}

return output
}

// toJSON converts the Info into a JSON String.
func (v *Info) toJSON() string {
bytes, _ := json.Marshal(v)

return string(bytes)
}

// toShortened converts the Version into a String.
func (v *Info) toShortened() string {
return fmt.Sprintf("Version: %s\n", v.Version)
}

// versionCmd represents the version command.
var (
shortened = false
version = "dev"
commit = "none"
date = "unknown"
output = "json"
versionCmd = &cobra.Command{
Use: "version",
Short: "Display the version of tool",
Run: func(cmd *cobra.Command, args []string) {
var response string

versionInfo := getVersion()

if shortened {
response = versionInfo.toShortened()
} else {
response = versionInfo.toJSON()
}

fmt.Printf("%s\n", response)
},
}
)

func init() {
rootCmd.AddCommand(versionCmd)
versionCmd.Flags().BoolVarP(&shortened, "short", "s", false, "Print just the version number.")
versionCmd.Flags().
StringVarP(&output, "output", "o", "json", "Output format. One of 'yaml' or 'json'.")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
github.com/xeipuuv/gojsonschema v0.0.0-20171230112544-511d08a359d1
go.hein.dev/go-version v0.1.0
sigs.k8s.io/yaml v1.4.0
)

Expand Down
Loading

0 comments on commit 66d8564

Please sign in to comment.