mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Refactor set
- Implement inline setters as OpenAPI extensions - Naming cleanup substitute -> set - Documentation cleanup - Simplify implementation
This commit is contained in:
120
cmd/config/internal/commands/cmdset.go
Normal file
120
cmd/config/internal/commands/cmdset.go
Normal file
@@ -0,0 +1,120 @@
|
||||
// Copyright 2019 The Kubernetes Authors.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/olekukonko/tablewriter"
|
||||
"github.com/spf13/cobra"
|
||||
"sigs.k8s.io/kustomize/cmd/config/internal/generateddocs/commands"
|
||||
"sigs.k8s.io/kustomize/kyaml/kio"
|
||||
"sigs.k8s.io/kustomize/kyaml/setters"
|
||||
)
|
||||
|
||||
// NewSetRunner returns a command runner.
|
||||
func NewSetRunner(parent string) *SetRunner {
|
||||
r := &SetRunner{}
|
||||
c := &cobra.Command{
|
||||
Use: "set DIR [NAME] [VALUE]",
|
||||
Args: cobra.RangeArgs(1, 3),
|
||||
Short: commands.SetShort,
|
||||
Long: commands.SetLong,
|
||||
Example: commands.SetExamples,
|
||||
PreRunE: r.preRunE,
|
||||
RunE: r.runE,
|
||||
}
|
||||
fixDocs(parent, c)
|
||||
r.Command = c
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
func SetCommand(parent string) *cobra.Command {
|
||||
return NewSetRunner(parent).Command
|
||||
}
|
||||
|
||||
type SetRunner struct {
|
||||
Command *cobra.Command
|
||||
Lookup setters.LookupSetters
|
||||
Perform setters.PerformSetters
|
||||
}
|
||||
|
||||
func (r *SetRunner) preRunE(c *cobra.Command, args []string) error {
|
||||
if len(args) > 1 {
|
||||
r.Perform.Name = args[1]
|
||||
r.Lookup.Name = args[1]
|
||||
}
|
||||
if len(args) > 2 {
|
||||
r.Perform.Value = args[2]
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SetRunner) runE(c *cobra.Command, args []string) error {
|
||||
|
||||
if len(args) == 3 {
|
||||
return handleError(c, r.perform(c, args))
|
||||
}
|
||||
|
||||
return handleError(c, r.lookup(c, args))
|
||||
}
|
||||
|
||||
func (r *SetRunner) lookup(c *cobra.Command, args []string) error {
|
||||
// lookup the setters
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{&kio.LocalPackageReader{PackagePath: args[0]}},
|
||||
Filters: []kio.Filter{&r.Lookup},
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
table := tablewriter.NewWriter(c.OutOrStdout())
|
||||
table.SetRowLine(false)
|
||||
table.SetBorder(false)
|
||||
table.SetHeaderLine(false)
|
||||
table.SetColumnSeparator(" ")
|
||||
table.SetCenterSeparator(" ")
|
||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
||||
table.SetHeader([]string{
|
||||
"NAME", "DESCRIPTION", "VALUE", "TYPE", "COUNT", "SETBY",
|
||||
})
|
||||
for i := range r.Lookup.SetterCounts {
|
||||
s := r.Lookup.SetterCounts[i]
|
||||
v := s.Value
|
||||
if s.Value == "" {
|
||||
v = s.Value
|
||||
}
|
||||
table.Append([]string{
|
||||
s.Name,
|
||||
"'" + s.Description + "'",
|
||||
v,
|
||||
fmt.Sprintf("%v", s.Type),
|
||||
fmt.Sprintf("%d", s.Count),
|
||||
s.SetBy,
|
||||
})
|
||||
}
|
||||
table.Render()
|
||||
return nil
|
||||
}
|
||||
|
||||
// perform the setters
|
||||
func (r *SetRunner) perform(c *cobra.Command, args []string) error {
|
||||
rw := &kio.LocalPackageReadWriter{
|
||||
PackagePath: args[0],
|
||||
}
|
||||
// perform the setters in the package
|
||||
err := kio.Pipeline{
|
||||
Inputs: []kio.Reader{rw},
|
||||
Filters: []kio.Filter{&r.Perform},
|
||||
Outputs: []kio.Writer{rw},
|
||||
}.Execute()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(c.OutOrStdout(), "set %d fields\n", r.Perform.Count)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user