From 88223ffe81e39a43fe8da18ab46c3c36df308837 Mon Sep 17 00:00:00 2001 From: YEVHENII SHCHERBINA Date: Mon, 12 Jan 2026 17:11:57 +0000 Subject: [PATCH] feat: add --version flag --- cli/cli.go | 23 ++++++++++++++++++++--- cmd/boundary/main.go | 3 +-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 3a2a20bf..692486af 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -13,10 +13,15 @@ import ( "github.com/coder/serpent" ) +// printVersion prints version information. +func printVersion(version string) { + fmt.Println(version) +} + // NewCommand creates and returns the root serpent command -func NewCommand() *serpent.Command { +func NewCommand(version string) *serpent.Command { // To make the top level boundary command, we just make some minor changes to the base command - cmd := BaseCommand() + cmd := BaseCommand(version) cmd.Use = "boundary [flags] -- command [args...]" // Add the flags and args pieces to usage. // Add example usage to the long description. This is different from usage as a subcommand because it @@ -39,8 +44,9 @@ func NewCommand() *serpent.Command { // Base command returns the boundary serpent command without the information involved in making it the // *top level* serpent command. We are creating this split to make it easier to integrate into the coder // CLI if needed. -func BaseCommand() *serpent.Command { +func BaseCommand(version string) *serpent.Command { cliConfig := config.CliConfig{} + var showVersion serpent.Bool // Set default config path if file exists - serpent will load it automatically if home, err := os.UserHomeDir(); err == nil { @@ -149,8 +155,19 @@ func BaseCommand() *serpent.Command { Value: &cliConfig.LogProxySocketPath, YAML: "", // CLI only, not loaded from YAML }, + { + Flag: "version", + Description: "Print version information and exit.", + Value: &showVersion, + YAML: "", // CLI only + }, }, Handler: func(inv *serpent.Invocation) error { + // Handle --version flag early + if showVersion.Value() { + printVersion(version) + return nil + } appConfig, err := config.NewAppConfigFromCliConfig(cliConfig, inv.Args) if err != nil { return fmt.Errorf("failed to parse cli config file: %v", err) diff --git a/cmd/boundary/main.go b/cmd/boundary/main.go index 86eefa36..189679a6 100644 --- a/cmd/boundary/main.go +++ b/cmd/boundary/main.go @@ -9,12 +9,11 @@ import ( // Version information injected at build time var ( - //nolint:unused version = "dev" // Set via -ldflags "-X main.version=v1.0.0" ) func main() { - cmd := cli.NewCommand() + cmd := cli.NewCommand(version) err := cmd.Invoke().WithOS().Run() if err != nil {