-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
230 lines (202 loc) · 5.43 KB
/
run.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// cspell:ignore fatih cleanup yoinked
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/alecthomas/kong"
"github.com/fatih/color"
)
type run struct {
Visibility string `short:"v" enum:"all,public,private" default:"all" help:"Filter by repo visibility."`
Affiliations []string `short:"a" enum:"owner,collaborator,organization_member" default:"owner" help:"Filter by affiliation to repo."`
Languages []string `short:"l" help:"Filter by repos containing one or more of the provided languages."`
Shell string `short:"s" env:"SHELL" help:"Shell to run command with."`
Interactive bool `short:"i" help:"Run commands sequentially and interactively."`
Regex *regexp.Regexp `short:"r" default:".*" help:"Filter via regex match on repo name."`
NoConfirm bool `short:"N" help:"Don't ask for confirmation."`
Cleanup bool `short:"c" help:"Remove temporary directory after running."`
Command []string `arg:"" help:"The command to run."`
Unlabelled bool `short:"u" help:"Don't display labels next to non-interactive output."`
}
// shamelessly yoinked from: https://github.com/docker/compose/blob/7c47673d4af41d79900e6c70bc1a3f9f17bdd387/pkg/utils/writer.go#L1-L62
type repoOutput struct {
name string
maxNameLen int
color color.Attribute
out io.Writer
buffer bytes.Buffer
}
func (o *repoOutput) Write(b []byte) (n int, err error) {
n, err = o.buffer.Write(b)
if err != nil {
return
}
for {
b = o.buffer.Bytes()
index := bytes.Index(b, []byte{'\n'})
if index < 0 {
break
}
_, err := color.New(o.color).Fprintf(o.out, "%s %s|", o.name,
strings.Repeat(" ", o.maxNameLen-len(o.name)))
if err != nil {
return n, err
}
_, err = o.out.Write([]byte{' '})
if err != nil {
return n, err
}
_, err = o.out.Write(o.buffer.Next(index + 1))
if err != nil {
return n, err
}
}
return
}
func (o *repoOutput) Close() (err error) {
b := o.buffer.Bytes()
if len(b) == 0 {
return
}
_, err = color.New(o.color).Fprintf(o.out, "%s %s|", o.name,
strings.Repeat(" ", o.maxNameLen-len(o.name)))
if err != nil {
return
}
_, err = o.out.Write([]byte{' '})
if err != nil {
return
}
_, err = o.out.Write(b)
if err != nil {
return
}
if b[len(b)-1] != '\n' {
_, err = o.out.Write([]byte{'\n'})
}
if err != nil {
return
}
return
}
func NewOutputPair(name string, maxNameLen int, index int) (repoOutput, repoOutput) {
var c int
wrappedIndex := index % 14
if wrappedIndex < 7 {
c = 31 + wrappedIndex
} else {
c = 84 + wrappedIndex
}
return repoOutput{name, maxNameLen, color.Attribute(c), os.Stdout, bytes.Buffer{}},
repoOutput{name, maxNameLen, color.Attribute(c), os.Stderr, bytes.Buffer{}}
}
func (c *run) Run(ctx *kong.Context) error {
repos := getRepos(c.Visibility, c.Affiliations, c.Languages, *c.Regex)
if !c.NoConfirm {
names := make([]string, len(repos))
for i, r := range repos {
names[i] = fmt.Sprintf("%s/%s", r.Owner.Login, r.Name)
}
fmt.Printf("%s\nContinue? [Y/n] ", strings.Join(names, "\n"))
var userResponse string
fmt.Scanln(&userResponse)
if !containsString([]string{"", "y", "yes"}, strings.ToLower(userResponse)) {
log.Fatalf("User cancelled with input %s\n", userResponse)
}
}
tmpDir := createTmpDir()
if c.Interactive {
copyWgs := make([]sync.WaitGroup, len(repos))
var cleanupWgs []sync.WaitGroup
if c.Cleanup {
cleanupWgs = make([]sync.WaitGroup, len(repos))
}
for i, r := range repos {
copyWgs[i].Add(1)
go func(i int, r repo) {
defer copyWgs[i].Done()
createCopy(r, tmpDir)
}(i, r)
}
for i, r := range repos {
copyWgs[i].Wait()
cmd := exec.Cmd{Path: c.Shell, Args: []string{c.Shell, "-c", strings.Join(c.Command, " ")},
Dir: r.tmpDir(tmpDir), Stdout: os.Stdout, Stderr: os.Stderr, Stdin: os.Stdin}
err := cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if c.Cleanup {
cleanupWgs[i].Add(1)
go func(i int, r repo) {
defer cleanupWgs[i].Done()
err := os.RemoveAll(r.tmpDir(tmpDir))
if err != nil {
log.Fatalln(err)
}
}(i, r)
}
}
if c.Cleanup {
for i := range cleanupWgs {
cleanupWgs[i].Wait()
}
err := os.RemoveAll(tmpDir)
if err != nil {
log.Fatalln(err)
}
}
} else {
var maxNameLen int
if !c.Unlabelled {
maxNameLen = 0
for _, r := range repos {
if len(r.Name) > maxNameLen {
maxNameLen = len(r.Name)
}
}
}
var wg sync.WaitGroup
wg.Add(len(repos))
for i, r := range repos {
go func(r repo, i int) {
defer wg.Done()
createCopy(r, tmpDir)
var cmd exec.Cmd
if c.Unlabelled {
cmd = exec.Cmd{Path: c.Shell, Args: []string{c.Shell, "-c", strings.Join(c.Command, " ")},
Dir: r.tmpDir(tmpDir), Stdout: os.Stdout, Stderr: os.Stderr}
err := cmd.Run()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
} else {
stdout, stderr := NewOutputPair(r.Name, maxNameLen, i)
cmd = exec.Cmd{Path: c.Shell, Args: []string{c.Shell, "-c", strings.Join(c.Command, " ")},
Dir: r.tmpDir(tmpDir), Stdout: &stdout, Stderr: &stderr}
err := cmd.Run()
stdout.Close()
stderr.Close()
if err != nil {
fmt.Fprintln(&stderr, err)
}
}
if c.Cleanup {
os.RemoveAll(r.tmpDir(tmpDir))
}
}(r, i)
}
wg.Wait()
if c.Cleanup {
os.RemoveAll(tmpDir)
}
}
return nil
}