diff options
author | Ash McKenzie <amckenzie@gitlab.com> | 2019-08-05 05:03:16 +0000 |
---|---|---|
committer | Ash McKenzie <amckenzie@gitlab.com> | 2019-08-05 05:03:16 +0000 |
commit | c577eb9ed8bd0336870f7a83302f70821d510169 (patch) | |
tree | ed7f7281633d97933e4465a2ac0f86d62c9a216e /go/internal/command/commandargs/command_args.go | |
parent | ed0460374a5ca13d9ea17c6a9c21151319b7fd53 (diff) | |
parent | 3b6f9f7583755e041e76142d7caf7716937907fa (diff) | |
download | gitlab-shell-c577eb9ed8bd0336870f7a83302f70821d510169.tar.gz |
Merge branch '181-migrate-gitlab-shell-checks-fallback' into 'master'
Support falling back to ruby version of checkers
See merge request gitlab-org/gitlab-shell!318
Diffstat (limited to 'go/internal/command/commandargs/command_args.go')
-rw-r--r-- | go/internal/command/commandargs/command_args.go | 108 |
1 files changed, 12 insertions, 96 deletions
diff --git a/go/internal/command/commandargs/command_args.go b/go/internal/command/commandargs/command_args.go index d8fe32d..5338d6b 100644 --- a/go/internal/command/commandargs/command_args.go +++ b/go/internal/command/commandargs/command_args.go @@ -1,111 +1,27 @@ package commandargs import ( - "errors" - "os" - "regexp" - - "github.com/mattn/go-shellwords" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/executable" ) type CommandType string -const ( - Discover CommandType = "discover" - TwoFactorRecover CommandType = "2fa_recovery_codes" - LfsAuthenticate CommandType = "git-lfs-authenticate" - ReceivePack CommandType = "git-receive-pack" - UploadPack CommandType = "git-upload-pack" - UploadArchive CommandType = "git-upload-archive" -) - -var ( - whoKeyRegex = regexp.MustCompile(`\bkey-(?P<keyid>\d+)\b`) - whoUsernameRegex = regexp.MustCompile(`\busername-(?P<username>\S+)\b`) -) - -type CommandArgs struct { - GitlabUsername string - GitlabKeyId string - SshArgs []string - CommandType CommandType -} - -func Parse(arguments []string) (*CommandArgs, error) { - if sshConnection := os.Getenv("SSH_CONNECTION"); sshConnection == "" { - return nil, errors.New("Only ssh allowed") - } - - args := &CommandArgs{} - args.parseWho(arguments) - - if err := args.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND")); err != nil { - return nil, errors.New("Invalid ssh command") - } - args.defineCommandType() - - return args, nil -} - -func (c *CommandArgs) parseWho(arguments []string) { - for _, argument := range arguments { - if keyId := tryParseKeyId(argument); keyId != "" { - c.GitlabKeyId = keyId - break - } - - if username := tryParseUsername(argument); username != "" { - c.GitlabUsername = username - break - } - } +type CommandArgs interface { + Parse() error + GetArguments() []string } -func tryParseKeyId(argument string) string { - matchInfo := whoKeyRegex.FindStringSubmatch(argument) - if len(matchInfo) == 2 { - // The first element is the full matched string - // The second element is the named `keyid` - return matchInfo[1] - } - - return "" -} +func Parse(e *executable.Executable, arguments []string) (CommandArgs, error) { + var args CommandArgs = &GenericArgs{Arguments: arguments} -func tryParseUsername(argument string) string { - matchInfo := whoUsernameRegex.FindStringSubmatch(argument) - if len(matchInfo) == 2 { - // The first element is the full matched string - // The second element is the named `username` - return matchInfo[1] + switch e.Name { + case executable.GitlabShell: + args = &Shell{Arguments: arguments} } - return "" -} - -func (c *CommandArgs) parseCommand(commandString string) error { - args, err := shellwords.Parse(commandString) - if err != nil { - return err + if err := args.Parse(); err != nil { + return nil, err } - // Handle Git for Windows 2.14 using "git upload-pack" instead of git-upload-pack - if len(args) > 1 && args[0] == "git" { - command := args[0] + "-" + args[1] - commandArgs := args[2:] - - args = append([]string{command}, commandArgs...) - } - - c.SshArgs = args - - return nil -} - -func (c *CommandArgs) defineCommandType() { - if len(c.SshArgs) == 0 { - c.CommandType = Discover - } else { - c.CommandType = CommandType(c.SshArgs[0]) - } + return args, nil } |