summaryrefslogtreecommitdiff
path: root/go/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'go/cmd')
-rw-r--r--go/cmd/gitlab-shell/command/command.go92
-rw-r--r--go/cmd/gitlab-shell/main.go42
2 files changed, 128 insertions, 6 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]
+}