@@ -70,50 +70,66 @@ type Selector struct {
7070 PostRun PostRunFunc
7171}
7272
73- // ExcludeCmd creates a selector that rejects commands whose path contains any listed phrase.
74- // Example: ExcludeCmd("kubectl delete ", "admin ") excludes "kubectl delete " and "cli admin user ".
75- func ExcludeCmd ( cmds ... string ) CmdSelector {
73+ // AllowCmdsContaining creates a selector that only accepts commands whose path contains a listed phrase.
74+ // Example: AllowCmdsContaining("get ", "helm list ") includes "kubectl get pods " and "helm list ".
75+ func AllowCmdsContaining ( substrings ... string ) CmdSelector {
7676 return func (cmd * cobra.Command ) bool {
77- for _ , phrase := range cmds {
78- if strings .Contains (cmd .CommandPath (), phrase ) {
79- return false
77+ for _ , s := range substrings {
78+ if strings .Contains (cmd .CommandPath (), s ) {
79+ return true
8080 }
8181 }
8282
83- return true
83+ return false
8484 }
8585}
8686
87- // AllowCmd creates a selector that only accepts commands whose path contains a listed phrase.
88- // Example: AllowCmd("get ", "helm list ") includes "kubectl get pods " and "helm list ".
89- func AllowCmd ( cmds ... string ) CmdSelector {
87+ // ExcludeCmdsContaining creates a selector that rejects commands whose path contains any listed phrase.
88+ // Example: ExcludeCmdsContaining("kubectl delete ", "admin ") excludes "kubectl delete " and "cli admin user ".
89+ func ExcludeCmdsContaining ( substrings ... string ) CmdSelector {
9090 return func (cmd * cobra.Command ) bool {
91- for _ , phrase := range cmds {
92- if strings .Contains (cmd .CommandPath (), phrase ) {
93- return true
91+ for _ , s := range substrings {
92+ if strings .Contains (cmd .CommandPath (), s ) {
93+ return false
9494 }
9595 }
9696
97- return false
97+ return true
9898 }
9999}
100100
101- // ExcludeFlag creates a selector that rejects flags whose name is listed.
102- // Example: ExcludeFlag("color", "kubeconfig") excludes flags named "color" and "kubeconfig".
103- func ExcludeFlag (names ... string ) FlagSelector {
104- return func (flag * pflag.Flag ) bool {
105- return ! slices .Contains (names , flag .Name )
101+ // AllowCmds creates a selector that only accepts commands whose path is listed.
102+ // Example: AllowCmds("kubectl get", "helm list") includes only those exact commands.
103+ func AllowCmds (cmds ... string ) CmdSelector {
104+ return func (cmd * cobra.Command ) bool {
105+ return slices .Contains (cmds , cmd .CommandPath ())
106+ }
107+ }
108+
109+ // ExcludeCmds creates a selector that rejects commands whose path is listed.
110+ // Example: ExcludeCmds("kubectl delete", "helm uninstall") excludes those exact commands.
111+ func ExcludeCmds (cmds ... string ) CmdSelector {
112+ return func (cmd * cobra.Command ) bool {
113+ return ! slices .Contains (cmds , cmd .CommandPath ())
106114 }
107115}
108116
109- // AllowFlag creates a selector that only accepts flags whose name is listed.
110- // Example: AllowFlag ("namespace", "output") includes only flags named "namespace" and "output".
111- func AllowFlag (names ... string ) FlagSelector {
117+ // AllowFlags creates a selector that only accepts flags whose name is listed.
118+ // Example: AllowFlags ("namespace", "output") includes only flags named "namespace" and "output".
119+ func AllowFlags (names ... string ) FlagSelector {
112120 return func (flag * pflag.Flag ) bool {
113121 return slices .Contains (names , flag .Name )
114122 }
115123}
116124
125+ // ExcludeFlags creates a selector that rejects flags whose name is listed.
126+ // Example: ExcludeFlags("color", "kubeconfig") excludes flags named "color" and "kubeconfig".
127+ func ExcludeFlags (names ... string ) FlagSelector {
128+ return func (flag * pflag.Flag ) bool {
129+ return ! slices .Contains (names , flag .Name )
130+ }
131+ }
132+
117133// NoFlags is a FlagSelector that excludes all flags.
118134func NoFlags (_ * pflag.Flag ) bool {
119135 return false
0 commit comments