Skip to content

Commit

Permalink
move trimming whitespace to error-check helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jan 9, 2024
1 parent 8396edb commit 1b299d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
15 changes: 4 additions & 11 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ func isValidCredsMessage(msg string) error {
if credentials.IsCredentialsMissingServerURLMessage(msg) {
return credentials.NewErrCredentialsMissingServerURL()
}

if credentials.IsCredentialsMissingUsernameMessage(msg) {
return credentials.NewErrCredentialsMissingUsername()
}

return nil
}

Expand All @@ -36,13 +34,10 @@ func Store(program ProgramFunc, creds *credentials.Credentials) error {

out, err := cmd.Output()
if err != nil {
t := strings.TrimSpace(string(out))

if isValidErr := isValidCredsMessage(t); isValidErr != nil {
if isValidErr := isValidCredsMessage(string(out)); isValidErr != nil {
err = isValidErr
}

return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, t)
return fmt.Errorf("error storing credentials - err: %v, out: `%s`", err, strings.TrimSpace(string(out)))
}

return nil
Expand All @@ -55,13 +50,11 @@ func Get(program ProgramFunc, serverURL string) (*credentials.Credentials, error

out, err := cmd.Output()
if err != nil {
t := strings.TrimSpace(string(out))

if credentials.IsErrCredentialsNotFoundMessage(t) {
if credentials.IsErrCredentialsNotFoundMessage(string(out)) {
return nil, credentials.NewErrCredentialsNotFound()
}

if isValidErr := isValidCredsMessage(t); isValidErr != nil {
if isValidErr := isValidCredsMessage(string(out)); isValidErr != nil {
err = isValidErr
}

Expand Down
11 changes: 7 additions & 4 deletions credentials/error.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package credentials

import "errors"
import (
"errors"
"strings"
)

const (
// ErrCredentialsNotFound standardizes the not found error, so every helper returns
Expand Down Expand Up @@ -47,7 +50,7 @@ func IsErrCredentialsNotFound(err error) bool {
// This function helps to check messages returned by an
// external program via its standard output.
func IsErrCredentialsNotFoundMessage(err string) bool {
return err == errCredentialsNotFoundMessage
return strings.TrimSpace(err) == errCredentialsNotFoundMessage
}

// errCredentialsMissingServerURL represents an error raised
Expand Down Expand Up @@ -104,7 +107,7 @@ func IsCredentialsMissingServerURL(err error) bool {
// IsCredentialsMissingServerURLMessage checks for an
// errCredentialsMissingServerURL in the error message.
func IsCredentialsMissingServerURLMessage(err string) bool {
return err == errCredentialsMissingServerURLMessage
return strings.TrimSpace(err) == errCredentialsMissingServerURLMessage
}

// IsCredentialsMissingUsername returns true if the error
Expand All @@ -117,5 +120,5 @@ func IsCredentialsMissingUsername(err error) bool {
// IsCredentialsMissingUsernameMessage checks for an
// errCredentialsMissingUsername in the error message.
func IsCredentialsMissingUsernameMessage(err string) bool {
return err == errCredentialsMissingUsernameMessage
return strings.TrimSpace(err) == errCredentialsMissingUsernameMessage
}

0 comments on commit 1b299d3

Please sign in to comment.