summaryrefslogtreecommitdiff
path: root/go/cmd/gitlab-shell/main.go
blob: 331e93defe500eb344e2b01ee1681816bf1e85ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main

import (
	"fmt"
	"os"
	"path/filepath"
	"syscall"

	"gitlab.com/gitlab-org/gitlab-shell/go/cmd/gitlab-shell/command"
	"gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
)

var (
	binDir   string
	rootDir  string
	features map[command.CommandType]bool
)

func init() {
	binDir = filepath.Dir(os.Args[0])
	rootDir = filepath.Dir(binDir)
	features = map[command.CommandType]bool{}
}

func migrate(config *config.Config) (int, bool) {
	if !config.Migration.Enabled {
		return 0, false
	}
	command, err := command.New(os.Args)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to build command: %v\n", err)
		return 0, false
	}

	if featureEnabled(config, command.Type) {

	}

	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() {
	rubyCmd := filepath.Join(binDir, "gitlab-shell-ruby")

	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)
	}
}

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.NewFromDir(rootDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed to read config, falling back to gitlab-shell-ruby: %v", err)
		execRuby()
	}

	// Try to handle the command with the Go implementation
	if exitCode, done := migrate(config); done {
		os.Exit(exitCode)
	}

	// Since a migration has not handled the command, fall back to Ruby to do so
	execRuby()
}

func featureEnabled(config *config.Config, commandType command.CommandType) bool {
	if features[commandType] {
		return true
	}

	fmt.Fprintf(os.Stderr, "Config: %v", config.Migration)

	for _, featureName := range config.Migration.Features {
		fmt.Fprintf(os.Stderr, "Setting feature: %v to %v", featureName, command.CommandType(featureName))

		features[command.CommandType(featureName)] = true
	}

	return features[commandType]
}