github.com/SamarSidharth/kpt@v0.0.0-20231122062228-c7d747ae3ace/commands/pkg/diff/cmddiff.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 diff 16 17 import ( 18 "context" 19 "os" 20 21 "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs" 22 "github.com/GoogleContainerTools/kpt/internal/pkg" 23 "github.com/GoogleContainerTools/kpt/internal/util/argutil" 24 "github.com/GoogleContainerTools/kpt/internal/util/cmdutil" 25 "github.com/GoogleContainerTools/kpt/internal/util/diff" 26 "github.com/GoogleContainerTools/kpt/internal/util/pathutil" 27 "github.com/GoogleContainerTools/kpt/pkg/printer" 28 "github.com/spf13/cobra" 29 "sigs.k8s.io/kustomize/kyaml/filesys" 30 ) 31 32 // NewRunner returns a command runner. 33 func NewRunner(ctx context.Context, parent string) *Runner { 34 r := &Runner{ 35 ctx: ctx, 36 } 37 c := &cobra.Command{ 38 Use: "diff [PKG_PATH@VERSION] [flags]", 39 Short: pkgdocs.DiffShort, 40 Long: pkgdocs.DiffShort + "\n" + pkgdocs.DiffLong, 41 Example: pkgdocs.DiffExamples, 42 PreRunE: r.preRunE, 43 RunE: r.runE, 44 SilenceUsage: true, 45 } 46 diffTool := "diff" 47 if tool := os.Getenv("KPT_EXTERNAL_DIFF"); tool != "" { 48 diffTool = tool 49 } 50 diffToolOpts := os.Getenv("KPT_EXTERNAL_DIFF_OPTS") 51 c.Flags().StringVar(&r.diffType, "diff-type", "", 52 "diff type you want to perform e.g. "+diff.SupportedDiffTypesLabel()) 53 c.Flags().StringVar(&r.DiffTool, "diff-tool", diffTool, 54 "diff tool to use to show the changes") 55 c.Flags().StringVar(&r.DiffToolOpts, "diff-tool-opts", diffToolOpts, 56 "diff tool commandline options to use to show the changes") 57 c.Flags().BoolVar(&r.Debug, "debug", false, 58 "when true, prints additional debug information and do not delete staged pkg dirs") 59 r.C = c 60 r.Output = printer.FromContextOrDie(r.ctx).OutStream() 61 cmdutil.FixDocs("kpt", parent, c) 62 return r 63 } 64 65 // NewCommand returns a diff command instance. 66 func NewCommand(ctx context.Context, parent string) *cobra.Command { 67 return NewRunner(ctx, parent).C 68 } 69 70 // Runner contains the run function 71 type Runner struct { 72 ctx context.Context 73 diff.Command 74 C *cobra.Command 75 diffType string 76 } 77 78 func (r *Runner) preRunE(_ *cobra.Command, args []string) error { 79 if len(args) == 0 { 80 args = append(args, pkg.CurDir) 81 } 82 dirVer := args[0] 83 dir, version, err := argutil.ParseDirVersion(dirVer) 84 if err != nil { 85 return err 86 } 87 if r.diffType == "" { 88 // pick sensible defaults for diff-type 89 r.DiffType = diff.TypeLocal 90 if version != "" { 91 // if target version is specified, default to 'combined' diff-type. 92 // xref: https://github.com/GoogleContainerTools/kpt/issues/139 93 r.DiffType = diff.TypeCombined 94 } 95 } else { 96 r.DiffType = diff.Type(r.diffType) 97 } 98 99 resolvedPath, err := argutil.ResolveSymlink(r.ctx, dir) 100 if err != nil { 101 return err 102 } 103 104 absResolvedPath, _, err := pathutil.ResolveAbsAndRelPaths(resolvedPath) 105 if err != nil { 106 return err 107 } 108 109 p, err := pkg.New(filesys.FileSystemOrOnDisk{}, absResolvedPath) 110 if err != nil { 111 return err 112 } 113 r.Path = string(p.UniquePath) 114 r.Ref = version 115 r.Output = printer.FromContextOrDie(r.ctx).OutStream() 116 117 return r.Validate() 118 } 119 120 func (r *Runner) runE(_ *cobra.Command, _ []string) error { 121 return r.Run(r.ctx) 122 }