Skip to content

Commit df17896

Browse files
committed
Validate properties of customCommand when commandMenu is used
1 parent 22512d5 commit df17896

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

pkg/config/user_config_validation.go

+17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ func validateCustomCommands(customCommands []CustomCommand) error {
9696
if err := validateCustomCommandKey(customCommand.Key); err != nil {
9797
return err
9898
}
99+
100+
if len(customCommand.CommandMenu) > 0 &&
101+
(len(customCommand.Context) > 0 ||
102+
len(customCommand.Command) > 0 ||
103+
customCommand.Subprocess != nil ||
104+
len(customCommand.Prompts) > 0 ||
105+
len(customCommand.LoadingText) > 0 ||
106+
customCommand.Stream != nil ||
107+
customCommand.ShowOutput != nil ||
108+
len(customCommand.OutputTitle) > 0 ||
109+
customCommand.After != nil) {
110+
commandRef := ""
111+
if len(customCommand.Key) > 0 {
112+
commandRef = fmt.Sprintf(" with key '%s'", customCommand.Key)
113+
}
114+
return fmt.Errorf("Error with custom command%s: it is not allowed to use both commandMenu and any of the other fields except key and description.", commandRef)
115+
}
99116
}
100117
return nil
101118
}

pkg/config/user_config_validation_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,58 @@ func TestUserConfigValidate_enums(t *testing.T) {
7474
{value: "invalid_value", valid: false},
7575
},
7676
},
77+
{
78+
name: "Custom command sub menu",
79+
setup: func(config *UserConfig, _ string) {
80+
config.CustomCommands = []CustomCommand{
81+
{
82+
Key: "X",
83+
Description: "My Custom Commands",
84+
CommandMenu: []CustomCommand{
85+
{Key: "1", Command: "echo 'hello'", Context: "global"},
86+
},
87+
},
88+
}
89+
},
90+
testCases: []testCase{
91+
{value: "", valid: true},
92+
},
93+
},
94+
{
95+
name: "Custom command sub menu",
96+
setup: func(config *UserConfig, _ string) {
97+
config.CustomCommands = []CustomCommand{
98+
{
99+
Key: "X",
100+
Context: "global",
101+
CommandMenu: []CustomCommand{
102+
{Key: "1", Command: "echo 'hello'", Context: "global"},
103+
},
104+
},
105+
}
106+
},
107+
testCases: []testCase{
108+
{value: "", valid: false},
109+
},
110+
},
111+
{
112+
name: "Custom command sub menu",
113+
setup: func(config *UserConfig, _ string) {
114+
falseVal := false
115+
config.CustomCommands = []CustomCommand{
116+
{
117+
Key: "X",
118+
Subprocess: &falseVal,
119+
CommandMenu: []CustomCommand{
120+
{Key: "1", Command: "echo 'hello'", Context: "global"},
121+
},
122+
},
123+
}
124+
},
125+
testCases: []testCase{
126+
{value: "", valid: false},
127+
},
128+
},
77129
}
78130

79131
for _, s := range scenarios {

0 commit comments

Comments
 (0)