Skip to content

Commit 7a30dd9

Browse files
authored
feat: add version subcommand (#197)
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
1 parent 7f6db21 commit 7a30dd9

File tree

4 files changed

+73
-4
lines changed

4 files changed

+73
-4
lines changed

internal/command/gw/gw.go

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
mvmv1 "github.com/weaveworks/flintlock/api/services/microvm/v1alpha1"
1616
cmdflags "github.com/weaveworks/flintlock/internal/command/flags"
1717
"github.com/weaveworks/flintlock/internal/config"
18+
"github.com/weaveworks/flintlock/internal/version"
1819
"github.com/weaveworks/flintlock/pkg/flags"
1920
"github.com/weaveworks/flintlock/pkg/log"
2021
)
@@ -27,6 +28,14 @@ func NewCommand(cfg *config.Config) *cobra.Command {
2728
PreRunE: func(c *cobra.Command, _ []string) error {
2829
flags.BindCommandToViper(c)
2930

31+
logger := log.GetLogger(c.Context())
32+
logger.Infof(
33+
"flintlockd, version=%s, built_on=%s, commit=%s",
34+
version.Version,
35+
version.BuildDate,
36+
version.CommitHash,
37+
)
38+
3039
return nil
3140
},
3241
RunE: func(c *cobra.Command, _ []string) error {

internal/command/root.go

+51-4
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ func NewRootCommand() (*cobra.Command, error) {
2929
return fmt.Errorf("configuring logging: %w", err)
3030
}
3131

32-
logger := log.GetLogger(cmd.Context())
33-
logger.Infof("flintlockd, version=%s, built_on=%s, commit=%s", version.Version, version.BuildDate, version.CommitHash)
34-
3532
return nil
3633
},
3734
RunE: func(c *cobra.Command, _ []string) error {
@@ -63,7 +60,7 @@ func initCobra() {
6360
viper.AddConfigPath("$HOME/.config/flintlockd/")
6461
}
6562

66-
viper.ReadInConfig() //nolint: errcheck
63+
_ = viper.ReadInConfig()
6764
}
6865

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

7572
cmd.AddCommand(runCmd)
73+
cmd.AddCommand(versionCommand())
7674

7775
gwCmd := gw.NewCommand(cfg)
7876
cmd.AddCommand(gwCmd)
7977

8078
return nil
8179
}
80+
81+
func versionCommand() *cobra.Command {
82+
cmd := &cobra.Command{
83+
Use: "version",
84+
Short: "Print the version number of flintlock",
85+
RunE: func(cmd *cobra.Command, args []string) error {
86+
var (
87+
long, short bool
88+
err error
89+
)
90+
91+
if long, err = cmd.Flags().GetBool("long"); err != nil {
92+
return nil
93+
}
94+
95+
if short, err = cmd.Flags().GetBool("short"); err != nil {
96+
return nil
97+
}
98+
99+
if short {
100+
fmt.Fprintln(cmd.OutOrStdout(), version.Version)
101+
102+
return nil
103+
}
104+
105+
if long {
106+
fmt.Fprintf(
107+
cmd.OutOrStdout(),
108+
"%s\n Version: %s\n CommitHash: %s\n BuildDate: %s\n",
109+
version.PackageName,
110+
version.Version,
111+
version.CommitHash,
112+
version.BuildDate,
113+
)
114+
115+
return nil
116+
}
117+
118+
fmt.Fprintf(cmd.OutOrStdout(), "%s %s\n", version.PackageName, version.Version)
119+
120+
return nil
121+
},
122+
}
123+
124+
_ = cmd.Flags().Bool("long", false, "Print long version information")
125+
_ = cmd.Flags().Bool("short", false, "Print short version information")
126+
127+
return cmd
128+
}

internal/command/run/run.go

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
cmdflags "github.com/weaveworks/flintlock/internal/command/flags"
2020
"github.com/weaveworks/flintlock/internal/config"
2121
"github.com/weaveworks/flintlock/internal/inject"
22+
"github.com/weaveworks/flintlock/internal/version"
2223
"github.com/weaveworks/flintlock/pkg/defaults"
2324
"github.com/weaveworks/flintlock/pkg/flags"
2425
"github.com/weaveworks/flintlock/pkg/log"
@@ -32,6 +33,14 @@ func NewCommand(cfg *config.Config) (*cobra.Command, error) {
3233
PreRunE: func(c *cobra.Command, _ []string) error {
3334
flags.BindCommandToViper(c)
3435

36+
logger := log.GetLogger(c.Context())
37+
logger.Infof(
38+
"flintlockd, version=%s, built_on=%s, commit=%s",
39+
version.Version,
40+
version.BuildDate,
41+
version.CommitHash,
42+
)
43+
3544
return nil
3645
},
3746
RunE: func(c *cobra.Command, _ []string) error {

internal/version/version.go

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package version
22

3+
// PackageName is the name of the package, not just FlintlockD,
4+
// all commands fall under this name.
5+
const PackageName = "flintlock"
6+
37
var (
48
Version = "undefined" // Specifies the app version
59
BuildDate = "undefined" // Specifies the build date

0 commit comments

Comments
 (0)