github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/update/replace.go (about)

     1  // Copyright 2019 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 update
    16  
    17  import (
    18  	"os"
    19  	"path/filepath"
    20  
    21  	"github.com/GoogleContainerTools/kpt/internal/errors"
    22  	"github.com/GoogleContainerTools/kpt/internal/pkg"
    23  	"github.com/GoogleContainerTools/kpt/internal/types"
    24  	"github.com/GoogleContainerTools/kpt/internal/util/pkgutil"
    25  )
    26  
    27  // Updater updates a package to a new upstream version.
    28  //
    29  // If the package at pkgPath differs from the upstream ref it was fetch from, then Update will
    30  // delete the local package.  This will wipe all local changes.
    31  type ReplaceUpdater struct{}
    32  
    33  func (u ReplaceUpdater) Update(options Options) error {
    34  	const op errors.Op = "update.Update"
    35  	paths, err := pkgutil.FindSubpackagesForPaths(pkg.Local, true, options.LocalPath, options.UpdatedPath)
    36  	if err != nil {
    37  		return errors.E(op, types.UniquePath(options.LocalPath), err)
    38  	}
    39  
    40  	for _, p := range append([]string{"."}, paths...) {
    41  		isRootPkg := false
    42  		if p == "." && options.IsRoot {
    43  			isRootPkg = true
    44  		}
    45  		localSubPkgPath := filepath.Join(options.LocalPath, p)
    46  		updatedSubPkgPath := filepath.Join(options.UpdatedPath, p)
    47  		err = pkgutil.RemovePackageContent(localSubPkgPath, !isRootPkg)
    48  		if err != nil {
    49  			return errors.E(op, types.UniquePath(localSubPkgPath), err)
    50  		}
    51  
    52  		// If the package doesn't exist in updated, we make sure it is
    53  		// deleted from the local package. If it exists in updated, we copy
    54  		// the content of the package into local.
    55  		_, err = os.Stat(updatedSubPkgPath)
    56  		if err != nil && !os.IsNotExist(err) {
    57  			return errors.E(op, types.UniquePath(localSubPkgPath), err)
    58  		}
    59  		if os.IsNotExist(err) {
    60  			if err = os.RemoveAll(localSubPkgPath); err != nil {
    61  				return errors.E(op, types.UniquePath(localSubPkgPath), err)
    62  			}
    63  		} else {
    64  			if err = pkgutil.CopyPackage(updatedSubPkgPath, localSubPkgPath, !isRootPkg, pkg.None); err != nil {
    65  				return errors.E(op, types.UniquePath(localSubPkgPath), err)
    66  			}
    67  		}
    68  	}
    69  	return nil
    70  }