motiv-labs / janus

An API Gateway written in Go
https://hellofresh.gitbooks.io/janus
MIT License
2.79k stars 318 forks source link

Simplify cmd Version command #446

Closed itsksaurabh closed 4 years ago

itsksaurabh commented 4 years ago

Currently, The version of the cmd tool is set using an additional function which can be seen here.

The Cobra Library already has a feature to set the version using the underlying struct field Version.

// Version defines the version for this command. If this value is non-empty and the command does not // define a "version" flag, a "version" boolean flag will be added to the command and, if specified, // will print content of the "Version" variable. A shorthand "v" flag will also be added if the // command does not define one.

Version string

Official docs: https://github.com/spf13/cobra/blob/5cdf8e26ba7046dd743463f60102ab52602c6428/command.go#L90

My proposal to improve it :

// NewRootCmd creates a new instance of the root command
func NewRootCmd(version string) *cobra.Command {
    ctx := context.Background()

    cmd := &cobra.Command{
        Use:     "janus",
        Version: version,    <======== Sets the version 
         .......
        ..................
}

I have already tested it and working as expected. Please check the output below:

$ ./janustest --version
janus version 0.0.0-dev