From aab85f3600caf04b491d6ca4fc3f0f004d9e3fc0 Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Mon, 29 Jul 2019 14:33:01 +0800 Subject: Support falling back to ruby version of checkers Rename the ruby scripts to have `-ruby` suffix and add a symlink for both to `./gitlab-shell`. The executable name will be used to determine how args will be parsed. For now, we only parse the arguments for gitlab-shell commands. If the executable is `gitlab-shell-authorized-keys-check` or `gitlab-shell-authorized-principals-check`, it'll always fallback to the ruby version. Ruby specs test the ruby script, the fallback from go to ruby and go implementation of both (still pending). --- go/cmd/gitlab-shell/main.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) (limited to 'go/cmd/gitlab-shell/main.go') diff --git a/go/cmd/gitlab-shell/main.go b/go/cmd/gitlab-shell/main.go index f4d519f..be388d3 100644 --- a/go/cmd/gitlab-shell/main.go +++ b/go/cmd/gitlab-shell/main.go @@ -6,7 +6,6 @@ import ( "path/filepath" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command" - "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/fallback" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" ) @@ -28,17 +27,6 @@ func findRootDir() (string, error) { return filepath.Dir(filepath.Dir(path)), nil } -// rubyExec will never return. It either replaces the current process with a -// Ruby interpreter, or outputs an error and kills the process. -func execRuby(rootDir string, readWriter *readwriter.ReadWriter) { - cmd := &fallback.Command{RootDir: rootDir, Args: os.Args} - - if err := cmd.Execute(); err != nil { - fmt.Fprintf(readWriter.ErrOut, "Failed to exec: %v\n", err) - os.Exit(1) - } -} - func main() { readWriter := &readwriter.ReadWriter{ Out: os.Stdout, @@ -52,12 +40,10 @@ func main() { os.Exit(1) } - // Fall back to Ruby in case of problems reading the config, but issue a - // warning as this isn't something we can sustain indefinitely config, err := config.NewFromDir(rootDir) if err != nil { - fmt.Fprintln(readWriter.ErrOut, "Failed to read config, falling back to gitlab-shell-ruby") - execRuby(rootDir, readWriter) + fmt.Fprintln(readWriter.ErrOut, "Failed to read config, exiting") + os.Exit(1) } cmd, err := command.New(os.Args, config, readWriter) -- cgit v1.2.1 From 3b6f9f7583755e041e76142d7caf7716937907fa Mon Sep 17 00:00:00 2001 From: Patrick Bajao Date: Fri, 2 Aug 2019 16:10:17 +0800 Subject: Add Executable struct This struct is responsible for determining the name and root dir of the executable. The `RootDir` property will be used to find the config. The `Name` property will be used to determine what `Command` and `CommandArgs` to be built. --- go/cmd/gitlab-shell/main.go | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) (limited to 'go/cmd/gitlab-shell/main.go') diff --git a/go/cmd/gitlab-shell/main.go b/go/cmd/gitlab-shell/main.go index be388d3..b716820 100644 --- a/go/cmd/gitlab-shell/main.go +++ b/go/cmd/gitlab-shell/main.go @@ -3,30 +3,13 @@ package main import ( "fmt" "os" - "path/filepath" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command" "gitlab.com/gitlab-org/gitlab-shell/go/internal/command/readwriter" "gitlab.com/gitlab-org/gitlab-shell/go/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/go/internal/executable" ) -// findRootDir determines the root directory (and so, the location of the config -// file) from os.Executable() -func findRootDir() (string, error) { - if path := os.Getenv("GITLAB_SHELL_DIR"); path != "" { - return path, nil - } - - path, err := os.Executable() - if err != nil { - return "", err - } - - // Start: /opt/.../gitlab-shell/bin/gitlab-shell - // Ends: /opt/.../gitlab-shell - return filepath.Dir(filepath.Dir(path)), nil -} - func main() { readWriter := &readwriter.ReadWriter{ Out: os.Stdout, @@ -34,19 +17,19 @@ func main() { ErrOut: os.Stderr, } - rootDir, err := findRootDir() + executable, err := executable.New() if err != nil { - fmt.Fprintln(readWriter.ErrOut, "Failed to determine root directory, exiting") + fmt.Fprintln(readWriter.ErrOut, "Failed to determine executable, exiting") os.Exit(1) } - config, err := config.NewFromDir(rootDir) + config, err := config.NewFromDir(executable.RootDir) if err != nil { fmt.Fprintln(readWriter.ErrOut, "Failed to read config, exiting") os.Exit(1) } - cmd, err := command.New(os.Args, config, readWriter) + cmd, err := command.New(executable, os.Args[1:], config, readWriter) if err != nil { // For now this could happen if `SSH_CONNECTION` is not set on // the environment -- cgit v1.2.1