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

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