Skip to content

Commit

Permalink
Add support for --version flag (#3442)
Browse files Browse the repository at this point in the history
Support `k6 --version` flag in addition to the already existing `k6 version` command.
---------

Co-authored-by: Ivan <2103732+codebien@users.noreply.github.com>
  • Loading branch information
ffapitalle and codebien authored Nov 10, 2023
1 parent 0db029a commit 46bb65e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ func newRootCommand(gs *state.GlobalState) *rootCommand {
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: c.persistentPreRunE,
Version: versionString(),
}

rootCmd.SetVersionTemplate(
`{{with .Name}}{{printf "%s " .}}{{end}}{{printf "v%s\n" .Version}}`,
)
rootCmd.PersistentFlags().AddFlagSet(rootCmdPersistentFlagSet(gs))
rootCmd.SetArgs(gs.CmdArgs[1:])
rootCmd.SetOut(gs.Stdout)
Expand Down
24 changes: 17 additions & 7 deletions cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,25 @@ import (
func TestVersion(t *testing.T) {
t.Parallel()

tests := map[string]struct {
args string
}{
"flag": {"--version"},
"subcommand": {"version"},
}

ts := NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "version"}
cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
assert.Contains(t, stdout, "k6 v"+consts.Version)
assert.Contains(t, stdout, runtime.Version())
assert.Contains(t, stdout, runtime.GOOS)
assert.Contains(t, stdout, runtime.GOARCH)
for _, tc := range tests {
ts.CmdArgs = []string{"k6", tc.args}
cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
assert.Contains(t, stdout, "k6 v"+consts.Version)
assert.Contains(t, stdout, runtime.Version())
assert.Contains(t, stdout, runtime.GOOS)
assert.Contains(t, stdout, runtime.GOARCH)
}

assert.Empty(t, ts.Stderr.Bytes())
assert.Empty(t, ts.LoggerHook.Drain())
Expand Down
29 changes: 18 additions & 11 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,30 @@ import (
"go.k6.io/k6/lib/consts"
)

func versionString() string {
v := consts.FullVersion()

if exts := ext.GetAll(); len(exts) > 0 {
extsDesc := make([]string, 0, len(exts))
for _, e := range exts {
extsDesc = append(extsDesc, fmt.Sprintf(" %s", e.String()))
}
v += fmt.Sprintf("\nExtensions:\n%s\n",
strings.Join(extsDesc, "\n"))
}
return v
}

func getCmdVersion(gs *state.GlobalState) *cobra.Command {
// versionCmd represents the version command.
return &cobra.Command{
Use: "version",
Short: "Show application version",
Long: `Show the application version and exit.`,
Run: func(_ *cobra.Command, _ []string) {
printToStdout(gs, fmt.Sprintf("k6 v%s\n", consts.FullVersion()))

if exts := ext.GetAll(); len(exts) > 0 {
extsDesc := make([]string, 0, len(exts))
for _, e := range exts {
extsDesc = append(extsDesc, fmt.Sprintf(" %s", e.String()))
}
printToStdout(gs, fmt.Sprintf("Extensions:\n%s\n",
strings.Join(extsDesc, "\n")))
}
Run: func(cmd *cobra.Command, _ []string) {
root := cmd.Root()
root.SetArgs([]string{"--version"})
_ = root.Execute()
},
}
}

0 comments on commit 46bb65e

Please sign in to comment.