From df17896de51616e5ef93af29d45de2d675da9388 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 23 Feb 2025 18:19:51 +0100 Subject: [PATCH] Validate properties of customCommand when commandMenu is used --- pkg/config/user_config_validation.go | 17 ++++++++ pkg/config/user_config_validation_test.go | 52 +++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/pkg/config/user_config_validation.go b/pkg/config/user_config_validation.go index 735c5aad2..2be2f04e9 100644 --- a/pkg/config/user_config_validation.go +++ b/pkg/config/user_config_validation.go @@ -96,6 +96,23 @@ func validateCustomCommands(customCommands []CustomCommand) error { if err := validateCustomCommandKey(customCommand.Key); err != nil { return err } + + if len(customCommand.CommandMenu) > 0 && + (len(customCommand.Context) > 0 || + len(customCommand.Command) > 0 || + customCommand.Subprocess != nil || + len(customCommand.Prompts) > 0 || + len(customCommand.LoadingText) > 0 || + customCommand.Stream != nil || + customCommand.ShowOutput != nil || + len(customCommand.OutputTitle) > 0 || + customCommand.After != nil) { + commandRef := "" + if len(customCommand.Key) > 0 { + commandRef = fmt.Sprintf(" with key '%s'", customCommand.Key) + } + 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) + } } return nil } diff --git a/pkg/config/user_config_validation_test.go b/pkg/config/user_config_validation_test.go index 1d8428019..c02353ea1 100644 --- a/pkg/config/user_config_validation_test.go +++ b/pkg/config/user_config_validation_test.go @@ -74,6 +74,58 @@ func TestUserConfigValidate_enums(t *testing.T) { {value: "invalid_value", valid: false}, }, }, + { + name: "Custom command sub menu", + setup: func(config *UserConfig, _ string) { + config.CustomCommands = []CustomCommand{ + { + Key: "X", + Description: "My Custom Commands", + CommandMenu: []CustomCommand{ + {Key: "1", Command: "echo 'hello'", Context: "global"}, + }, + }, + } + }, + testCases: []testCase{ + {value: "", valid: true}, + }, + }, + { + name: "Custom command sub menu", + setup: func(config *UserConfig, _ string) { + config.CustomCommands = []CustomCommand{ + { + Key: "X", + Context: "global", + CommandMenu: []CustomCommand{ + {Key: "1", Command: "echo 'hello'", Context: "global"}, + }, + }, + } + }, + testCases: []testCase{ + {value: "", valid: false}, + }, + }, + { + name: "Custom command sub menu", + setup: func(config *UserConfig, _ string) { + falseVal := false + config.CustomCommands = []CustomCommand{ + { + Key: "X", + Subprocess: &falseVal, + CommandMenu: []CustomCommand{ + {Key: "1", Command: "echo 'hello'", Context: "global"}, + }, + }, + } + }, + testCases: []testCase{ + {value: "", valid: false}, + }, + }, } for _, s := range scenarios {