Commit f6a7f171 authored by Nick Thomas's avatar Nick Thomas

Correctly determine the root directory for gitlab-shell

Credit to https://gitlab.com/ejiek for spotting this one.
parent 1b741a0b
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment