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

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