github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/errors/resolver/git.go (about)

     1  // Copyright 2021 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package resolver
    16  
    17  import (
    18  	goerrors "errors"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/GoogleContainerTools/kpt/internal/gitutil"
    23  )
    24  
    25  //nolint:gochecknoinits
    26  func init() {
    27  	AddErrorResolver(&gitExecErrorResolver{})
    28  }
    29  
    30  const (
    31  	genericGitExecError = `
    32  Error: Failed to execute git command {{ printf "%q " .gitcmd }}
    33  {{- if gt (len .repo) 0 -}}
    34  against repo {{ printf "%q " .repo }}
    35  {{- end }}
    36  {{- if gt (len .ref) 0 -}}
    37  for reference {{ printf "%q " .ref }}
    38  {{- end }}
    39  
    40  {{- template "ExecOutputDetails" . }}
    41  `
    42  
    43  	unknownRefGitExecError = `
    44  Error: Unknown ref {{ printf "%q" .ref }}. Please verify that the reference exists in upstream repo {{ printf "%q" .repo }}.
    45  
    46  {{- template "ExecOutputDetails" . }}
    47  `
    48  
    49  	noGitBinaryError = `
    50  Error: No git executable found. kpt requires git to be installed and available in the path.
    51  
    52  {{- template "ExecOutputDetails" . }}
    53  `
    54  
    55  	httpsAuthRequired = `
    56  Error: Repository {{ printf "%q" .repo }} requires authentication. kpt does not support this for the 'https' protocol. Please use the 'git' protocol instead.
    57  
    58  {{- template "ExecOutputDetails" . }}
    59  `
    60  
    61  	repositoryUnavailable = `
    62  Error: Unable to access repository {{ printf "%q" .repo }}.
    63  
    64  {{- template "ExecOutputDetails" . }}
    65  `
    66  
    67  	repositoryNotFound = `
    68  Error: Repository {{ printf "%q" .repo }} not found.
    69  
    70  {{- template "ExecOutputDetails" . }}
    71  `
    72  )
    73  
    74  // gitExecErrorResolver is an implementation of the ErrorResolver interface
    75  // that can produce error messages for errors of the gitutil.GitExecError type.
    76  type gitExecErrorResolver struct{}
    77  
    78  func (*gitExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
    79  	var gitExecErr *gitutil.GitExecError
    80  	if !goerrors.As(err, &gitExecErr) {
    81  		return ResolvedResult{}, false
    82  	}
    83  	fullCommand := fmt.Sprintf("git %s %s", gitExecErr.Command,
    84  		strings.Join(gitExecErr.Args, " "))
    85  	tmplArgs := map[string]interface{}{
    86  		"gitcmd": fullCommand,
    87  		"repo":   gitExecErr.Repo,
    88  		"ref":    gitExecErr.Ref,
    89  		"stdout": gitExecErr.StdOut,
    90  		"stderr": gitExecErr.StdErr,
    91  	}
    92  	var msg string
    93  	switch gitExecErr.Type {
    94  	case gitutil.UnknownReference:
    95  		msg = ExecuteTemplate(unknownRefGitExecError, tmplArgs)
    96  	case gitutil.GitExecutableNotFound:
    97  		msg = ExecuteTemplate(noGitBinaryError, tmplArgs)
    98  	case gitutil.HTTPSAuthRequired:
    99  		msg = ExecuteTemplate(httpsAuthRequired, tmplArgs)
   100  	case gitutil.RepositoryUnavailable:
   101  		msg = ExecuteTemplate(repositoryUnavailable, tmplArgs)
   102  	case gitutil.RepositoryNotFound:
   103  		msg = ExecuteTemplate(repositoryNotFound, tmplArgs)
   104  	default:
   105  		msg = ExecuteTemplate(genericGitExecError, tmplArgs)
   106  	}
   107  	return ResolvedResult{
   108  		Message: msg,
   109  	}, true
   110  }