summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2019-04-09 16:28:27 +0100
committerNick Thomas <nick@gitlab.com>2019-04-12 15:16:08 +0100
commitf6a7f1714763d8f0048c23f0cc75184addd6d4dc (patch)
tree531f86122e77d293c6cbd2da9118846710d1d846
parent1b741a0bcbb5d47e546291a5f2dd538d2928e9ad (diff)
downloadgitlab-shell-f6a7f1714763d8f0048c23f0cc75184addd6d4dc.tar.gz
Correctly determine the root directory for gitlab-shell
Credit to https://gitlab.com/ejiek for spotting this one.
-rw-r--r--go/cmd/gitlab-shell/main.go35
1 files changed, 24 insertions, 11 deletions
diff --git a/go/cmd/gitlab-shell/main.go b/go/cmd/gitlab-shell/main.go
index 51b5210..04de4c4 100644
--- a/go/cmd/gitlab-shell/main.go
+++ b/go/cmd/gitlab-shell/main.go
@@ -11,21 +11,22 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
)
-var (
- binDir string
- rootDir string
- readWriter *readwriter.ReadWriter
-)
+// findRootDir determines the root directory (and so, the location of the config
+// file) from os.Executable()
+func findRootDir() (string, error) {
+ path, err := os.Executable()
+ if err != nil {
+ return "", err
+ }
-func init() {
- binDir = filepath.Dir(os.Args[0])
- rootDir = filepath.Dir(binDir)
- readWriter = &readwriter.ReadWriter{Out: os.Stdout, In: os.Stdin, ErrOut: os.Stderr}
+ // Start: /opt/.../gitlab-shell/bin/gitlab-shell
+ // Ends: /opt/.../gitlab-shell
+ 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() {
+func execRuby(readWriter *readwriter.ReadWriter) {
cmd := &fallback.Command{}
if err := cmd.Execute(readWriter); err != nil {
fmt.Fprintf(readWriter.ErrOut, "Failed to exec: %v\n", err)
@@ -34,12 +35,24 @@ func execRuby() {
}
func main() {
+ readWriter := &readwriter.ReadWriter{
+ Out: os.Stdout,
+ In: os.Stdin,
+ ErrOut: os.Stderr,
+ }
+
+ rootDir, err := findRootDir()
+ if err != nil {
+ fmt.Fprintln(readWriter.ErrOut, "Failed to determine root directory, falling back to gitlab-shell-ruby")
+ execRuby(readWriter)
+ }
+
// 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()
+ execRuby(readWriter)
}
cmd, err := command.New(os.Args, config)