github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/internal/util/fix/fix.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 fix
    16  
    17  import (
    18  	"fmt"
    19  	"io"
    20  	"path/filepath"
    21  
    22  	"sigs.k8s.io/kustomize/kyaml/fix/fixsetters"
    23  )
    24  
    25  // Command fixes the local kpt package and upgrades it to use latest feature
    26  type Command struct {
    27  	// PkgPath path to the kpt package directory
    28  	PkgPath string
    29  
    30  	// DryRun indicates that only preview of actions should be printed without
    31  	// performing actual actions
    32  	DryRun bool
    33  
    34  	// StdOut standard out to write messages to
    35  	StdOut io.Writer
    36  }
    37  
    38  // Run runs the Command.
    39  func (c Command) Run() error {
    40  	printFunc := printFunc(c.StdOut, c.DryRun)
    41  	printFunc("processing resource configs to identify possible fixes... ")
    42  	return c.fixV1Setters()
    43  }
    44  
    45  func (c Command) fixV1Setters() error {
    46  	printFunc := printFunc(c.StdOut, c.DryRun)
    47  	f := &fixsetters.SetterFixer{
    48  		PkgPath:     c.PkgPath,
    49  		OpenAPIPath: filepath.Join(c.PkgPath, "Kptfile"),
    50  		DryRun:      c.DryRun,
    51  	}
    52  	sfr, err := f.FixV1Setters()
    53  	if err != nil {
    54  		return err
    55  	}
    56  	if !sfr.NeedFix {
    57  		printFunc("package is using latest version of setters, no fix needed")
    58  		return nil
    59  	}
    60  
    61  	for _, setter := range sfr.CreatedSetters {
    62  		printFunc("created setter with name %s", setter)
    63  	}
    64  	printFunc("created %d setters in total", len(sfr.CreatedSetters))
    65  
    66  	for _, subst := range sfr.CreatedSubst {
    67  		printFunc("created substitution with name %s", subst)
    68  	}
    69  	printFunc("created %d substitution in total", len(sfr.CreatedSubst))
    70  
    71  	for setter, err := range sfr.FailedSetters {
    72  		printFunc("failed to create setter with name %s: %v", setter, err)
    73  	}
    74  
    75  	for subst, err := range sfr.FailedSubst {
    76  		printFunc("failed to create substitution with name %s: %v", subst, err)
    77  	}
    78  
    79  	return err
    80  }
    81  
    82  type printerFunc func(format string, a ...interface{})
    83  
    84  func printFunc(w io.Writer, dryRun bool) printerFunc {
    85  	return func(format string, a ...interface{}) {
    86  		if dryRun {
    87  			format += " (dry-run)"
    88  		}
    89  		fmt.Fprintf(w, format+"\n", a...)
    90  	}
    91  }