Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions aks-node-controller/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ type ProvisionStatusFiles struct {
}

func (a *App) Run(ctx context.Context, args []string) int {
if handled := handleInfoCommand(args); handled {
return 0
}

slog.Info("aks-node-controller started", "args", args)
err := a.run(ctx, args)
exitCode := errToExitCode(err)
Expand All @@ -108,6 +112,30 @@ func (a *App) Run(ctx context.Context, args []string) int {
return exitCode
}

// handleInfoCommand handles --version, version, --help, and help commands
// without logging noise. Returns true if handled.
func handleInfoCommand(args []string) bool {
if len(args) < 2 {
return false
}
switch args[1] {
case "--version", "version":
_, _ = fmt.Fprintln(os.Stdout, Version)
return true
case "--help", "help":
_, _ = fmt.Fprintln(os.Stdout, "Usage: aks-node-controller <command> [options]")
_, _ = fmt.Fprintln(os.Stdout)
_, _ = fmt.Fprintln(os.Stdout, "Commands:")
_, _ = fmt.Fprintln(os.Stdout, " provision Run node provisioning")
_, _ = fmt.Fprintln(os.Stdout, " provision-wait Wait for provisioning to complete")
_, _ = fmt.Fprintln(os.Stdout, " version Print the version")
_, _ = fmt.Fprintln(os.Stdout, " help Print this help message")
return true
default:
return false
}
}

func (a *App) run(ctx context.Context, args []string) error {
command := ""
if len(args) >= 2 {
Expand All @@ -117,12 +145,6 @@ func (a *App) run(ctx context.Context, args []string) error {
return errors.New("missing command argument")
}

if command == "--version" || command == "version" {
//nolint:forbidigo // stdout is part of the interface
fmt.Println(Version)
return nil
}

cmd, ok := getCommandRegistry()[command]
if !ok {
return fmt.Errorf("unknown command: %s", command)
Expand Down
12 changes: 12 additions & 0 deletions aks-node-controller/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ func TestApp_Run(t *testing.T) {
assert.Equal(t, 0, exitCode)
})

t.Run("--help flag returns success exit code", func(t *testing.T) {
tt := NewTestApp(t, TestAppConfig{})
exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "--help"})
assert.Equal(t, 0, exitCode)
})

t.Run("help command returns success exit code", func(t *testing.T) {
tt := NewTestApp(t, TestAppConfig{})
exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "help"})
assert.Equal(t, 0, exitCode)
})
Comment thread
Devinwong marked this conversation as resolved.

t.Run("provision command with missing flag", func(t *testing.T) {
tt := NewTestApp(t, TestAppConfig{})
exitCode := tt.App.Run(context.Background(), []string{"aks-node-controller", "provision"})
Expand Down
Loading