github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/errors/resolver/update.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  	"github.com/GoogleContainerTools/kpt/internal/errors"
    19  	"github.com/GoogleContainerTools/kpt/internal/util/update"
    20  )
    21  
    22  //nolint:gochecknoinits
    23  func init() {
    24  	AddErrorResolver(&updateErrorResolver{})
    25  }
    26  
    27  var (
    28  	//nolint:lll
    29  	pkgNotGitRepo = `
    30  Package {{ printf "%q" .repo }} is not within a git repository. Please initialize a repository using 'git init' and then commit the changes using 'git commit -m "<commit message>"'.
    31  `
    32  
    33  	pkgRepoDirty = `
    34  Package {{ printf "%q" .repo }} contains uncommitted changes. Please commit the changes using 'git commit -m "<commit message>"'.
    35  `
    36  )
    37  
    38  // updateErrorResolver is an implementation of the ErrorResolver interface
    39  // to resolve update errors.
    40  type updateErrorResolver struct{}
    41  
    42  func (*updateErrorResolver) Resolve(err error) (ResolvedResult, bool) {
    43  	var msg string
    44  
    45  	var pkgNotGitRepoError *update.PkgNotGitRepoError
    46  	if errors.As(err, &pkgNotGitRepoError) {
    47  		msg = ExecuteTemplate(pkgNotGitRepo, map[string]interface{}{
    48  			"repo": pkgNotGitRepoError.Path,
    49  		})
    50  	}
    51  
    52  	var pkgRepoDirtyError *update.PkgRepoDirtyError
    53  	if errors.As(err, &pkgRepoDirtyError) {
    54  		msg = ExecuteTemplate(pkgRepoDirty, map[string]interface{}{
    55  			"repo": pkgRepoDirtyError.Path,
    56  		})
    57  	}
    58  
    59  	if msg != "" {
    60  		return ResolvedResult{
    61  			Message: msg,
    62  		}, true
    63  	}
    64  	return ResolvedResult{}, false
    65  }