diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2019-09-19 20:02:49 +0300 |
---|---|---|
committer | Jacob Vosmaer <jacob@gitlab.com> | 2019-09-19 20:02:49 +0300 |
commit | e6df3d9ff81a70946827edc4ca7c2acf87804901 (patch) | |
tree | a7538c0e82f8d7d5248c862b2f61170ef33b2ffd | |
parent | b3731e92896b3241a9e2dad174c488d0f78a6b6d (diff) | |
parent | f9650efb1ef2ca47272e0db3af5d54074e2cece6 (diff) |
Merge branch 'po-dsl-pos-arg-option' into 'master'
Nested command for DSL
See merge request gitlab-org/gitaly!1498
-rw-r--r-- | changelogs/unreleased/po-dsl-pos-arg-option.yml | 5 | ||||
-rw-r--r-- | internal/git/safecmd.go | 20 | ||||
-rw-r--r-- | internal/git/safecmd_test.go | 24 | ||||
-rw-r--r-- | internal/git/staticargs.go | 19 |
4 files changed, 68 insertions, 0 deletions
diff --git a/changelogs/unreleased/po-dsl-pos-arg-option.yml b/changelogs/unreleased/po-dsl-pos-arg-option.yml new file mode 100644 index 000000000..03c515879 --- /dev/null +++ b/changelogs/unreleased/po-dsl-pos-arg-option.yml @@ -0,0 +1,5 @@ +--- +title: Nested command for DSL +merge_request: 1498 +author: +type: other diff --git a/internal/git/safecmd.go b/internal/git/safecmd.go index 352ee2ffe..fa9a437eb 100644 --- a/internal/git/safecmd.go +++ b/internal/git/safecmd.go @@ -80,6 +80,26 @@ type Option interface { ValidateArgs() ([]string, error) } +// SubSubCmd is a positional argument that appears in the list of options for +// a subcommand. +type SubSubCmd struct { + Name string +} + +// IsOption is a method present on all Flag interface implementations +func (SubSubCmd) IsOption() {} + +// ValidateArgs returns an error if the command name or options are not +// sanitary +func (sc SubSubCmd) ValidateArgs() ([]string, error) { + if !subCmdNameRegex.MatchString(sc.Name) { + return nil, &invalidArgErr{ + msg: fmt.Sprintf("invalid sub-sub command name %q", sc.Name), + } + } + return []string{sc.Name}, 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 e646c7d4d..1be8fbeb5 100644 --- a/internal/git/safecmd_test.go +++ b/internal/git/safecmd_test.go @@ -29,6 +29,9 @@ func TestFlagValidation(t *testing.T) { {option: git.ValueFlag{"-k", "--anything"}, valid: true}, {option: git.ValueFlag{"-k", ""}, valid: true}, + // valid SubSubCmd inputs + {option: git.SubSubCmd{"meow"}, valid: true}, + // valid FlagCombo inputs // invalid Flag inputs @@ -40,6 +43,9 @@ func TestFlagValidation(t *testing.T) { // invalid ValueFlag inputs {option: git.ValueFlag{"k", "asdf"}}, // missing dash + + // invalid SubSubCmd inputs + {option: git.SubSubCmd{"--meow"}}, // cannot start with dash } { args, err := tt.option.ValidateArgs() if tt.valid { @@ -76,6 +82,13 @@ func TestSafeCmdInvalidArg(t *testing.T) { }, errMsg: "positional arg \"--tweet\" cannot start with dash '-'", }, + { + subCmd: git.SubCmd{ + Name: "meow", + Flags: []git.Option{git.SubSubCmd{"-invalid"}}, + }, + errMsg: "invalid sub-sub command name \"-invalid\"", + }, } { _, err := git.SafeCmd( context.Background(), @@ -136,6 +149,17 @@ func TestSafeCmdValid(t *testing.T) { }, expectArgs: []string{"-a", "-b", "c", "d", "-e", "-f", "g", "-h=i", "1", "2", "--", "3", "4", "5"}, }, + { + subCmd: git.SubCmd{ + Name: "noun", + Flags: []git.Option{ + git.SubSubCmd{"verb"}, + git.OutputToStdout, + git.Flag{"--adjective"}, + }, + }, + expectArgs: []string{"noun", "verb", "-", "--adjective"}, + }, } { cmd, err := git.SafeCmd(ctx, testRepo, tt.globals, tt.subCmd) require.NoError(t, err) diff --git a/internal/git/staticargs.go b/internal/git/staticargs.go new file mode 100644 index 000000000..7caaf9475 --- /dev/null +++ b/internal/git/staticargs.go @@ -0,0 +1,19 @@ +package git + +// StaticOption are reusable trusted options +type StaticOption struct { + value string +} + +// IsOption is a method present on all Flag interface implementations +func (sa StaticOption) IsOption() {} + +// ValidateArgs just passes through the already trusted value. This never +// returns an error. +func (sa StaticOption) ValidateArgs() ([]string, error) { return []string{sa.value}, nil } + +var ( + // OutputToStdout is used indicate the output should be sent to STDOUT + // Seen in: git bundle create + OutputToStdout = StaticOption{value: "-"} +) |