Skip to content

Commit d71d1f1

Browse files
committed
add job state
Signed-off-by: Michael Crosby <[email protected]>
1 parent 9678826 commit d71d1f1

File tree

1 file changed

+55
-11
lines changed

1 file changed

+55
-11
lines changed

main.go

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func multiplexAction(context *cli.Context) error {
6363
}
6464

6565
concurrent := context.GlobalInt("concurrency")
66+
lines := context.GlobalInt("lines")
6667

6768
// Parse OpenSSH client config file at ~/.ssh/config:
6869
user, err := user.Current()
@@ -115,7 +116,7 @@ func multiplexAction(context *cli.Context) error {
115116
host: host,
116117
config: sections[host],
117118
signal: signal,
118-
//lines: make([]string, 1),
119+
state: pending,
119120
})
120121
}
121122

@@ -127,15 +128,7 @@ func multiplexAction(context *cli.Context) error {
127128
for range signal {
128129
w.Flush()
129130
for _, i := range jobs {
130-
var data string
131-
status := green
132-
if i.err != nil {
133-
status = red
134-
data = i.err.Error()
135-
} else {
136-
data = i.read(5)
137-
}
138-
fmt.Fprintf(w, lineformat, status, underline, i.host, reset, data)
131+
fmt.Fprintf(w, lineformat, formatHostLine(i), i.read(lines))
139132
}
140133
w.Flush()
141134
}
@@ -155,21 +148,61 @@ func multiplexAction(context *cli.Context) error {
155148
return nil
156149
}
157150

151+
func getState(i int) string {
152+
switch i {
153+
case pending:
154+
return "PENDING"
155+
case running:
156+
return "RUNNING"
157+
case finished:
158+
return "FINISHED"
159+
default:
160+
return "UNKNOWN"
161+
}
162+
}
163+
164+
func formatHostLine(j *job) string {
165+
var (
166+
status = green
167+
statemsg = ""
168+
)
169+
if j.err != nil {
170+
status = red
171+
statemsg = fmt.Sprintf(": ERROR %s", j.err)
172+
} else {
173+
statemsg = fmt.Sprintf(": %s", getState(j.state))
174+
}
175+
return fmt.Sprintf("%s%s%s%s%s",
176+
status,
177+
underline,
178+
j.host,
179+
statemsg,
180+
reset,
181+
)
182+
}
183+
158184
const (
159185
escape = "\x1b"
160186
reset = escape + "[0m"
161187
red = escape + "[31m" // nolint: deadcode, varcheck, unused
162188
green = escape + "[32m"
163189
underline = escape + "[4m"
164190
)
165-
const lineformat = "%s%s%s%s\n%s\n"
191+
const lineformat = "%s\n%s\n"
192+
193+
const (
194+
pending = iota + 1
195+
running
196+
finished
197+
)
166198

167199
type job struct {
168200
host string
169201
config SSHClientOptions
170202
signal chan struct{}
171203
lines []string
172204
err error
205+
state int
173206
}
174207

175208
func (i *job) read(count int) string {
@@ -184,13 +217,19 @@ func executeCommand(wg *sync.WaitGroup, jobs chan *job, c command, user string,
184217
defer wg.Done()
185218

186219
for job := range jobs {
220+
job.state = running
221+
job.signal <- struct{}{}
222+
187223
var err error
188224
if job.host, err = cleanHost(job.host); err != nil {
189225
job.err = err
226+
continue
190227
}
191228
if err = runSSH(job, c, user, agt, methods, cliOptions, quiet); err != nil {
192229
job.err = err
193230
}
231+
job.state = finished
232+
job.signal <- struct{}{}
194233
}
195234
}
196235

@@ -321,6 +360,11 @@ func main() {
321360
Usage: "set the concurrent worker limit",
322361
Value: 10,
323362
},
363+
cli.IntFlag{
364+
Name: "lines,l",
365+
Usage: "number of lines to display on screen at once",
366+
Value: 5,
367+
},
324368
}
325369
app.Action = multiplexAction
326370
if err := app.Run(os.Args); err != nil {

0 commit comments

Comments
 (0)