diff options
author | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-12-19 17:43:17 +0100 |
---|---|---|
committer | Bob Van Landuyt <bob@vanlanduyt.co> | 2018-12-20 17:58:44 +0100 |
commit | 9d5b20753967c88bd76f55ecbc37e854bd3a63ce (patch) | |
tree | ca18b287c1527d60adaea38d83d4b441cfa6d1fc /go | |
parent | f8d74597781f9ac9bfe2dbe6b1feacb512a52ae8 (diff) | |
download | gitlab-shell-bvl-port-gitlabnet-to-go.tar.gz |
WIP: Parse commands & enable featuresbvl-port-gitlabnet-to-go
Diffstat (limited to 'go')
-rw-r--r-- | go/cmd/gitlab-shell/command/command.go | 92 | ||||
-rw-r--r-- | go/cmd/gitlab-shell/main.go | 42 | ||||
-rw-r--r-- | go/internal/config/config.go | 4 | ||||
-rw-r--r-- | go/internal/gitlabclient/client.go | 14 |
4 files changed, 134 insertions, 18 deletions
diff --git a/go/cmd/gitlab-shell/command/command.go b/go/cmd/gitlab-shell/command/command.go new file mode 100644 index 0000000..c35a685 --- /dev/null +++ b/go/cmd/gitlab-shell/command/command.go @@ -0,0 +1,92 @@ +package command + +import ( + "os" + "regexp" + "strconv" +) + +type CommandType string + +const ( + Discover CommandType = "discover" +) + +type Command struct { + GitlabUsername string + GitlabKeyId int + SshConnection bool + Command string + Type CommandType +} + +func New(arguments []string) (*Command, error) { + _, sshConnection := os.LookupEnv("SSH_CONNECTION") + + command := &Command{SshConnection: sshConnection} + + if _, err := parseWho(arguments, command); err != nil { + return nil, err + } + + originalCommand, _ := os.LookupEnv("SSH_ORIGINAL_COMMAND") + parseCommand(originalCommand, command) + + return command, nil +} + +func parseWho(arguments []string, command *Command) (*Command, error) { + var err error = nil + + for _, argument := range arguments { + keyId, err := tryParseKeyId(argument) + if keyId > 0 && err != nil { + command.GitlabKeyId = keyId + break + } + + username, err := tryParseUsername(argument) + if username != "" && err != nil { + command.GitlabUsername = username + break + } + } + + return command, err +} + +func tryParseKeyId(argument string) (int, error) { + whoKeyRegex, err := regexp.Compile(`\bkey-(?P<keyid>\d+)\b`) + if err != nil { + return 0, err + } + + keyMatch := whoKeyRegex.FindString(argument) + if keyMatch != "" { + gitlabKeyId, err := strconv.Atoi(keyMatch) + + return gitlabKeyId, err + } + + return 0, nil +} + +func tryParseUsername(argument string) (string, error) { + whoUsernameRegex, err := regexp.Compile(`\busername-(?P<username>\S+)\b`) + if err != nil { + return "", err + } + + usernameMatch := whoUsernameRegex.FindString(argument) + return usernameMatch, nil +} + +func parseCommand(commandString string, command *Command) *Command { + command.Command = commandString + + if commandString == "" { + command.Type = Discover + } + + return nil +} diff --git a/go/cmd/gitlab-shell/main.go b/go/cmd/gitlab-shell/main.go index ae54151..331e93d 100644 --- a/go/cmd/gitlab-shell/main.go +++ b/go/cmd/gitlab-shell/main.go @@ -6,22 +6,36 @@ import ( "path/filepath" "syscall" + "gitlab.com/gitlab-org/gitlab-shell/go/cmd/gitlab-shell/command" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" ) var ( - binDir string - rootDir string + binDir string + rootDir string + features map[command.CommandType]bool ) func init() { binDir = filepath.Dir(os.Args[0]) rootDir = filepath.Dir(binDir) + features = map[command.CommandType]bool{} } -func migrate(*config.Config) (int, bool) { - // TODO: Dispatch appropriate requests to Go handlers and return - // <exitstatus, true> depending on how they fare +func migrate(config *config.Config) (int, bool) { + if !config.Migration.Enabled { + return 0, false + } + command, err := command.New(os.Args) + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to build command: %v\n", err) + return 0, false + } + + if featureEnabled(config, command.Type) { + + } + return 0, false } @@ -42,7 +56,7 @@ func main() { // warning as this isn't something we can sustain indefinitely config, err := config.NewFromDir(rootDir) if err != nil { - fmt.Fprintln(os.Stderr, "Failed to read config, falling back to gitlab-shell-ruby") + fmt.Fprintf(os.Stderr, "Failed to read config, falling back to gitlab-shell-ruby: %v", err) execRuby() } @@ -54,3 +68,19 @@ func main() { // Since a migration has not handled the command, fall back to Ruby to do so execRuby() } + +func featureEnabled(config *config.Config, commandType command.CommandType) bool { + if features[commandType] { + return true + } + + fmt.Fprintf(os.Stderr, "Config: %v", config.Migration) + + for _, featureName := range config.Migration.Features { + fmt.Fprintf(os.Stderr, "Setting feature: %v to %v", featureName, command.CommandType(featureName)) + + features[command.CommandType(featureName)] = true + } + + return features[commandType] +} diff --git a/go/internal/config/config.go b/go/internal/config/config.go index d325fd4..76ba07d 100644 --- a/go/internal/config/config.go +++ b/go/internal/config/config.go @@ -74,7 +74,9 @@ func parseConfig(configBytes []byte, cfg *Config) error { cfg.LogFormat = "text" } - baseUrl, err := url.Parse(cfg.GitlabUrl) + unescapedUrl, err := url.PathUnescape(cfg.GitlabUrl) + + baseUrl, err := url.Parse(unescapedUrl) if err != nil { return err } diff --git a/go/internal/gitlabclient/client.go b/go/internal/gitlabclient/client.go index c186b6c..5a8dd9d 100644 --- a/go/internal/gitlabclient/client.go +++ b/go/internal/gitlabclient/client.go @@ -4,7 +4,7 @@ import ( "net/http" "time" - "gitlab.com/gitlab-org/go/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" ) type Client struct { @@ -21,20 +21,12 @@ func New() (*Client, error) { return nil, err } - tr = &http.Transport{ + tr := &http.Transport{ MaxIdleConns: 10, IdleConnTimeout: 30 * time.Second, DisableCompression: true, } - httpClient = &http.Client{Transport: tr} + httpClient := &http.Client{Transport: tr} return &Client{config: config, httpClient: httpClient}, nil } - -func (c *Client) Discover(gitlabId string) { - -} - -func (c *Client) get(path string) (*Response, error) { - -} |