Bladeren bron

add map of environment variables

bmallred 10 jaren geleden
bovenliggende
commit
fab59c7fc6
1 gewijzigde bestanden met toevoegingen van 57 en 66 verwijderingen
  1. 57 66
      cmd/app.go

+ 57 - 66
cmd/app.go

@ -1,7 +1,6 @@
1 1
package cmd
2 2
3 3
import (
4
	"bytes"
5 4
	"errors"
6 5
	"fmt"
7 6
	"io"
@ -10,6 +9,52 @@ import (
10 9
	"strings"
11 10
)
12 11
12
var (
13
	EnvironmentVariables = map[string]string{
14
		"CODE_VCS": "git;hg;tf;bzr",
15
16
		"CODE_GIT_ADD":      "add",
17
		"CODE_GIT_CHECK":    "branch",
18
		"CODE_GIT_INCOMING": "log ..@{u}",
19
		"CODE_GIT_MERGE":    "merge",
20
		"CODE_GIT_MV":       "mv",
21
		"CODE_GIT_PULL":     "fetch",
22
		"CODE_GIT_PUSH":     "push",
23
		"CODE_GIT_RM":       "rm",
24
		"CODE_GIT_UPDATE":   "pull",
25
26
		"CODE_HG_ADD":      "add",
27
		"CODE_HG_CHECK":    "branch",
28
		"CODE_HG_INCOMING": "incoming",
29
		"CODE_HG_MERGE":    "merge",
30
		"CODE_HG_MV":       "mv",
31
		"CODE_HG_PULL":     "pull",
32
		"CODE_HG_PUSH":     "push",
33
		"CODE_HG_RM":       "rm",
34
		"CODE_HG_UPDATE":   "pull -u",
35
36
		"CODE_BZR_ADD":      "add",
37
		"CODE_BZR_CHECK":    "root",
38
		"CODE_BZR_INCOMING": "missing",
39
		"CODE_BZR_MERGE":    "merge",
40
		"CODE_BZR_MV":       "mv",
41
		"CODE_BZR_PULL":     "pull",
42
		"CODE_BZR_PUSH":     "push",
43
		"CODE_BZR_RM":       "rm",
44
		"CODE_BZR_UPDATE":   "update",
45
46
		"CODE_TF_ADD":      "add",
47
		"CODE_TF_CHECK":    "branches .",
48
		"CODE_TF_INCOMING": "history -r -stopafter:1 -version:W~T .",
49
		"CODE_TF_MERGE":    "merge",
50
		"CODE_TF_MV":       "rename",
51
		"CODE_TF_PULL":     "get -preview",
52
		"CODE_TF_PUSH":     "checkin",
53
		"CODE_TF_RM":       "delete",
54
		"CODE_TF_UPDATE":   "get",
55
	}
56
)
57
13 58
// The application
14 59
type App struct {
15 60
	Args      []string
@ -71,70 +116,11 @@ func GetVersionControlSystems() []string {
71 116
}
72 117
73 118
func ConfigureEnvironment() {
74
	env := os.Getenv("CODE_VCS")
75
	if env == "" {
76
		os.Setenv("CODE_VCS", "git;hg;tf;bzr")
77
	}
119
	for key, value := range EnvironmentVariables {
120
		env := os.Getenv(key)
78 121
79
	systems := GetVersionControlSystems()
80
81
	// Configure VCS checking
82
	for _, vcs := range systems {
83
		key := fmt.Sprintf("CODE_%s_CHECK", strings.ToUpper(vcs))
84
		env = os.Getenv(key)
85
86
		// Temporarily define the subcommand `incoming` for known version control systems
87
		if env == "" {
88
			incoming := ""
89
90
			switch vcs {
91
			case "git":
92
				incoming = "branch"
93
				break
94
			case "hg":
95
				incoming = "branch"
96
				break
97
			case "bzr":
98
				incoming = "root"
99
				break
100
			case "tf":
101
				incoming = "branches ."
102
				break
103
			}
104
105
			if incoming != "" {
106
				os.Setenv(key, incoming)
107
			}
108
		}
109
	}
110
111
	// Configure VCS incoming
112
	for _, vcs := range systems {
113
		key := fmt.Sprintf("CODE_%s_INCOMING", strings.ToUpper(vcs))
114
		env = os.Getenv(key)
115
116
		// Temporarily define the subcommand `incoming` for known version control systems
117
		if env == "" {
118
			incoming := ""
119
120
			switch vcs {
121
			case "git":
122
				incoming = "log ..@{u}"
123
				break
124
			case "hg":
125
				incoming = "incoming"
126
				break
127
			case "bzr":
128
				incoming = "missing"
129
				break
130
			case "tf":
131
				incoming = "history -r -stopafter:1 -version:W~T ."
132
				break
133
			}
134
135
			if incoming != "" {
136
				os.Setenv(key, incoming)
137
			}
122
		if env == "" && value != "" {
123
			os.Setenv(key, value)
138 124
		}
139 125
	}
140 126
}
@ -146,11 +132,16 @@ func isVersionControlled(vcs, directory string) error {
146 132
		return errors.New(fmt.Sprintf("CODE_%s_CHECK is not set", strings.ToUpper(vcs)))
147 133
	}
148 134
135
	err := os.Chdir(directory)
136
	if err != nil {
137
		return err
138
	}
139
149 140
	// Execute the command and swallow any output
150
	var out bytes.Buffer
141
	//var out bytes.Buffer
151 142
	actions := strings.Split(env, " ")
152 143
	cmd := exec.Command(vcs, actions...)
153
	cmd.Stdout = &out
144
	//cmd.Stdout = &out
154 145
155 146
	return cmd.Run()
156 147
}