mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Fmt with subpackages
This commit is contained in:
@@ -4,7 +4,11 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/ext"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio/filters"
|
||||
@@ -33,6 +37,8 @@ formatting substitution verbs {'%n': 'metadata.name', '%s': 'metadata.namespace'
|
||||
`if true, override existing filepath annotations.`)
|
||||
c.Flags().BoolVar(&r.UseSchema, "use-schema", false,
|
||||
`if true, uses openapi resource schema to format resources.`)
|
||||
c.Flags().BoolVarP(&r.RecurseSubPackages, "recurse-subpackages", "R", false,
|
||||
"formats resource files recursively in all the nested subpackages")
|
||||
r.Command = c
|
||||
return r
|
||||
}
|
||||
@@ -43,12 +49,13 @@ func FmtCommand(name string) *cobra.Command {
|
||||
|
||||
// FmtRunner contains the run function
|
||||
type FmtRunner struct {
|
||||
Command *cobra.Command
|
||||
FilenamePattern string
|
||||
SetFilenames bool
|
||||
KeepAnnotations bool
|
||||
Override bool
|
||||
UseSchema bool
|
||||
Command *cobra.Command
|
||||
FilenamePattern string
|
||||
SetFilenames bool
|
||||
KeepAnnotations bool
|
||||
Override bool
|
||||
UseSchema bool
|
||||
RecurseSubPackages bool
|
||||
}
|
||||
|
||||
func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
@@ -59,17 +66,6 @@ func (r *FmtRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
|
||||
f := []kio.Filter{filters.FormatFilter{
|
||||
UseSchema: r.UseSchema,
|
||||
}}
|
||||
|
||||
// format with file names
|
||||
if r.SetFilenames {
|
||||
f = append(f, &filters.FileSetter{
|
||||
FilenamePattern: r.FilenamePattern,
|
||||
Override: r.Override,
|
||||
})
|
||||
}
|
||||
|
||||
// format stdin if there are no args
|
||||
if len(args) == 0 {
|
||||
@@ -79,20 +75,64 @@ func (r *FmtRunner) runE(c *cobra.Command, args []string) error {
|
||||
KeepReaderAnnotations: r.KeepAnnotations,
|
||||
}
|
||||
return handleError(c, kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw}, Filters: f, Outputs: []kio.Writer{rw}}.Execute())
|
||||
Inputs: []kio.Reader{rw}, Filters: r.fmtFilters(), Outputs: []kio.Writer{rw}}.Execute())
|
||||
}
|
||||
|
||||
for i := range args {
|
||||
path := args[i]
|
||||
rw := &kio.LocalPackageReadWriter{
|
||||
NoDeleteFiles: true,
|
||||
PackagePath: path,
|
||||
KeepReaderAnnotations: r.KeepAnnotations}
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw}, Filters: f, Outputs: []kio.Writer{rw}}.Execute()
|
||||
for _, rootPkgPath := range args {
|
||||
e := executeCmdOnPkgs{
|
||||
writer: c.OutOrStdout(),
|
||||
needOpenAPI: false,
|
||||
recurseSubPackages: r.RecurseSubPackages,
|
||||
cmdRunner: r,
|
||||
rootPkgPath: rootPkgPath,
|
||||
}
|
||||
|
||||
err := e.execute()
|
||||
if err != nil {
|
||||
return handleError(c, err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *FmtRunner) executeCmd(w io.Writer, pkgPath string) error {
|
||||
openAPIFileName, err := ext.OpenAPIFileName()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rw := &kio.LocalPackageReadWriter{
|
||||
NoDeleteFiles: true,
|
||||
PackagePath: pkgPath,
|
||||
KeepReaderAnnotations: r.KeepAnnotations, PackageFileName: openAPIFileName}
|
||||
err = kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw}, Filters: r.fmtFilters(), Outputs: []kio.Writer{rw}}.Execute()
|
||||
|
||||
if err != nil {
|
||||
// return err if RecurseSubPackages is false
|
||||
if !r.RecurseSubPackages {
|
||||
return err
|
||||
} else {
|
||||
// print error message and continue if RecurseSubPackages is true
|
||||
fmt.Fprintf(w, "%s in package %q\n", err.Error(), pkgPath)
|
||||
}
|
||||
} else {
|
||||
fmt.Fprintf(w, "formatted resource files in package %q\n", pkgPath)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *FmtRunner) fmtFilters() []kio.Filter {
|
||||
fmtFilters := []kio.Filter{filters.FormatFilter{
|
||||
UseSchema: r.UseSchema,
|
||||
}}
|
||||
|
||||
// format with file names
|
||||
if r.SetFilenames {
|
||||
fmtFilters = append(fmtFilters, &filters.FileSetter{
|
||||
FilenamePattern: r.FilenamePattern,
|
||||
Override: r.Override,
|
||||
})
|
||||
}
|
||||
return fmtFilters
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user