diff options
author | Patrick Bajao <ebajao@gitlab.com> | 2019-08-02 16:10:17 +0800 |
---|---|---|
committer | Patrick Bajao <ebajao@gitlab.com> | 2019-08-02 16:10:17 +0800 |
commit | 3b6f9f7583755e041e76142d7caf7716937907fa (patch) | |
tree | ed7f7281633d97933e4465a2ac0f86d62c9a216e /go/internal/executable/executable.go | |
parent | 592823d5e25006331b361b36cc61df7802fc1938 (diff) | |
download | gitlab-shell-3b6f9f7583755e041e76142d7caf7716937907fa.tar.gz |
Add Executable struct181-migrate-gitlab-shell-checks-fallback
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.
Diffstat (limited to 'go/internal/executable/executable.go')
-rw-r--r-- | go/internal/executable/executable.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/go/internal/executable/executable.go b/go/internal/executable/executable.go new file mode 100644 index 0000000..63f4c90 --- /dev/null +++ b/go/internal/executable/executable.go @@ -0,0 +1,58 @@ +package executable + +import ( + "os" + "path/filepath" +) + +const ( + GitlabShell = "gitlab-shell" + AuthorizedKeysCheck = "gitlab-shell-authorized-keys-check" + AuthorizedPrincipalsCheck = "gitlab-shell-authorized-principals-check" +) + +type Executable struct { + Name string + RootDir string +} + +var ( + // osExecutable is overridden in tests + osExecutable = os.Executable +) + +func New() (*Executable, error) { + path, err := osExecutable() + if err != nil { + return nil, err + } + + rootDir, err := findRootDir(path) + if err != nil { + return nil, err + } + + executable := &Executable{ + Name: filepath.Base(path), + RootDir: rootDir, + } + + return executable, nil +} + +func findRootDir(path string) (string, error) { + // Start: /opt/.../gitlab-shell/bin/gitlab-shell + // Ends: /opt/.../gitlab-shell + rootDir := filepath.Dir(filepath.Dir(path)) + pathFromEnv := os.Getenv("GITLAB_SHELL_DIR") + + if pathFromEnv != "" { + if _, err := os.Stat(pathFromEnv); os.IsNotExist(err) { + return "", err + } + + rootDir = pathFromEnv + } + + return rootDir, nil +} |