From e40e47530bc799a32b7e6632a4b27367601a1917 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Mon, 10 Jun 2024 10:36:45 +0200 Subject: [PATCH] refactor: remove use of message.Fatal in wait command --- src/cmd/tools/wait.go | 8 +++++--- src/config/lang/english.go | 8 ++------ src/pkg/utils/wait.go | 22 ++++++++++++---------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/cmd/tools/wait.go b/src/cmd/tools/wait.go index 9977c58011..bb767ec972 100644 --- a/src/cmd/tools/wait.go +++ b/src/cmd/tools/wait.go @@ -5,6 +5,7 @@ package tools import ( + "fmt" "time" "github.com/defenseunicorns/zarf/src/config/lang" @@ -28,11 +29,11 @@ var waitForCmd = &cobra.Command{ Long: lang.CmdToolsWaitForLong, Example: lang.CmdToolsWaitForExample, Args: cobra.MinimumNArgs(1), - Run: func(_ *cobra.Command, args []string) { + RunE: func(_ *cobra.Command, args []string) error { // Parse the timeout string timeout, err := time.ParseDuration(waitTimeout) if err != nil { - message.Fatalf(err, lang.CmdToolsWaitForErrTimeoutString, waitTimeout) + return fmt.Errorf("invalid timeout duration %s, use a valid duration string e.g. 1s, 2m, 3h: %w", waitTimeout, err) } kind := args[0] @@ -51,8 +52,9 @@ var waitForCmd = &cobra.Command{ // Execute the wait command. if err := utils.ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier, timeout); err != nil { - message.Fatal(err, err.Error()) + return err } + return err }, } diff --git a/src/config/lang/english.go b/src/config/lang/english.go index 36112f99a9..35017ac758 100644 --- a/src/config/lang/english.go +++ b/src/config/lang/english.go @@ -580,12 +580,8 @@ $ zarf tools wait-for https 1.1.1.1 200 # wait $ zarf tools wait-for http google.com # wait for any 2xx response from http://google.com $ zarf tools wait-for http google.com success # wait for any 2xx response from http://google.com ` - CmdToolsWaitForFlagTimeout = "Specify the timeout duration for the wait command." - CmdToolsWaitForErrTimeoutString = "Invalid timeout duration '%s'. Please use a valid duration string (e.g. 1s, 2m, 3h)." - CmdToolsWaitForErrTimeout = "Wait timed out." - CmdToolsWaitForErrConditionString = "Invalid HTTP status code. Please use a valid HTTP status code (e.g. 200, 404, 500)." - CmdToolsWaitForErrZarfPath = "Could not locate the current Zarf binary path." - CmdToolsWaitForFlagNamespace = "Specify the namespace of the resources to wait for." + CmdToolsWaitForFlagTimeout = "Specify the timeout duration for the wait command." + CmdToolsWaitForFlagNamespace = "Specify the namespace of the resources to wait for." CmdToolsKubectlDocs = "Kubectl command. See https://kubernetes.io/docs/reference/kubectl/overview/ for more information." diff --git a/src/pkg/utils/wait.go b/src/pkg/utils/wait.go index a4815e5ea5..4ccc3d2481 100644 --- a/src/pkg/utils/wait.go +++ b/src/pkg/utils/wait.go @@ -5,6 +5,7 @@ package utils import ( + "errors" "fmt" "net" "net/http" @@ -15,7 +16,6 @@ import ( "github.com/defenseunicorns/zarf/src/pkg/utils/exec" - "github.com/defenseunicorns/zarf/src/config/lang" "github.com/defenseunicorns/zarf/src/pkg/message" ) @@ -33,8 +33,7 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, // Handle network endpoints. switch kind { case "http", "https", "tcp": - waitForNetworkEndpoint(kind, identifier, condition, timeout) - return nil + return waitForNetworkEndpoint(kind, identifier, condition, timeout) } // Type of wait, condition or JSONPath @@ -50,7 +49,7 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, // Get the Zarf command configuration. zarfCommand, err := GetFinalExecutableCommand() if err != nil { - message.Fatal(err, lang.CmdToolsWaitForErrZarfPath) + return fmt.Errorf("could not locate the current Zarf binary path: %w", err) } identifierMsg := identifier @@ -88,7 +87,7 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, select { case <-expired: - message.Fatal(nil, lang.CmdToolsWaitForErrTimeout) + return errors.New("wait timed out") default: spinner.Updatef(existMsg) @@ -132,7 +131,7 @@ func ExecuteWait(waitTimeout, waitNamespace, condition, kind, identifier string, } // waitForNetworkEndpoint waits for a network endpoint to respond. -func waitForNetworkEndpoint(resource, name, condition string, timeout time.Duration) { +func waitForNetworkEndpoint(resource, name, condition string, timeout time.Duration) error { // Set the timeout for the wait-for command. expired := time.After(timeout) @@ -153,7 +152,7 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat select { case <-expired: - message.Fatal(nil, lang.CmdToolsWaitForErrTimeout) + return errors.New("wait timed out") default: switch resource { @@ -179,8 +178,11 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat // Convert the condition to an int and check if it's a valid HTTP status code. code, err := strconv.Atoi(condition) - if err != nil || http.StatusText(code) == "" { - message.Fatal(err, lang.CmdToolsWaitForErrConditionString) + if err != nil { + return fmt.Errorf("http status code %s is not an integer: %w", condition, err) + } + if http.StatusText(code) == "" { + return errors.New("http status code %s is unknown") } // Try to get the URL and check the status code. @@ -202,7 +204,7 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat // Yay, we made it! spinner.Success() - return + return nil } } }