github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/internal/errors/resolver/update.go (about)

     1  // Copyright 2021 The kpt Authors
     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  	"fmt"
    19  
    20  	"github.com/GoogleContainerTools/kpt/internal/errors"
    21  	"github.com/GoogleContainerTools/kpt/internal/util/update"
    22  )
    23  
    24  //nolint:gochecknoinits
    25  func init() {
    26  	AddErrorResolver(&updateErrorResolver{})
    27  }
    28  
    29  // updateErrorResolver is an implementation of the ErrorResolver interface
    30  // to resolve update errors.
    31  type updateErrorResolver struct{}
    32  
    33  func (*updateErrorResolver) Resolve(err error) (ResolvedResult, bool) {
    34  	var msg string
    35  
    36  	var pkgNotGitRepoError *update.PkgNotGitRepoError
    37  	if errors.As(err, &pkgNotGitRepoError) {
    38  		//nolint:lll
    39  		msg = fmt.Sprintf("Package %q is not within a git repository.", pkgNotGitRepoError.Path)
    40  		msg += " Please initialize a repository using 'git init' and then commit the changes using 'git commit -m \"<commit message>\"'."
    41  	}
    42  
    43  	var pkgRepoDirtyError *update.PkgRepoDirtyError
    44  	if errors.As(err, &pkgRepoDirtyError) {
    45  		msg = fmt.Sprintf("Package %q contains uncommitted changes.", pkgRepoDirtyError.Path)
    46  		msg += " Please commit the changes using 'git commit -m \"<commit message>\"'."
    47  	}
    48  
    49  	if msg != "" {
    50  		return ResolvedResult{
    51  			Message: msg,
    52  		}, true
    53  	}
    54  	return ResolvedResult{}, false
    55  }