diff options
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 +} |