Skip to content

Commit

Permalink
Refactor git cmd and error parser
Browse files Browse the repository at this point in the history
Reduce cyclic complexity in git_error_parser
  • Loading branch information
Baran Dalgic committed Oct 15, 2021
1 parent 8290272 commit f63cf05
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 96 deletions.
29 changes: 21 additions & 8 deletions cmd/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ func (e ExitError) Error() string {

type settings struct {
help bool
skipValidation bool
depth uint
url string
revision string
depth uint
target string
resultFileCommitSha string
resultFileCommitAuthor string
resultErrorMessage string
resultErrorReason string
secretPath string
skipValidation bool
}

var flagValues settings
Expand Down Expand Up @@ -112,6 +112,7 @@ func main() {
// Execute performs flag parsing, input validation and the Git clone
func Execute(ctx context.Context) error {
flagValues = settings{depth: 1}

pflag.Parse()

if flagValues.help {
Expand Down Expand Up @@ -419,10 +420,18 @@ func checkCredentials() (credentialType, error) {
return typePrivateKey, nil

case hasPrivateKey && !isSSHGitURL:
return typeUndef, &ExitError{Code: 110, Message: "Credential/URL inconsistency: SSH credentials provided, but URL is not a SSH Git URL", Reason: shpgit.AuthInvalidUserOrPass}
return typeUndef, &ExitError{
Code: 110,
Message: "Credential/URL inconsistency: SSH credentials provided, but URL is not a SSH Git URL",
Reason: shpgit.AuthInvalidUserOrPass,
}

case !hasPrivateKey && isSSHGitURL:
return typeUndef, &ExitError{Code: 110, Message: "Credential/URL inconsistency: No SSH credentials provided, but URL is a SSH Git URL", Reason: shpgit.AuthInvalidKey}
return typeUndef, &ExitError{
Code: 110,
Message: "Credential/URL inconsistency: No SSH credentials provided, but URL is a SSH Git URL",
Reason: shpgit.AuthInvalidKey,
}
}

// Checking whether mounted secret is of type `kubernetes.io/basic-auth`
Expand All @@ -435,8 +444,11 @@ func checkCredentials() (credentialType, error) {
return typeUsernamePassword, nil

case hasUsername && !hasPassword || !hasUsername && hasPassword:
return typeUndef, &ExitError{Code: 110, Message: "Basic Auth incomplete: Both username and password need to be configured", Reason: shpgit.AuthInvalidUserOrPass}

return typeUndef, &ExitError{
Code: 110,
Message: "Basic Auth incomplete: Both username and password need to be configured",
Reason: shpgit.AuthInvalidUserOrPass,
}
}

return typeUndef, &ExitError{Code: 110, Message: "Unsupported type of credentials provided, either SSH private key or username/password is supported"}
Expand All @@ -447,10 +459,11 @@ func writeErrorResults(failure *shpgit.ErrorResult) (err error) {
return nil
}

if err = ioutil.WriteFile(flagValues.resultErrorMessage, []byte(strings.TrimSpace(failure.Message)), 0644); err != nil {
if err = ioutil.WriteFile(flagValues.resultErrorMessage, []byte(strings.TrimSpace(failure.Message)), 0600); err != nil {
return err
}
if err = ioutil.WriteFile(flagValues.resultErrorReason, []byte(failure.Reason.String()), 0644); err != nil {

if err = ioutil.WriteFile(flagValues.resultErrorReason, []byte(failure.Reason.String()), 0600); err != nil {
return err
}

Expand Down
Loading

0 comments on commit f63cf05

Please sign in to comment.