/
main.go
65 lines (54 loc) · 1.34 KB
/
main.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"os/exec"
)
func main() {
command := flag.String("command", "", "The git command")
ignoreErros := flag.Bool(
"ignore-errors",
false,
"Keep running after error if true")
flag.Parse()
// Get managed repos from environment variables
root := os.Getenv("MG_ROOT")
if root[len(root) - 1] != '/' {
root += "/"
}
repo_names := strings.Split(os.Getenv("MG_REPOS"), ",")
var repos []string
// Verify all repos exist and are actually git repos (have .git sub-dir)
for _, r := range repo_names {
path := root + r
_, err := os.Stat(path + "/.git")
if err != nil {
log.Fatal(err)
}
repos = append(repos, path)
}
// Break the git command into components (needed to execute)
var git_components []string
for _, component := range strings.Split(*command, " ") {
git_components = append(git_components, component)
}
command_string := "git " + *command
for _, r := range repos {
// Go to the repo's directory
os.Chdir(r);
// Print the command
fmt.Printf("[%s] %s\n", r, command_string)
// Execute the command
out, err := exec.Command("git", git_components...).CombinedOutput()
// Print the result
fmt.Println(string(out))
// Bail out if there was an error and NOT ignoring errors
if err != nil && !*ignoreErros {
os.Exit(1)
}
}
fmt.Println("Done.")
}