Skip to content

Commit 69f7ebb

Browse files
feat: add --version flag (#154)
1 parent 33bb69b commit 69f7ebb

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

cli/cli.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@ import (
1313
"github.com/coder/serpent"
1414
)
1515

16+
// printVersion prints version information.
17+
func printVersion(version string) {
18+
fmt.Println(version)
19+
}
20+
1621
// NewCommand creates and returns the root serpent command
17-
func NewCommand() *serpent.Command {
22+
func NewCommand(version string) *serpent.Command {
1823
// To make the top level boundary command, we just make some minor changes to the base command
19-
cmd := BaseCommand()
24+
cmd := BaseCommand(version)
2025
cmd.Use = "boundary [flags] -- command [args...]" // Add the flags and args pieces to usage.
2126

2227
// 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 {
3944
// Base command returns the boundary serpent command without the information involved in making it the
4045
// *top level* serpent command. We are creating this split to make it easier to integrate into the coder
4146
// CLI if needed.
42-
func BaseCommand() *serpent.Command {
47+
func BaseCommand(version string) *serpent.Command {
4348
cliConfig := config.CliConfig{}
49+
var showVersion serpent.Bool
4450

4551
// Set default config path if file exists - serpent will load it automatically
4652
if home, err := os.UserHomeDir(); err == nil {
@@ -149,8 +155,19 @@ func BaseCommand() *serpent.Command {
149155
Value: &cliConfig.LogProxySocketPath,
150156
YAML: "", // CLI only, not loaded from YAML
151157
},
158+
{
159+
Flag: "version",
160+
Description: "Print version information and exit.",
161+
Value: &showVersion,
162+
YAML: "", // CLI only
163+
},
152164
},
153165
Handler: func(inv *serpent.Invocation) error {
166+
// Handle --version flag early
167+
if showVersion.Value() {
168+
printVersion(version)
169+
return nil
170+
}
154171
appConfig, err := config.NewAppConfigFromCliConfig(cliConfig, inv.Args)
155172
if err != nil {
156173
return fmt.Errorf("failed to parse cli config file: %v", err)

cmd/boundary/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import (
99

1010
// Version information injected at build time
1111
var (
12-
//nolint:unused
1312
version = "dev" // Set via -ldflags "-X main.version=v1.0.0"
1413
)
1514

1615
func main() {
17-
cmd := cli.NewCommand()
16+
cmd := cli.NewCommand(version)
1817

1918
err := cmd.Invoke().WithOS().Run()
2019
if err != nil {

0 commit comments

Comments
 (0)