Skip to content

Commit b43569a

Browse files
authored
feat: add support for --help and version commands in aks-node-controller (#8313)
1 parent f399ec8 commit b43569a

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

aks-node-controller/app.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ type ProvisionStatusFiles struct {
9797
}
9898

9999
func (a *App) Run(ctx context.Context, args []string) int {
100+
if handled := handleInfoCommand(args); handled {
101+
return 0
102+
}
103+
100104
slog.Info("aks-node-controller started", "args", args)
101105
err := a.run(ctx, args)
102106
exitCode := errToExitCode(err)
@@ -108,6 +112,30 @@ func (a *App) Run(ctx context.Context, args []string) int {
108112
return exitCode
109113
}
110114

115+
// handleInfoCommand handles --version, version, --help, and help commands
116+
// without logging noise. Returns true if handled.
117+
func handleInfoCommand(args []string) bool {
118+
if len(args) < 2 {
119+
return false
120+
}
121+
switch args[1] {
122+
case "--version", "version":
123+
_, _ = fmt.Fprintln(os.Stdout, Version)
124+
return true
125+
case "--help", "help":
126+
_, _ = fmt.Fprintln(os.Stdout, "Usage: aks-node-controller <command> [options]")
127+
_, _ = fmt.Fprintln(os.Stdout)
128+
_, _ = fmt.Fprintln(os.Stdout, "Commands:")
129+
_, _ = fmt.Fprintln(os.Stdout, " provision Run node provisioning")
130+
_, _ = fmt.Fprintln(os.Stdout, " provision-wait Wait for provisioning to complete")
131+
_, _ = fmt.Fprintln(os.Stdout, " version Print the version")
132+
_, _ = fmt.Fprintln(os.Stdout, " help Print this help message")
133+
return true
134+
default:
135+
return false
136+
}
137+
}
138+
111139
func (a *App) run(ctx context.Context, args []string) error {
112140
command := ""
113141
if len(args) >= 2 {
@@ -117,12 +145,6 @@ func (a *App) run(ctx context.Context, args []string) error {
117145
return errors.New("missing command argument")
118146
}
119147

120-
if command == "--version" || command == "version" {
121-
//nolint:forbidigo // stdout is part of the interface
122-
fmt.Println(Version)
123-
return nil
124-
}
125-
126148
cmd, ok := getCommandRegistry()[command]
127149
if !ok {
128150
return fmt.Errorf("unknown command: %s", command)

aks-node-controller/app_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ func TestApp_Run(t *testing.T) {
7878
assert.Equal(t, 0, exitCode)
7979
})
8080

81+
t.Run("--help flag returns success exit code", func(t *testing.T) {
82+
tt := NewTestApp(t, TestAppConfig{})
83+
exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "--help"})
84+
assert.Equal(t, 0, exitCode)
85+
})
86+
87+
t.Run("help command returns success exit code", func(t *testing.T) {
88+
tt := NewTestApp(t, TestAppConfig{})
89+
exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "help"})
90+
assert.Equal(t, 0, exitCode)
91+
})
92+
8193
t.Run("provision command with missing flag", func(t *testing.T) {
8294
tt := NewTestApp(t, TestAppConfig{})
8395
exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "provision"})

0 commit comments

Comments
 (0)