Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
fleetctl: make tryWaitForUnitStates return error instead of int
Browse files Browse the repository at this point in the history
Now that tryWaitForSystemdActiveState() returns error instead of int,
tryWaitForUnitStates() should also return error instead of int, to be
as idiomatic as possible.
  • Loading branch information
Dongsu Park committed Aug 19, 2016
1 parent 9a8d53a commit 09eb8e9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
10 changes: 5 additions & 5 deletions fleetctl/fleetctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,23 +960,23 @@ func getBlockAttempts(cCmd *cobra.Command) int {
// wait, it will assume that all units reached their desired state.
// If maxAttempts is zero tryWaitForUnitStates will retry forever, and
// if it is greater than zero, it will retry up to the indicated value.
// It returns 0 on success or 1 on errors.
func tryWaitForUnitStates(units []string, state string, js job.JobState, maxAttempts int, out io.Writer) (ret int) {
// It returns nil on success or error on failure.
func tryWaitForUnitStates(units []string, state string, js job.JobState, maxAttempts int, out io.Writer) error {
// We do not wait just assume we reached the desired state
if maxAttempts <= -1 {
for _, name := range units {
stdout("Triggered unit %s %s", name, state)
}
return
return nil
}

errchan := waitForUnitStates(units, js, maxAttempts, out)
for err := range errchan {
stderr("Error waiting for units: %v", err)
ret = 1
return err
}

return
return nil
}

// waitForUnitStates polls each of the indicated units until each of their
Expand Down
6 changes: 3 additions & 3 deletions fleetctl/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func runLoadUnit(cCmd *cobra.Command, args []string) (exit int) {
}
}

exitVal := tryWaitForUnitStates(loading, "load", job.JobStateLoaded, getBlockAttempts(cCmd), os.Stdout)
if exitVal != 0 {
stderr("Error waiting for unit states, exit status: %d", exitVal)
err = tryWaitForUnitStates(loading, "load", job.JobStateLoaded, getBlockAttempts(cCmd), os.Stdout)
if err != nil {
stderr("Error waiting for unit states, exit status: %v", err)
return 1
}

Expand Down
7 changes: 3 additions & 4 deletions fleetctl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ func runStartUnit(cCmd *cobra.Command, args []string) (exit int) {
}
}

exitVal := tryWaitForUnitStates(starting, "start", job.JobStateLaunched, getBlockAttempts(cCmd), os.Stdout)
if exitVal != 0 {
stderr("Error waiting for unit states, exit status: %d", exitVal)
return exitVal
if err := tryWaitForUnitStates(starting, "start", job.JobStateLaunched, getBlockAttempts(cCmd), os.Stdout); err != nil {
stderr("Error waiting for unit states, exit status: %v", err)
return 1
}

if err := tryWaitForSystemdActiveState(starting, getBlockAttempts(cCmd)); err != nil {
Expand Down
12 changes: 6 additions & 6 deletions fleetctl/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func runStopUnit(cCmd *cobra.Command, args []string) (exit int) {
}
}

exit = tryWaitForUnitStates(stopping, "stop", job.JobStateLoaded, getBlockAttempts(cCmd), os.Stdout)
if exit == 0 {
stderr("Successfully stopped units %v.", stopping)
} else {
stderr("Failed to stop units %v. exit == %d.", stopping, exit)
err = tryWaitForUnitStates(stopping, "stop", job.JobStateLoaded, getBlockAttempts(cCmd), os.Stdout)
if err != nil {
stderr("Failed to stop units %v. err: %v", stopping, err)
return 1
}

return
stderr("Successfully stopped units %v.", stopping)
return 0
}
12 changes: 6 additions & 6 deletions fleetctl/unload.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ func runUnloadUnit(cCmd *cobra.Command, args []string) (exit int) {
}
}

exit = tryWaitForUnitStates(wait, "unload", job.JobStateInactive, getBlockAttempts(cCmd), os.Stdout)
if exit == 0 {
stderr("Successfully unloaded units %v.", wait)
} else {
stderr("Failed to unload units %v. exit == %d.", wait, exit)
err = tryWaitForUnitStates(wait, "unload", job.JobStateInactive, getBlockAttempts(cCmd), os.Stdout)
if err != nil {
stderr("Failed to unload units %v. err: %v", wait, err)
return 1
}

return
stderr("Successfully unloaded units %v.", wait)
return 0
}

0 comments on commit 09eb8e9

Please sign in to comment.