summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2018-09-28 15:17:17 +0100
committerNick Thomas <nick@gitlab.com>2018-09-28 15:24:45 +0100
commitf435c4644abc167cdfe6bfe3d320ff840f468c09 (patch)
treec4b4754790e619082ca935cef0bb489f3a6470b6
parent7f1098a1d9fd79b394b53b0c43fcc4741349d43a (diff)
downloadgitlab-shell-f435c4644abc167cdfe6bfe3d320ff840f468c09.tar.gz
Allow the config directory to be specified
-rw-r--r--go/cmd/gitlab-shell/main.go27
-rw-r--r--go/internal/config/config.go19
-rw-r--r--go/internal/config/config_test.go2
3 files changed, 31 insertions, 17 deletions
diff --git a/go/cmd/gitlab-shell/main.go b/go/cmd/gitlab-shell/main.go
index 44384ab..ae54151 100644
--- a/go/cmd/gitlab-shell/main.go
+++ b/go/cmd/gitlab-shell/main.go
@@ -9,21 +9,28 @@ import (
"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
)
-func migrate(_cfg *config.Config, _args []string) (int, bool) {
- // TODO: decide whether to handle the request in Go or not
+var (
+ binDir string
+ rootDir string
+)
+
+func init() {
+ binDir = filepath.Dir(os.Args[0])
+ rootDir = filepath.Dir(binDir)
+}
+
+func migrate(*config.Config) (int, bool) {
+ // TODO: Dispatch appropriate requests to Go handlers and return
+ // <exitstatus, true> depending on how they fare
return 0, false
}
// rubyExec will never return. It either replaces the current process with a
// Ruby interpreter, or outputs an error and kills the process.
func execRuby() {
- root := filepath.Dir(os.Args[0])
-
- rubyCmd := filepath.Join(root, "gitlab-shell-ruby")
- rubyArgs := os.Args[1:]
- rubyEnv := os.Environ()
+ rubyCmd := filepath.Join(binDir, "gitlab-shell-ruby")
- execErr := syscall.Exec(rubyCmd, rubyArgs, rubyEnv)
+ execErr := syscall.Exec(rubyCmd, os.Args, os.Environ())
if execErr != nil {
fmt.Fprintf(os.Stderr, "Failed to exec(%q): %v\n", rubyCmd, execErr)
os.Exit(1)
@@ -33,14 +40,14 @@ func execRuby() {
func main() {
// 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.New()
+ config, err := config.NewFromDir(rootDir)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to read config, falling back to gitlab-shell-ruby")
execRuby()
}
// Try to handle the command with the Go implementation
- if exitCode, done := migrate(config, os.Args[1:]); done {
+ if exitCode, done := migrate(config); done {
os.Exit(exitCode)
}
diff --git a/go/internal/config/config.go b/go/internal/config/config.go
index 4069851..435cb29 100644
--- a/go/internal/config/config.go
+++ b/go/internal/config/config.go
@@ -26,24 +26,31 @@ type Config struct {
}
func New() (*Config, error) {
- cfg := Config{}
-
dir, err := os.Getwd()
if err != nil {
return nil, err
}
- cfg.RootDir = dir
- configBytes, err := ioutil.ReadFile(path.Join(cfg.RootDir, configFile))
+ return NewFromDir(dir)
+}
+
+func NewFromDir(dir string) (*Config, error) {
+ return newFromFile(path.Join(dir, configFile))
+}
+
+func newFromFile(filename string) (*Config, error) {
+ cfg := &Config{RootDir: path.Dir(filename)}
+
+ configBytes, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
- if err := parseConfig(configBytes, &cfg); err != nil {
+ if err := parseConfig(configBytes, cfg); err != nil {
return nil, err
}
- return &cfg, nil
+ return cfg, nil
}
// parseConfig expects YAML data in configBytes and a Config instance with RootDir set.
diff --git a/go/internal/config/config_test.go b/go/internal/config/config_test.go
index 368ab29..87a582f 100644
--- a/go/internal/config/config_test.go
+++ b/go/internal/config/config_test.go
@@ -6,7 +6,7 @@ import (
"testing"
)
-func TestConfigLogFile(t *testing.T) {
+func TestParseConfig(t *testing.T) {
testRoot := "/foo/bar"
testCases := []struct {
yaml string