-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (124 loc) · 3.42 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"bytes"
"flag"
"fmt"
"os"
"sort"
"github.com/hatchify/scribe"
"github.com/hatchify/stringset"
)
var out = scribe.New("Branch releaser")
func main() {
var (
// Source branch where data will be merged from
source string
// Destination branch where data will be merged into
destination string
// Whether or not the current request is for the current directory or its children
recursive bool
// How the error should be handled
onError string
err error
)
flag.StringVar(&source, "source", "", "Source branch to merge from")
flag.StringVar(&destination, "destination", "", "Destination branch to merge into")
flag.BoolVar(&recursive, "r", false, "Whether or not the current request is for the current directory or its children")
flag.StringVar(&onError, "onError", "exit", "How to handle errors (e.g. exit, warn, ignore)")
flag.Parse()
defer close(err)
if len(source) == 0 {
out.Error("invalid source branch, cannot be empty")
return
}
if len(destination) == 0 {
out.Error("invalid destination branch, cannot be empty")
return
}
warn := getMeta(".branch-releaser-warn")
if warn.Has(destination) && !requestPermission(destination) {
out.Warning("Aborting..")
return
}
if !recursive {
// Recursive flag isn't set, run release within the current directory then
err = release(source, destination)
return
}
var cwd string
// Get the current working directory
if cwd, err = os.Getwd(); err != nil {
out.Errorf("error getting current working directory: %v", err)
return
}
// Ensure we switch to the current working directory after our releases have completed
defer os.Chdir(cwd)
successful := stringset.New()
errored := stringset.New()
out.Notificationf("Beginning recursive release for the children of \"%s\"", cwd)
var dirs []string
// Get the child directories for the current directory
if dirs, err = getDirs(); err != nil {
out.Errorf("error getting target directories: %v", err)
return
}
// Get ignored list
ignored := getMeta(".branch-releaser-ignore")
// Iterate through all the child directories
for _, dir := range dirs {
if ignored.Has(dir) {
continue
}
// Execute release for current child
if err = executeWithinDir(dir, func() (err error) {
return release(source, destination)
}); err == nil {
successful.Set(dir)
continue
}
switch onError {
case "exit":
out.Error(err.Error())
return
case "warn":
out.Warning(err.Error())
case "ignore":
}
err = nil
errored.Set(dir)
}
successResults := successful.Slice()
sort.Slice(successResults, func(a, b int) (less bool) {
return successResults[a] < successResults[b]
})
outBuf := bytes.NewBuffer(nil)
outBuf.WriteString("Successful:\n")
for _, result := range successResults {
line := fmt.Sprintf("\t- %s\n", result)
outBuf.WriteString(line)
}
fmt.Print("\n")
out.Success(outBuf.String())
errorResults := errored.Slice()
sort.Slice(errorResults, func(a, b int) (less bool) {
return errorResults[a] < errorResults[b]
})
if len(errorResults) > 0 {
errBuf := bytes.NewBuffer(nil)
errBuf.WriteString("Errored:\n")
for _, result := range errorResults {
line := fmt.Sprintf("\t- %s\n", result)
errBuf.WriteString(line)
}
fmt.Print("\n")
out.Error(errBuf.String())
}
}
func close(err error) {
if err == nil {
// No error exists, exit with a OK status code
os.Exit(0)
}
// Error exists, exit with an error status code
os.Exit(1)
}