mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-01-04 23:17:50 +00:00
allow kubectl cmd to process dirs recursively
reqs:
- the kubectl cmd must support the -f | --filename flag
- the kubectl cmd must support visiting a dir one level deep,
or using more than one resource
This commit is contained in:
@@ -74,17 +74,25 @@ saved copy to include the latest resource version.`
|
||||
kubectl edit svc/docker-registry --output-version=v1 -o json`
|
||||
)
|
||||
|
||||
// EditOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
|
||||
// referencing the cmd.Flags()
|
||||
type EditOptions struct {
|
||||
Filenames []string
|
||||
Recursive bool
|
||||
}
|
||||
|
||||
var errExit = fmt.Errorf("exit directly")
|
||||
|
||||
func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
||||
filenames := []string{}
|
||||
options := &EditOptions{}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "edit (RESOURCE/NAME | -f FILENAME)",
|
||||
Short: "Edit a resource on the server",
|
||||
Long: editLong,
|
||||
Example: fmt.Sprintf(editExample),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
err := RunEdit(f, out, errOut, cmd, args, filenames)
|
||||
err := RunEdit(f, out, errOut, cmd, args, options)
|
||||
if err == errExit {
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -92,7 +100,8 @@ func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
||||
},
|
||||
}
|
||||
usage := "Filename, directory, or URL to file to use to edit the resource"
|
||||
kubectl.AddJsonFilenameFlag(cmd, &filenames, usage)
|
||||
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
|
||||
cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
|
||||
cmd.Flags().StringP("output", "o", "yaml", "Output format. One of: yaml|json.")
|
||||
cmd.Flags().String("output-version", "", "Output the formatted object with the given group version (for ex: 'extensions/v1beta1').")
|
||||
cmd.Flags().Bool("windows-line-endings", gruntime.GOOS == "windows", "Use Windows line-endings (default Unix line-endings)")
|
||||
@@ -101,7 +110,7 @@ func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
||||
return cmd
|
||||
}
|
||||
|
||||
func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, filenames []string) error {
|
||||
func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args []string, options *EditOptions) error {
|
||||
var printer kubectl.ResourcePrinter
|
||||
var ext string
|
||||
switch format := cmdutil.GetFlagString(cmd, "output"); format {
|
||||
@@ -130,7 +139,7 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
|
||||
|
||||
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
|
||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||
FilenameParam(enforceNamespace, filenames...).
|
||||
FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
|
||||
ResourceTypeOrNameArgs(true, args...).
|
||||
Latest().
|
||||
Flatten().
|
||||
|
||||
Reference in New Issue
Block a user