Skip to content

Commit

Permalink
feat(relayer): versioning (#43)
Browse files Browse the repository at this point in the history
* feat: versioning

also adds a placeholder to command selector & params

* remove placeholder
  • Loading branch information
adamewozniak authored May 14, 2024
1 parent af630f4 commit dec39e7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions relayer/cmd/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func init() {
params.SetAddressPrefixes()
rootCmd.PersistentFlags().String(flagLogLevel, zerolog.InfoLevel.String(), "logging level")
rootCmd.PersistentFlags().String(flagLogFormat, logLevelText, "logging format; must be either json or text")
rootCmd.AddCommand(getVersionCmd())
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand Down
65 changes: 65 additions & 0 deletions relayer/cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

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

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

const (
flagFormat = "format"
)

var (
// Version defines the application version (defined at compile time)
Version = ""

// Commit defines the application commit hash (defined at compile time)
Commit = ""

versionFormat string
)

type versionInfo struct {
Version string `json:"version" yaml:"version"`
Commit string `json:"commit" yaml:"commit"`
Go string `json:"go" yaml:"go"`
}

func getVersionCmd() *cobra.Command {
versionCmd := &cobra.Command{
Use: "version",
Short: "Print binary version information",
RunE: func(_ *cobra.Command, _ []string) error {
verInfo := versionInfo{
Version: Version,
Commit: Commit,
Go: fmt.Sprintf("%s %s/%s", runtime.Version(), runtime.GOOS, runtime.GOARCH),
}

var bz []byte

var err error
switch versionFormat {
case "json":
bz, err = json.Marshal(verInfo)

default:
bz, err = yaml.Marshal(&verInfo)
}
if err != nil {
return err
}

_, err = fmt.Println(string(bz))
return err
},
}

versionCmd.Flags().StringVar(&versionFormat, flagFormat, "text", "Print the version in the given format (text|json)")

return versionCmd
}

0 comments on commit dec39e7

Please sign in to comment.