Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for --version flag #3442

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 17 additions & 10 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) {
ffapitalle marked this conversation as resolved.
Show resolved Hide resolved
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")))
}
root := cmd.Root()
root.SetArgs([]string{"--version"})
root.Execute()
},
}
}