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

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