Skip to content

Commit

Permalink
Add check-yes and check-no modes
Browse files Browse the repository at this point in the history
  • Loading branch information
href committed Jun 23, 2024
1 parent 546f9ef commit dcfc94a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
21 changes: 14 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ func getApp() *cli.Cli {
Servers can be passed via -s/--servers, or STDIN.
Output Modes (-m/--mode):
host: shows server before each outputted line, default
plain: show output as-is
check: show server and ✓ on success, x on failure, no output
exit: show server and exit code, no output
slient: show nothing
host shows server before each outputted line, default
plain show output as-is
check show server and ✓ on success, x on failure, no output
check-yes show server and ✓ on success, nothing otherwise
check-no show server and x on success, nothing otherwise
exit show server and exit code, no output
slient show nothing
Exit Code:
ssh-each will return an exit code of 0, if at least one command
completed and all completed commands were successful.
This can be overwritten by using --exit-ok.
`), "\r\n"))

app.Spec = strings.Join([]string{
Expand All @@ -38,15 +42,18 @@ func getApp() *cli.Cli {
"[-u=<user>]",
"[-p=<port>]",
"[-m=<mode>]",
"[--exit-ok]",
"COMMAND",
}, " ")

exitOK := false
builder := ssh.CommandBuilder{}
servers := app.StringOpt("s servers", "", "Comma separated servers")
workers := app.IntOpt("w workers", 16, "Concurrent SSH processes")
port := app.IntOpt("p port", 0, "Default port")
mode := app.StringOpt("m mode", "host", "Output mode")
app.BoolOptPtr(&builder.TTY, "t tty", false, "Use pseudo-terminal")
app.BoolOptPtr(&exitOK, "exit-ok", false, "Ignore server command errors")
app.StringOptPtr(&builder.ExplicitUser, "u user", "", "Default user")
app.StringArgPtr(&builder.Command, "COMMAND", "", "Command to execute")

Expand All @@ -58,7 +65,7 @@ func getApp() *cli.Cli {
os.Exit(1)
}

if !(0 <= *port && *port <= 65535) {
if *port < 0 || 65535 < *port {
fmt.Println("Invalid default port")
os.Exit(1)
}
Expand Down Expand Up @@ -99,7 +106,7 @@ func getApp() *cli.Cli {
rep.On(result)
}

if rep.Success() {
if exitOK || rep.Success() {
os.Exit(0)
} else {
os.Exit(1)
Expand Down
25 changes: 24 additions & 1 deletion term/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const (
// CheckReport shows the hostname and a ✓ or x depending on the exit code.
CheckReport

// CheckYesReport works like CheckReport, but hides x's
CheckYesReport

// CheckYesReport works like CheckReport, but hides ✓'s
CheckNoReport

// ExitReport shows the hostname and the exit code.
ExitReport

Expand All @@ -44,6 +50,10 @@ func ReportModeFromString(mode string) (ReportMode, bool) {
return PlainReport, true
case "check":
return CheckReport, true
case "check-yes":
return CheckYesReport, true
case "check-no":
return CheckNoReport, true
case "exit":
return ExitReport, true
case "silent":
Expand All @@ -65,9 +75,10 @@ type Report struct {

// NewReport creates a new report.
func NewReport(mode ReportMode) Report {
if !(MinReport <= mode && mode <= MaxReport) {
if mode < MinReport || MaxReport < mode {
panic(fmt.Sprintf("unsupported mode: %d", mode))
}

return Report{
mode: mode,
exitCodes: make([]int, 0),
Expand Down Expand Up @@ -136,6 +147,10 @@ func (r *Report) printOutput(file *os.File, server string, output string) {
return
case CheckReport:
return
case CheckYesReport:
return
case CheckNoReport:
return
case ExitReport:
return
case PlainReport:
Expand Down Expand Up @@ -180,6 +195,14 @@ func (r *Report) printResult(server string, exitCode int) {
}

fmt.Fprint(os.Stdout, server, ": ", mark, "\n")
case CheckYesReport:
if r.successCodes[exitCode] {
fmt.Fprint(os.Stdout, server, ": ✓\n")
}
case CheckNoReport:
if !r.successCodes[exitCode] {
fmt.Fprint(os.Stdout, server, ": x\n")
}
case ExitReport:
fmt.Fprint(os.Stdout, server, ": ", exitCode, "\n")
default:
Expand Down

0 comments on commit dcfc94a

Please sign in to comment.