github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/pkg/get/cmdget.go (about)

     1  // Copyright 2019 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 get
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"strings"
    21  
    22  	docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
    23  	"github.com/GoogleContainerTools/kpt/internal/errors"
    24  	"github.com/GoogleContainerTools/kpt/internal/pkg"
    25  	"github.com/GoogleContainerTools/kpt/internal/types"
    26  	"github.com/GoogleContainerTools/kpt/internal/util/argutil"
    27  	"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
    28  	"github.com/GoogleContainerTools/kpt/internal/util/get"
    29  	"github.com/GoogleContainerTools/kpt/internal/util/parse"
    30  	"github.com/GoogleContainerTools/kpt/internal/util/pathutil"
    31  	kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
    32  	"github.com/spf13/cobra"
    33  	"sigs.k8s.io/kustomize/kyaml/filesys"
    34  )
    35  
    36  // NewRunner returns a command runner
    37  func NewRunner(ctx context.Context, parent string) *Runner {
    38  	r := &Runner{
    39  		ctx: ctx,
    40  	}
    41  	c := &cobra.Command{
    42  		Use:        "get REPO_URI[.git]/PKG_PATH[@VERSION] [LOCAL_DEST_DIRECTORY]",
    43  		Args:       cobra.MinimumNArgs(1),
    44  		Short:      docs.GetShort,
    45  		Long:       docs.GetShort + "\n" + docs.GetLong,
    46  		Example:    docs.GetExamples,
    47  		RunE:       r.runE,
    48  		PreRunE:    r.preRunE,
    49  		SuggestFor: []string{"clone", "cp", "fetch"},
    50  	}
    51  	cmdutil.FixDocs("kpt", parent, c)
    52  	r.Command = c
    53  	c.Flags().StringVar(&r.strategy, "strategy", string(kptfilev1.ResourceMerge),
    54  		"update strategy that should be used when updating this package -- must be one of: "+
    55  			strings.Join(kptfilev1.UpdateStrategiesAsStrings(), ","))
    56  	c.Flags().BoolVar(&r.isDeploymentInstance, "for-deployment", false,
    57  		"(Experimental) indicates if this package will be deployed to a cluster.")
    58  	_ = c.RegisterFlagCompletionFunc("strategy", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    59  		return kptfilev1.UpdateStrategiesAsStrings(), cobra.ShellCompDirectiveDefault
    60  	})
    61  	return r
    62  }
    63  
    64  func NewCommand(ctx context.Context, parent string) *cobra.Command {
    65  	return NewRunner(ctx, parent).Command
    66  }
    67  
    68  // Runner contains the run function
    69  type Runner struct {
    70  	ctx                  context.Context
    71  	Get                  get.Command
    72  	Command              *cobra.Command
    73  	strategy             string
    74  	isDeploymentInstance bool
    75  }
    76  
    77  func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
    78  	const op errors.Op = "cmdget.preRunE"
    79  	if len(args) == 1 {
    80  		args = append(args, pkg.CurDir)
    81  	} else {
    82  		_, err := os.Lstat(args[1])
    83  		if err == nil || os.IsExist(err) {
    84  			resolvedPath, err := argutil.ResolveSymlink(r.ctx, args[1])
    85  			if err != nil {
    86  				return errors.E(op, err)
    87  			}
    88  			args[1] = resolvedPath
    89  		}
    90  	}
    91  	t, err := parse.GitParseArgs(r.ctx, args)
    92  	if err != nil {
    93  		return errors.E(op, err)
    94  	}
    95  
    96  	r.Get.Git = &t.Git
    97  	absDestPath, _, err := pathutil.ResolveAbsAndRelPaths(t.Destination)
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	p, err := pkg.New(filesys.FileSystemOrOnDisk{}, absDestPath)
   103  	if err != nil {
   104  		return errors.E(op, types.UniquePath(t.Destination), err)
   105  	}
   106  	r.Get.Destination = string(p.UniquePath)
   107  
   108  	strategy, err := kptfilev1.ToUpdateStrategy(r.strategy)
   109  	if err != nil {
   110  		return err
   111  	}
   112  	r.Get.UpdateStrategy = strategy
   113  	r.Get.IsDeploymentInstance = r.isDeploymentInstance
   114  	return nil
   115  }
   116  
   117  func (r *Runner) runE(_ *cobra.Command, _ []string) error {
   118  	const op errors.Op = "cmdget.runE"
   119  	if err := r.Get.Run(r.ctx); err != nil {
   120  		return errors.E(op, types.UniquePath(r.Get.Destination), err)
   121  	}
   122  
   123  	return nil
   124  }