Skip to content

Commit fc73ce7

Browse files
authored
feat: add --version flag to root command (#15)
* feat: add --version flag to root command Why: Users expect --version flag as standard CLI pattern (issue #9) Approach: Set rootCmd.Version in createRootCmd, add functional tests Learned: Cobra auto-adds -v/--version when Version field is set Next: Create PR and close issue Changes: - Added rootCmd.Version = config.Version in createRootCmd() - Added functional tests for both 'version' subcommand and '--version' flag - Tests verify output format and exit code - All tests pass Fixes #9 🤖 Claude <[email protected]> * test: add proper version output validation with CheckFuncs Why: Need to validate version output format supports both 'unknown' and version strings Approach: Use CheckFuncs to validate output format while stripping variable content Learned: CheckFunc provides better validation than regex for variable content Next: Push and verify CI passes Changes: - Added strings import to data_for_test.go - Added CheckFunc for version subcommand to validate format - Added CheckFunc for --version flag to validate 'otel-cli version X' format - Both CheckFuncs handle 'unknown' or multi-part version strings - Use regex to strip content for checkOutput comparison 🤖 Claude <[email protected]>
1 parent 6d810ec commit fc73ce7

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

data_for_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package main_test
1010
import (
1111
"os"
1212
"regexp"
13+
"strings"
1314
"syscall"
1415
"testing"
1516
"time"
@@ -1373,4 +1374,64 @@ var suites = []FixtureSuite{
13731374
},
13741375
},
13751376
},
1377+
// version flag tests
1378+
{
1379+
{
1380+
Name: "version subcommand",
1381+
Config: FixtureConfig{
1382+
CliArgs: []string{"version"},
1383+
},
1384+
Expect: Results{
1385+
CliOutputRe: regexp.MustCompile(`.+`), // match and strip any content
1386+
CliOutput: "\n", // after strip, should be just newline
1387+
ExitCode: 0,
1388+
},
1389+
CheckFuncs: []CheckFunc{
1390+
func(t *testing.T, f Fixture, r Results) {
1391+
// version subcommand outputs just the version string
1392+
// should be "unknown" or "version commit date"
1393+
output := strings.TrimSpace(r.CliOutput)
1394+
if output == "" {
1395+
t.Errorf("version subcommand produced no output")
1396+
return
1397+
}
1398+
// valid outputs: "unknown" or multi-part version like "0.6.0 abc123 2025-11-09"
1399+
if output != "unknown" && !regexp.MustCompile(`^\S+`).MatchString(output) {
1400+
t.Errorf("version subcommand output format unexpected: %q", output)
1401+
}
1402+
},
1403+
},
1404+
},
1405+
{
1406+
Name: "--version flag",
1407+
Config: FixtureConfig{
1408+
CliArgs: []string{"--version"},
1409+
},
1410+
Expect: Results{
1411+
CliOutputRe: regexp.MustCompile(`.+`), // match and strip any content
1412+
CliOutput: "\n", // after strip, should be just newline
1413+
ExitCode: 0,
1414+
},
1415+
CheckFuncs: []CheckFunc{
1416+
func(t *testing.T, f Fixture, r Results) {
1417+
// --version flag outputs "otel-cli version <version>"
1418+
output := strings.TrimSpace(r.CliOutput)
1419+
if !strings.HasPrefix(output, "otel-cli version ") {
1420+
t.Errorf("--version flag should start with 'otel-cli version ', got: %q", output)
1421+
return
1422+
}
1423+
// extract version part after "otel-cli version "
1424+
version := strings.TrimPrefix(output, "otel-cli version ")
1425+
if version == "" {
1426+
t.Errorf("--version flag has no version content after prefix")
1427+
return
1428+
}
1429+
// valid version: "unknown" or multi-part like "0.6.0 abc123 2025-11-09"
1430+
if version != "unknown" && !regexp.MustCompile(`^\S+`).MatchString(version) {
1431+
t.Errorf("--version flag version format unexpected: %q", version)
1432+
}
1433+
},
1434+
},
1435+
},
1436+
},
13761437
}

otelcli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func createRootCmd(config *Config) *cobra.Command {
5959

6060
cobra.EnableCommandSorting = false
6161
rootCmd.Flags().SortFlags = false
62+
rootCmd.Version = config.Version
6263

6364
Diag.NumArgs = len(os.Args) - 1
6465
Diag.CliArgs = []string{}

0 commit comments

Comments
 (0)