Skip to content

Commit

Permalink
fix: Return non-zero code, when some sub-status is wrong
Browse files Browse the repository at this point in the history
* When some sub-status is not correct (system is not registered,
  insights-client is not registered or yggdrasil is not running),
  then return non-zero status code
* This change follow behavior of subscription-manager and it
  could help automation
  • Loading branch information
jirihnidek authored and subpop committed Sep 10, 2024
1 parent abefeb8 commit 8478045
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ type SystemStatus struct {
InsightsError string `json:"insights_error,omitempty"`
YggdrasilRunning bool `json:"yggdrasil_running"`
YggdrasilError string `json:"yggdrasil_error,omitempty"`
returnCode int
}

// printJSONStatus tries to print the system status as JSON to stdout.
Expand Down Expand Up @@ -763,6 +764,10 @@ func statusAction(ctx *cli.Context) (err error) {
fmt.Errorf("unable to print status as %s document: %s", format, err.Error()),
1)
}
// When any of status is not correct, then return 1 exit code
if systemStatus.returnCode != 0 {
err = cli.Exit("", 1)
}
}(&systemStatus)
}

Expand Down Expand Up @@ -800,6 +805,12 @@ func statusAction(ctx *cli.Context) (err error) {
fmt.Printf("\nManage your connected systems: https://red.ht/connector\n")
}

// At the end check if all statuses are correct.
// If not, return 1 exit code without any message.
if systemStatus.returnCode != 0 {
return cli.Exit("", 1)
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func rhsmStatus(systemStatus *SystemStatus) error {
return fmt.Errorf("unable to get consumer UUID: %s", err)
}
if uuid == "" {
systemStatus.returnCode += 1
if uiSettings.isMachineReadable {
systemStatus.RHSMConnected = false
} else {
Expand Down Expand Up @@ -53,6 +54,7 @@ func insightStatus(systemStatus *SystemStatus) {
fmt.Print(uiSettings.iconOK + " Connected to Red Hat Insights\n")
}
} else {
systemStatus.returnCode += 1
if err == nil {
if uiSettings.isMachineReadable {
systemStatus.InsightsConnected = false
Expand Down Expand Up @@ -95,6 +97,7 @@ func serviceStatus(systemStatus *SystemStatus) error {
fmt.Printf(uiSettings.iconOK+" The %v service is active\n", ServiceName)
}
} else {
systemStatus.returnCode += 1
if uiSettings.isMachineReadable {
systemStatus.YggdrasilRunning = false
} else {
Expand Down

0 comments on commit 8478045

Please sign in to comment.