Skip to content

Commit da11b64

Browse files
Add a --version flag to the pulumi command (#23898)
`pulumi --version` previously failed with "unknown flag"; the version was only available via the `pulumi version` subcommand. This wires up cobra's built-in version handling by setting the root command's `Version` field, with a version template so `pulumi --version` prints exactly the same output as `pulumi version` (just the version string). Notes: - The persistent `--verbose`/`-v` shorthand is unaffected: cobra only claims `-v` for `--version` when the shorthand is free, so `--version` gets no shorthand. - Cobra handles `--version` before `PersistentPreRunE`, so `pulumi --version` skips the async update check that `pulumi version` triggers, which seems desirable for a plain version query. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 03bf3a8 commit da11b64

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: improvement
2+
body: Add a `--version` flag to the `pulumi` command that prints the version, same
3+
as `pulumi version`
4+
component: cli
5+
custom:
6+
PR: "23898"

‎pkg/cmd/pulumi/pulumi.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ func NewPulumiCmd() (*cobra.Command, func()) {
260260
cmd = &cobra.Command{
261261
Use: "pulumi",
262262
Short: "Pulumi command line",
263+
Version: version.Version,
263264
SilenceErrors: true,
264265
SilenceUsage: true,
265266
Long: "Pulumi - Modern Infrastructure as Code\n" +
@@ -444,6 +445,8 @@ func NewPulumiCmd() (*cobra.Command, func()) {
444445
},
445446
}
446447

448+
cmd.SetVersionTemplate("{{.Version}}\n")
449+
447450
cmd.PersistentFlags().StringVarP(&cwd, "cwd", "C", "",
448451
"Run pulumi as if it had been started in another directory")
449452
cmd.PersistentFlags().BoolVarP(&cmdutil.Emoji, "emoji", "e", runtime.GOOS == "darwin",

‎tests/smoke/smoke_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,3 +1366,17 @@ func TestPulumiNewEmptyOperations(t *testing.T) {
13661366
e.RunCommand("pulumi", "stack", "init", "testing")
13671367
e.RunCommand("pulumi", "config", "set", "key", "value")
13681368
}
1369+
1370+
// Test that `pulumi --version` prints the same output as `pulumi version`.
1371+
func TestVersionFlag(t *testing.T) {
1372+
t.Parallel()
1373+
1374+
e := ptesting.NewEnvironment(t)
1375+
defer e.DeleteIfNotFailed()
1376+
1377+
versionCmdStdout, _ := e.RunCommand("pulumi", "version")
1378+
versionFlagStdout, _ := e.RunCommand("pulumi", "--version")
1379+
1380+
assert.NotEmpty(t, strings.TrimSpace(versionCmdStdout))
1381+
assert.Equal(t, versionCmdStdout, versionFlagStdout)
1382+
}

0 commit comments

Comments
 (0)