Debian Codegraph @

cmd/config/runner/runner.go

raw symbols SCIP
1 // Copyright 2019 The Kubernetes Authors.
2 // SPDX-License-Identifier: Apache-2.0
3 package runner
4
5 import (
6 "fmt"
7 "io"
8 "os"
9 "strings"
10
11 "github.com/go-errors/errors"
12 "github.com/spf13/cobra"
13 "sigs.k8s.io/kustomize/cmd/config/ext"
14 "sigs.k8s.io/kustomize/kyaml/pathutil"
15 )
16
17 // CmdRunner interface holds ExecuteCmd definition which executes respective command's
18 // implementation on single package
19 type CmdRunner interface {
20 ExecuteCmd(w io.Writer, pkgPath string) error
21 }
22
23 // ExecuteCmdOnPkgs struct holds the parameters necessary to
24 // execute the filter command on packages in rootPkgPath
25 type ExecuteCmdOnPkgs struct {
26 RootPkgPath string
27 RecurseSubPackages bool
28 NeedOpenAPI bool
29 CmdRunner CmdRunner
30 Writer io.Writer
31 SkipPkgPathPrint bool
32 }
33
34 // ExecuteCmdOnPkgs takes the function definition for a command to be executed on single package, applies that definition
35 // recursively on all the subpackages present in rootPkgPath if recurseSubPackages is true, else applies the command on rootPkgPath only
36 func (e ExecuteCmdOnPkgs) Execute() error {
37 pkgsPaths, err := pathutil.DirsWithFile(e.RootPkgPath, ext.KRMFileName(), e.RecurseSubPackages)
38 if err != nil {
39 return err
40 }
41
42 if len(pkgsPaths) == 0 {
43 // at this point, there are no openAPI files in the rootPkgPath
44 if e.NeedOpenAPI {
45 // few executions need openAPI file to be present(ex: setters commands), if true throw an error
46 return errors.Errorf("unable to find %q in package %q", ext.KRMFileName(), e.RootPkgPath)
47 }
48
49 // add root path for commands which doesn't need openAPI(ex: annotate, fmt)
50 pkgsPaths = []string{e.RootPkgPath}
51 }
52
53 // for commands which doesn't need openAPI file, make sure that the root package is
54 // included all the times
55 if !e.NeedOpenAPI && !containsString(pkgsPaths, e.RootPkgPath) {
56 pkgsPaths = append([]string{e.RootPkgPath}, pkgsPaths...)
57 }
58
59 for i := range pkgsPaths {
60 err := e.processPkg(pkgsPaths[i])
61 if err != nil {
62 return err
63 }
64 if i != len(pkgsPaths)-1 {
65 fmt.Fprint(e.Writer, "\n")
66 }
67 }
68 return nil
69 }
70
71 func (e ExecuteCmdOnPkgs) processPkg(pkgPath string) error {
72 // Add schema present in openAPI file for current package
73 if !e.SkipPkgPathPrint {
74 fmt.Fprintf(e.Writer, "%s/\n", pkgPath)
75 }
76
77 return e.CmdRunner.ExecuteCmd(e.Writer, pkgPath)
78 }
79
80 // ParseFieldPath parse a flag value into a field path
81 func ParseFieldPath(path string) ([]string, error) {
82 // fixup '\.' so we don't split on it
83 match := strings.ReplaceAll(path, "\\.", "$$$$")
84 parts := strings.Split(match, ".")
85 for i := range parts {
86 parts[i] = strings.ReplaceAll(parts[i], "$$$$", ".")
87 }
88
89 // split the list index from the list field
90 var newParts []string
91 for i := range parts {
92 if !strings.Contains(parts[i], "[") {
93 newParts = append(newParts, parts[i])
94 continue
95 }
96 p := strings.Split(parts[i], "[")
97 if len(p) != 2 {
98 return nil, fmt.Errorf("unrecognized path element: %s. "+
99 "Should be of the form 'list[field=value]'", parts[i])
100 }
101 p[1] = "[" + p[1]
102 newParts = append(newParts, p[0], p[1])
103 }
104 return newParts, nil
105 }
106
107 func HandleError(c *cobra.Command, err error) error {
108 if err == nil {
109 return nil
110 }
111 if StackOnError {
112 if err, ok := err.(*errors.Error); ok {
113 fmt.Fprintf(os.Stderr, "%s", err.Stack())
114 }
115 }
116
117 if ExitOnError {
118 fmt.Fprintf(c.ErrOrStderr(), "Error: %v\n", err)
119 os.Exit(1)
120 }
121 return err
122 }
123
124 // ExitOnError if true, will cause commands to call os.Exit instead of returning an error.
125 // Used for skipping printing usage on failure.
126 var ExitOnError bool
127
128 // StackOnError if true, will print a stack trace on failure.
129 var StackOnError bool
130
131 const cmdName = "kustomize fn"
132
133 // FixDocs replaces instances of old with new in the docs for c
134 func FixDocs(newStr string, c *cobra.Command) {
135 c.Use = strings.ReplaceAll(c.Use, cmdName, newStr)
136 c.Short = strings.ReplaceAll(c.Short, cmdName, newStr)
137 c.Long = strings.ReplaceAll(c.Long, cmdName, newStr)
138 c.Example = strings.ReplaceAll(c.Example, cmdName, newStr)
139 }
140
141 // containsString returns true if slice contains s
142 func containsString(slice []string, s string) bool {
143 for _, item := range slice {
144 if item == s {
145 return true
146 }
147 }
148 return false
149 }