summaryrefslogtreecommitdiff
path: root/go/internal/command/commandargs/command_args.go
diff options
context:
space:
mode:
authorPatrick Bajao <ebajao@gitlab.com>2019-07-29 15:56:00 +0800
committerPatrick Bajao <ebajao@gitlab.com>2019-07-31 12:03:37 +0800
commit3b0176df497263323da2fae793a79b568502e6db (patch)
treef4ff3e232bc3717c539d73a4f0460d6d12b4a6de /go/internal/command/commandargs/command_args.go
parentaab85f3600caf04b491d6ca4fc3f0f004d9e3fc0 (diff)
downloadgitlab-shell-3b0176df497263323da2fae793a79b568502e6db.tar.gz
Support different CommandArgs type
`CommandArgs` has been renamed to `Shell`. An interface has been added that includes `Executable()` and `Arguments()` method. The `BaseArgs` implement this methods and should be embeeded in each type.
Diffstat (limited to 'go/internal/command/commandargs/command_args.go')
-rw-r--r--go/internal/command/commandargs/command_args.go125
1 files changed, 13 insertions, 112 deletions
diff --git a/go/internal/command/commandargs/command_args.go b/go/internal/command/commandargs/command_args.go
index d68f5d2..9f28817 100644
--- a/go/internal/command/commandargs/command_args.go
+++ b/go/internal/command/commandargs/command_args.go
@@ -1,128 +1,29 @@
package commandargs
-import (
- "errors"
- "os"
- "path/filepath"
- "regexp"
-
- "github.com/mattn/go-shellwords"
-)
-
type CommandType string
type Executable 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"
- GitlabShell Executable = "gitlab-shell"
-)
-
-var (
- whoKeyRegex = regexp.MustCompile(`\bkey-(?P<keyid>\d+)\b`)
- whoUsernameRegex = regexp.MustCompile(`\busername-(?P<username>\S+)\b`)
+ GitlabShell Executable = "gitlab-shell"
)
-type CommandArgs struct {
- arguments []string
- GitlabUsername string
- GitlabKeyId string
- SshArgs []string
- CommandType CommandType
-}
-
-func Parse(arguments []string) (*CommandArgs, error) {
- args := &CommandArgs{arguments: arguments}
-
- if args.Executable() == GitlabShell {
- if sshConnection := os.Getenv("SSH_CONNECTION"); sshConnection == "" {
- return nil, errors.New("Only ssh allowed")
- }
-
- args.parseWho()
-
- if err := args.parseCommand(os.Getenv("SSH_ORIGINAL_COMMAND")); err != nil {
- return nil, errors.New("Invalid ssh command")
- }
- args.defineCommandType()
-
- return args, nil
- } else {
- return args, nil
- }
-}
-
-func (c *CommandArgs) Executable() Executable {
- return Executable(filepath.Base(c.arguments[0]))
-}
-
-func (c *CommandArgs) Arguments() []string {
- return c.arguments[1:]
+type CommandArgs interface {
+ Parse() error
+ Executable() Executable
+ Arguments() []string
}
-func (c *CommandArgs) parseWho() {
- for _, argument := range c.arguments {
- if keyId := tryParseKeyId(argument); keyId != "" {
- c.GitlabKeyId = keyId
- break
- }
+func Parse(arguments []string) (CommandArgs, error) {
+ var args CommandArgs = &BaseArgs{arguments: arguments}
- if username := tryParseUsername(argument); username != "" {
- c.GitlabUsername = username
- break
- }
+ switch args.Executable() {
+ case GitlabShell:
+ args = &Shell{BaseArgs: args.(*BaseArgs)}
}
-}
-
-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 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]
- }
-
- return ""
-}
-
-func (c *CommandArgs) parseCommand(commandString string) error {
- args, err := shellwords.Parse(commandString)
- if err != nil {
- return 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...)
+ if err := args.Parse(); err != nil {
+ return nil, err
}
- 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
}