Fixes #30562: Refactor kubectl command options to use common struct for common file params

This commit is contained in:
ymqytw
2016-08-17 11:28:07 -07:00
parent 115855bbd2
commit c67a62da49
28 changed files with 182 additions and 291 deletions

View File

@@ -37,13 +37,6 @@ import (
"k8s.io/kubernetes/pkg/util/validation"
)
// LabelOptions 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 LabelOptions struct {
Filenames []string
Recursive bool
}
var (
label_long = dedent.Dedent(`
Update the labels on a resource.
@@ -73,7 +66,7 @@ var (
)
func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command {
options := &LabelOptions{}
options := &resource.FilenameOptions{}
// retrieve a list of handled resources from printer as valid args
validArgs, argAliases := []string{}, []string{}
@@ -103,9 +96,8 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
cmd.Flags().Bool("all", false, "select all resources in the namespace of the specified resource types")
cmd.Flags().String("resource-version", "", "If non-empty, the labels update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.")
usage := "Filename, directory, or URL to a file identifying the resource to update the labels"
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
cmdutil.AddRecursiveFlag(cmd, &options.Recursive)
usage := "identifying the resource to update the labels"
cmdutil.AddFilenameOptionFlags(cmd, options, usage)
cmdutil.AddDryRunFlag(cmd)
cmdutil.AddRecordFlag(cmd)
cmdutil.AddInclude3rdPartyFlags(cmd)
@@ -180,12 +172,12 @@ func labelFunc(obj runtime.Object, overwrite bool, resourceVersion string, label
return nil
}
func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *LabelOptions) error {
func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *resource.FilenameOptions) error {
resources, labelArgs, err := cmdutil.GetResourcesAndPairs(args, "label")
if err != nil {
return err
}
if len(resources) < 1 && len(options.Filenames) == 0 {
if len(resources) < 1 && cmdutil.IsFilenameEmpty(options.Filenames) {
return cmdutil.UsageError(cmd, "one or more resources must be specified as <resource> <name> or <resource>/<name>")
}
if len(labelArgs) < 1 {
@@ -210,7 +202,7 @@ func RunLabel(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []stri
b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
FilenameParam(enforceNamespace, options).
SelectorParam(selector).
ResourceTypeOrNameArgs(all, resources...).
Flatten().