-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_git.go
45 lines (37 loc) · 948 Bytes
/
env_git.go
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
package beluga
import (
"os/exec"
"strings"
)
func cmdString(cmd string, args ...string) string {
output, err := exec.Command(cmd, args...).Output()
if err != nil {
return ""
}
return strings.TrimSpace(string(output))
}
func gitEnvRead(e Environment) error {
versionTag := cmdString("git", "describe", "--tags", "--match", "v*", "--exclude \"v*-*\"")
branch := cmdString("git", "rev-parse", "--abbrev-ref", "HEAD")
environment := ""
// lists all tags that the current commit points to
for _, tag := range strings.Split(cmdString("git", "tag", "--points-at", "HEAD"), "\n") {
if tag == versionTag {
environment = envProduction
break
}
}
if environment == "" {
if branch == e.DefaultBranch() {
environment = envStaging
} else {
environment = envReview
}
}
env := parseVersion(versionTag)
if len(environment) > 0 && env != nil {
env[varEnvironment] = environment
}
e.MergeMissing(env)
return nil
}