Skip to content

Commit a3caaca

Browse files
committed
kubectl-ate: skip the install wizard when flags are set
LocalNonPersistentFlags returns a new FlagSet whose NFlag is always 0, so install asked for its options even with --microvm or -f set. Check each flag's Changed instead. Signed-off-by: Eitan Yarmush <eitan.yarmush@solo.io>
1 parent 790eb06 commit a3caaca

2 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎cmd/kubectl-ate/internal/cmd/install.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,15 @@ func useTUI(cmd *cobra.Command) bool {
215215
// wizardWanted reports whether install should ask for its options: on a
216216
// terminal with no install flags set.
217217
func wizardWanted(cmd *cobra.Command) bool {
218-
return useTUI(cmd) && cmd.LocalNonPersistentFlags().NFlag() == 0
218+
return useTUI(cmd) && !localFlagSet(cmd)
219+
}
220+
221+
// localFlagSet reports whether any of cmd's own flags was set. The FlagSet
222+
// LocalNonPersistentFlags returns doesn't count set flags, so check each one.
223+
func localFlagSet(cmd *cobra.Command) bool {
224+
set := false
225+
cmd.LocalNonPersistentFlags().VisitAll(func(f *pflag.Flag) { set = set || f.Changed })
226+
return set
219227
}
220228

221229
func isTerminal(v any) bool {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2026 The Agent Substrate Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"testing"
19+
20+
"github.com/spf13/cobra"
21+
)
22+
23+
func TestLocalFlagSet(t *testing.T) {
24+
tests := []struct {
25+
name string
26+
args []string
27+
want bool
28+
}{
29+
{name: "none", args: nil, want: false},
30+
{name: "local", args: []string{"--microvm"}, want: true},
31+
{name: "persistent only", args: []string{"--context", "kind"}, want: false},
32+
}
33+
for _, tc := range tests {
34+
t.Run(tc.name, func(t *testing.T) {
35+
root := &cobra.Command{Use: "root"}
36+
root.PersistentFlags().String("context", "", "")
37+
var got bool
38+
sub := &cobra.Command{Use: "install", RunE: func(cmd *cobra.Command, _ []string) error {
39+
got = localFlagSet(cmd)
40+
return nil
41+
}}
42+
sub.Flags().Bool("microvm", false, "")
43+
root.AddCommand(sub)
44+
root.SetArgs(append([]string{"install"}, tc.args...))
45+
if err := root.Execute(); err != nil {
46+
t.Fatal(err)
47+
}
48+
if got != tc.want {
49+
t.Errorf("localFlagSet() = %v, want %v", got, tc.want)
50+
}
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)