From 5979b40546456b89b745f59edbec8f371364dea7 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 16 Feb 2025 16:19:15 +0100 Subject: [PATCH] Validate keys of custom commands --- pkg/config/user_config_validation.go | 20 ++++++++++++++++++++ pkg/config/user_config_validation_test.go | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/pkg/config/user_config_validation.go b/pkg/config/user_config_validation.go index 83b2a7c5a..735c5aad2 100644 --- a/pkg/config/user_config_validation.go +++ b/pkg/config/user_config_validation.go @@ -22,6 +22,9 @@ func (config *UserConfig) Validate() error { if err := validateKeybindings(config.Keybinding); err != nil { return err } + if err := validateCustomCommands(config.CustomCommands); err != nil { + return err + } return nil } @@ -79,3 +82,20 @@ func validateKeybindings(keybindingConfig KeybindingConfig) error { return nil } + +func validateCustomCommandKey(key string) error { + if !isValidKeybindingKey(key) { + return fmt.Errorf("Unrecognized key '%s' for custom command. For permitted values see %s", + key, constants.Links.Docs.CustomKeybindings) + } + return nil +} + +func validateCustomCommands(customCommands []CustomCommand) error { + for _, customCommand := range customCommands { + if err := validateCustomCommandKey(customCommand.Key); err != nil { + return err + } + } + return nil +} diff --git a/pkg/config/user_config_validation_test.go b/pkg/config/user_config_validation_test.go index 8069b13be..1d8428019 100644 --- a/pkg/config/user_config_validation_test.go +++ b/pkg/config/user_config_validation_test.go @@ -56,6 +56,24 @@ func TestUserConfigValidate_enums(t *testing.T) { {value: "1,2,3,4,5,6", valid: false}, }, }, + { + name: "Custom command keybinding", + setup: func(config *UserConfig, value string) { + config.CustomCommands = []CustomCommand{ + { + Key: value, + Command: "echo 'hello'", + }, + } + }, + testCases: []testCase{ + {value: "", valid: true}, + {value: "", valid: true}, + {value: "q", valid: true}, + {value: "", valid: true}, + {value: "invalid_value", valid: false}, + }, + }, } for _, s := range scenarios {