Skip to content

Commit

Permalink
refactor: remove use of message.Fatal in wait command
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Jun 10, 2024
1 parent b2b504b commit e40e475
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
8 changes: 5 additions & 3 deletions src/cmd/tools/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package tools

import (
"fmt"
"time"

"github.com/defenseunicorns/zarf/src/config/lang"
Expand All @@ -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]
Expand All @@ -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
},
}

Expand Down
8 changes: 2 additions & 6 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down
22 changes: 12 additions & 10 deletions src/pkg/utils/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package utils

import (
"errors"
"fmt"
"net"
"net/http"
Expand All @@ -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"
)

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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 {
Expand All @@ -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.
Expand All @@ -202,7 +204,7 @@ func waitForNetworkEndpoint(resource, name, condition string, timeout time.Durat

// Yay, we made it!
spinner.Success()
return
return nil
}
}
}

0 comments on commit e40e475

Please sign in to comment.