diff options
author | Jaime Martinez <jmartinez@gitlab.com> | 2020-04-22 04:03:13 +0300 |
---|---|---|
committer | Vladimir Shushlin <v.shushlin@gmail.com> | 2020-05-08 15:06:07 +0300 |
commit | b9c69dc5390d15e114d0771d2c217a4be80693b2 (patch) | |
tree | f73a588d02c912ad4826a82d3dbfa595fddacba4 /internal/validateargs | |
parent | cf03e89ed1b63f763dab88b60d6e9148e2f70b19 (diff) |
Consolidate args validation
Diffstat (limited to 'internal/validateargs')
-rw-r--r-- | internal/validateargs/validateargs.go | 35 | ||||
-rw-r--r-- | internal/validateargs/validateargs_test.go | 5 |
2 files changed, 20 insertions, 20 deletions
diff --git a/internal/validateargs/validateargs.go b/internal/validateargs/validateargs.go index 263d3c52..3b75b69b 100644 --- a/internal/validateargs/validateargs.go +++ b/internal/validateargs/validateargs.go @@ -5,39 +5,36 @@ import ( "strings" ) +const ( + deprecatedMessage = "command line options have been deprecated:" + notAllowedMsg = "invalid command line arguments:" +) + var deprecatedArgs = []string{"-sentry-dsn"} var notAllowedArgs = []string{"-auth-client-id", "-auth-client-secret", "-auth-secret"} // Deprecated checks if deprecated params have been used func Deprecated(args []string) error { - var foundDeprecatedArgs []string - - argsStr := strings.Join(args, " ") - for _, deprecatedArg := range deprecatedArgs { - if strings.Contains(argsStr, deprecatedArg) { - foundDeprecatedArgs = append(foundDeprecatedArgs, deprecatedArg) - } - } - - if len(foundDeprecatedArgs) > 0 { - return fmt.Errorf("deprecation message: %s should not be passed as a command line arguments", strings.Join(foundDeprecatedArgs, ", ")) - } - return nil + return validate(args, deprecatedArgs, deprecatedMessage) } // NotAllowed checks if explicitly not allowed params have been used func NotAllowed(args []string) error { - var foundNotAllowedArgs []string + return validate(args, notAllowedArgs, notAllowedMsg) +} + +func validate(args, invalidArgs []string, errMsg string) error { + var foundInvalidArgs []string argsStr := strings.Join(args, " ") - for _, notAllowedArg := range notAllowedArgs { - if strings.Contains(argsStr, notAllowedArg) { - foundNotAllowedArgs = append(foundNotAllowedArgs, notAllowedArg) + for _, invalidArg := range invalidArgs { + if strings.Contains(argsStr, invalidArg) { + foundInvalidArgs = append(foundInvalidArgs, invalidArg) } } - if len(foundNotAllowedArgs) > 0 { - return fmt.Errorf("%s should not be passed as a command line arguments", strings.Join(foundNotAllowedArgs, ", ")) + if len(foundInvalidArgs) > 0 { + return fmt.Errorf("%s %s", errMsg, strings.Join(foundInvalidArgs, ", ")) } return nil diff --git a/internal/validateargs/validateargs_test.go b/internal/validateargs/validateargs_test.go index 02f2f2ef..4ec5cd89 100644 --- a/internal/validateargs/validateargs_test.go +++ b/internal/validateargs/validateargs_test.go @@ -17,13 +17,15 @@ func TestValidParams(t *testing.T) { func TestInvalidDeprecatedParms(t *testing.T) { tests := map[string][]string{ - "Sentry DSN passed": []string{"gitlab-pages", "-sentry-dsn", "abc123"}, + "Sentry DSN passed": []string{"gitlab-pages", "-sentry-dsn", "abc123"}, + "Sentry DSN using key=value": []string{"gitlab-pages", "-sentry-dsn=abc123"}, } for name, args := range tests { t.Run(name, func(t *testing.T) { err := Deprecated(args) require.Error(t, err) + require.Contains(t, err.Error(), deprecatedMessage) }) } } @@ -42,6 +44,7 @@ func TestInvalidNotAllowedParams(t *testing.T) { t.Run(name, func(t *testing.T) { err := NotAllowed(args) require.Error(t, err) + require.Contains(t, err.Error(), notAllowedMsg) }) } } |