Skip to content

Commit

Permalink
feat: add version subcommand (#197)
Browse files Browse the repository at this point in the history
The `version` subcommand without any flags prints out a general version
information, good to check what version we have.

    ❯ ./bin/flintlockd_amd64 version
    flintlock v0.1.0-alpha.1-11-af7a1a9

The `version` subcommand with `--short` flag prints out only the
version, useful for scripting.

    ❯ ./bin/flintlockd_amd64 version --short
    v0.1.0-alpha.1-11-af7a1a9

The `version` subcommand with `--long` flag prints out a detailed version
information, useful for bug reports.

    ❯ ./bin/flintlockd_amd64 version --long
    flintlock
      Version:    v0.1.0-alpha.1-12-gaf7a1a9
      CommitHash: af7a1a9
      BuildDate:  2021-11-03T11:38:43Z

== Why a subcommand and not a `-v` or `-V` flag?

Because it can be confusing. Some applications are using `-v` for
version, some are using `-V` for version because `-v` is the verbose
flag. Go, kubectl, and Helm follows the same logic, they have a
`version` subcommand.

== Different flags

* I find it annoying to parse version information from `version`
  subcommands all the time when I want to automate something, that's the
  `--short`.
* I find it useful to print out everything we have for debugging or
  filing bug reports, that's the `--long` flag.
* I find it useful to print out the package/application name, so even if
  the binary was renamed I can see what is the name of the application,
  but without extra information like commit hash or build date.  That's
  the no-flag option.

== Moved the log.Info call

In general, it's useful to print out the info log on `run` and `gw`, but
not for `version`.

Fixes #163
  • Loading branch information
yitsushi authored Nov 3, 2021
1 parent 7f6db21 commit 7a30dd9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
9 changes: 9 additions & 0 deletions internal/command/gw/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
mvmv1 "github.com/weaveworks/flintlock/api/services/microvm/v1alpha1"
cmdflags "github.com/weaveworks/flintlock/internal/command/flags"
"github.com/weaveworks/flintlock/internal/config"
"github.com/weaveworks/flintlock/internal/version"
"github.com/weaveworks/flintlock/pkg/flags"
"github.com/weaveworks/flintlock/pkg/log"
)
Expand All @@ -27,6 +28,14 @@ func NewCommand(cfg *config.Config) *cobra.Command {
PreRunE: func(c *cobra.Command, _ []string) error {
flags.BindCommandToViper(c)

logger := log.GetLogger(c.Context())
logger.Infof(
"flintlockd, version=%s, built_on=%s, commit=%s",
version.Version,
version.BuildDate,
version.CommitHash,
)

return nil
},
RunE: func(c *cobra.Command, _ []string) error {
Expand Down
55 changes: 51 additions & 4 deletions internal/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ func NewRootCommand() (*cobra.Command, error) {
return fmt.Errorf("configuring logging: %w", err)
}

logger := log.GetLogger(cmd.Context())
logger.Infof("flintlockd, version=%s, built_on=%s, commit=%s", version.Version, version.BuildDate, version.CommitHash)

return nil
},
RunE: func(c *cobra.Command, _ []string) error {
Expand Down Expand Up @@ -63,7 +60,7 @@ func initCobra() {
viper.AddConfigPath("$HOME/.config/flintlockd/")
}

viper.ReadInConfig() //nolint: errcheck
_ = viper.ReadInConfig()
}

func addRootSubCommands(cmd *cobra.Command, cfg *config.Config) error {
Expand All @@ -73,9 +70,59 @@ func addRootSubCommands(cmd *cobra.Command, cfg *config.Config) error {
}

cmd.AddCommand(runCmd)
cmd.AddCommand(versionCommand())

gwCmd := gw.NewCommand(cfg)
cmd.AddCommand(gwCmd)

return nil
}

func versionCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "version",
Short: "Print the version number of flintlock",
RunE: func(cmd *cobra.Command, args []string) error {
var (
long, short bool
err error
)

if long, err = cmd.Flags().GetBool("long"); err != nil {
return nil
}

if short, err = cmd.Flags().GetBool("short"); err != nil {
return nil
}

if short {
fmt.Fprintln(cmd.OutOrStdout(), version.Version)

return nil
}

if long {
fmt.Fprintf(
cmd.OutOrStdout(),
"%s\n Version: %s\n CommitHash: %s\n BuildDate: %s\n",
version.PackageName,
version.Version,
version.CommitHash,
version.BuildDate,
)

return nil
}

fmt.Fprintf(cmd.OutOrStdout(), "%s %s\n", version.PackageName, version.Version)

return nil
},
}

_ = cmd.Flags().Bool("long", false, "Print long version information")
_ = cmd.Flags().Bool("short", false, "Print short version information")

return cmd
}
9 changes: 9 additions & 0 deletions internal/command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
cmdflags "github.com/weaveworks/flintlock/internal/command/flags"
"github.com/weaveworks/flintlock/internal/config"
"github.com/weaveworks/flintlock/internal/inject"
"github.com/weaveworks/flintlock/internal/version"
"github.com/weaveworks/flintlock/pkg/defaults"
"github.com/weaveworks/flintlock/pkg/flags"
"github.com/weaveworks/flintlock/pkg/log"
Expand All @@ -32,6 +33,14 @@ func NewCommand(cfg *config.Config) (*cobra.Command, error) {
PreRunE: func(c *cobra.Command, _ []string) error {
flags.BindCommandToViper(c)

logger := log.GetLogger(c.Context())
logger.Infof(
"flintlockd, version=%s, built_on=%s, commit=%s",
version.Version,
version.BuildDate,
version.CommitHash,
)

return nil
},
RunE: func(c *cobra.Command, _ []string) error {
Expand Down
4 changes: 4 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package version

// PackageName is the name of the package, not just FlintlockD,
// all commands fall under this name.
const PackageName = "flintlock"

var (
Version = "undefined" // Specifies the app version
BuildDate = "undefined" // Specifies the build date
Expand Down

0 comments on commit 7a30dd9

Please sign in to comment.