diff options
Diffstat (limited to 'internal/git')
-rw-r--r-- | internal/git/safecmd.go | 21 | ||||
-rw-r--r-- | internal/git/safecmd_test.go | 24 |
2 files changed, 44 insertions, 1 deletions
diff --git a/internal/git/safecmd.go b/internal/git/safecmd.go index fa9a437eb..43d7ed037 100644 --- a/internal/git/safecmd.go +++ b/internal/git/safecmd.go @@ -100,6 +100,27 @@ func (sc SubSubCmd) ValidateArgs() ([]string, error) { return []string{sc.Name}, nil } +// ConfigPair is a sub-command option for use with commands like "git config" +type ConfigPair struct { + Key string + Value string +} + +// IsOption is a method present on all Flag interface implementations +func (ConfigPair) IsOption() {} + +var configKeyRegex = regexp.MustCompile(`^[[:alnum:]]+[-[:alnum:]]*\.(.+\.)*[[:alnum:]]+[-[:alnum:]]*$`) + +// ValidateArgs validates the config pair args +func (cp ConfigPair) ValidateArgs() ([]string, error) { + if !configKeyRegex.MatchString(cp.Key) { + return nil, &invalidArgErr{ + msg: fmt.Sprintf("config key %q failed regexp validation", cp.Key), + } + } + return []string{cp.Key, cp.Value}, nil +} + // Flag is a single token optional command line argument that enables or // disables functionality (e.g. "-L") type Flag struct { diff --git a/internal/git/safecmd_test.go b/internal/git/safecmd_test.go index 1be8fbeb5..4d8689cc5 100644 --- a/internal/git/safecmd_test.go +++ b/internal/git/safecmd_test.go @@ -32,7 +32,11 @@ func TestFlagValidation(t *testing.T) { // valid SubSubCmd inputs {option: git.SubSubCmd{"meow"}, valid: true}, - // valid FlagCombo inputs + // valid ConfigPair inputs + {option: git.ConfigPair{"a.b.c", "d"}, valid: true}, + {option: git.ConfigPair{"core.sound", "meow"}, valid: true}, + {option: git.ConfigPair{"asdf-qwer.1234-5678", ""}, valid: true}, + {option: git.ConfigPair{"http.https://user@example.com/repo.git.user", "kitty"}, valid: true}, // invalid Flag inputs {option: git.Flag{"-*"}}, // invalid character @@ -46,6 +50,15 @@ func TestFlagValidation(t *testing.T) { // invalid SubSubCmd inputs {option: git.SubSubCmd{"--meow"}}, // cannot start with dash + + // invalid ConfigPair inputs + {option: git.ConfigPair{"", ""}}, // key cannot be empty + {option: git.ConfigPair{" ", ""}}, // key cannot be whitespace + {option: git.ConfigPair{"asdf", ""}}, // two components required + {option: git.ConfigPair{"asdf.", ""}}, // 2nd component must be non-empty + {option: git.ConfigPair{"--asdf.asdf", ""}}, // key cannot start with dash + {option: git.ConfigPair{"as[[df.asdf", ""}}, // 1st component cannot contain non-alphanumeric + {option: git.ConfigPair{"asdf.as]]df", ""}}, // 2nd component cannot contain non-alphanumeric } { args, err := tt.option.ValidateArgs() if tt.valid { @@ -160,6 +173,15 @@ func TestSafeCmdValid(t *testing.T) { }, expectArgs: []string{"noun", "verb", "-", "--adjective"}, }, + { + subCmd: git.SubCmd{ + Name: "config", + Flags: []git.Option{ + git.ConfigPair{"user.name", "jramsay"}, + }, + }, + expectArgs: []string{"config", "user.name", "jramsay"}, + }, } { cmd, err := git.SafeCmd(ctx, testRepo, tt.globals, tt.subCmd) require.NoError(t, err) |