Blame SOURCES/0003-fix-collect-error-messages-during-disconnect.patch

696de9
From 0e3ce2489f92cc037936866a1d6d7901fb14d440 Mon Sep 17 00:00:00 2001
696de9
From: Link Dupont <link@sub-pop.net>
696de9
Date: Mon, 14 Feb 2022 15:15:54 -0500
696de9
Subject: [PATCH] fix: collect error messages during disconnect
696de9
696de9
If an error occurs during disconnect, the error is collected and
696de9
reported at the end of the operation instead of aborting the operation
696de9
part-way through.
696de9
696de9
Fixes: ESSNTL-2281
696de9
---
696de9
 main.go | 42 +++++++++++++++++++++++++++++++++---------
696de9
 1 file changed, 33 insertions(+), 9 deletions(-)
696de9
696de9
diff --git a/main.go b/main.go
696de9
index 0e6cc07..db5a34d 100644
696de9
--- a/main.go
696de9
+++ b/main.go
696de9
@@ -6,6 +6,7 @@ import (
696de9
 	"fmt"
696de9
 	"os"
696de9
 	"strings"
696de9
+	"text/tabwriter"
696de9
 	"time"
696de9
 
696de9
 	"git.sr.ht/~spc/go-log"
696de9
@@ -18,6 +19,7 @@ import (
696de9
 
696de9
 const successPrefix = "\033[32m●\033[0m"
696de9
 const failPrefix = "\033[31m●\033[0m"
696de9
+const errorPrefix = "\033[31m!\033[0m"
696de9
 
696de9
 func main() {
696de9
 	app := cli.NewApp()
696de9
@@ -169,6 +171,7 @@ func main() {
696de9
 			UsageText:   fmt.Sprintf("%v disconnect", app.Name),
696de9
 			Description: fmt.Sprintf("The disconnect command disconnects the system from Red Hat Subscription Management, Red Hat Insights and %v and deactivates the %v daemon. %v will no longer be able to interact with the system.", Provider, BrandName, Provider),
696de9
 			Action: func(c *cli.Context) error {
696de9
+				errorMessages := make(map[string]error)
696de9
 				hostname, err := os.Hostname()
696de9
 				if err != nil {
696de9
 					return cli.Exit(err, 1)
696de9
@@ -180,29 +183,50 @@ func main() {
696de9
 				s.Suffix = fmt.Sprintf(" Deactivating the %v daemon", BrandName)
696de9
 				s.Start()
696de9
 				if err := deactivate(); err != nil {
696de9
-					return cli.Exit(err, 1)
696de9
+					errorMessages[BrandName] = fmt.Errorf("cannot deactivate daemon: %w", err)
696de9
+					s.Stop()
696de9
+					fmt.Printf(errorPrefix+" Cannot deactivate the %v daemon\n", BrandName)
696de9
+				} else {
696de9
+					s.Stop()
696de9
+					fmt.Printf(failPrefix+" Deactivated the %v daemon\n", BrandName)
696de9
 				}
696de9
-				s.Stop()
696de9
-				fmt.Printf(failPrefix+" Deactivated the %v daemon\n", BrandName)
696de9
 
696de9
 				s.Suffix = " Disconnecting from Red Hat Insights..."
696de9
 				s.Start()
696de9
 				if err := unregisterInsights(); err != nil {
696de9
-					return cli.Exit(err, 1)
696de9
+					errorMessages["insights"] = fmt.Errorf("cannot disconnect from Red Hat Insights: %w", err)
696de9
+					s.Stop()
696de9
+					fmt.Printf(errorPrefix + " Cannot disconnect from Red Hat Insights\n")
696de9
+				} else {
696de9
+					s.Stop()
696de9
+					fmt.Print(failPrefix + " Disconnected from Red Hat Insights\n")
696de9
 				}
696de9
-				s.Stop()
696de9
-				fmt.Print(failPrefix + " Disconnected from Red Hat Insights\n")
696de9
 
696de9
 				s.Suffix = " Disconnecting from Red Hat Subscription Management..."
696de9
 				s.Start()
696de9
 				if err := unregister(); err != nil {
696de9
-					return cli.Exit(err, 1)
696de9
+					errorMessages["rhsm"] = fmt.Errorf("cannot disconnect from Red Hat Subscription Management: %w", err)
696de9
+					s.Stop()
696de9
+					fmt.Printf(errorPrefix + " Cannot disconnect from Red Hat Subscription Management\n")
696de9
+				} else {
696de9
+					s.Stop()
696de9
+					fmt.Printf(failPrefix + " Disconnected from Red Hat Subscription Management\n")
696de9
 				}
696de9
-				s.Stop()
696de9
-				fmt.Printf(failPrefix + " Disconnected from Red Hat Subscription Management\n")
696de9
 
696de9
 				fmt.Printf("\nManage your Red Hat connector systems: https://red.ht/connector\n")
696de9
 
696de9
+				if len(errorMessages) > 0 {
696de9
+					fmt.Println()
696de9
+					fmt.Printf("The following errors were encountered during disconnect:\n\n")
696de9
+					w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
696de9
+					fmt.Fprintln(w, "STEP\tERROR\t")
696de9
+					for svc, err := range errorMessages {
696de9
+						fmt.Fprintf(w, "%v\t%v\n", svc, err)
696de9
+					}
696de9
+					w.Flush()
696de9
+					return cli.Exit("", 1)
696de9
+				}
696de9
+
696de9
 				return nil
696de9
 			},
696de9
 		},