bind filenames var instead of looking up

This commit is contained in:
deads2k
2015-08-14 14:46:43 -04:00
parent 5c2c42eed8
commit f1b81ff678
17 changed files with 169 additions and 87 deletions

View File

@@ -31,6 +31,12 @@ import (
"k8s.io/kubernetes/pkg/kubectl/resource"
)
// ReplaceOptions 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 ReplaceOptions struct {
Filenames []string
}
const (
replace_long = `Replace a resource by filename or stdin.
@@ -53,6 +59,8 @@ kubectl replace --force -f ./pod.json`
)
func NewCmdReplace(f *cmdutil.Factory, out io.Writer) *cobra.Command {
options := &ReplaceOptions{}
cmd := &cobra.Command{
Use: "replace -f FILENAME",
// update is deprecated.
@@ -62,12 +70,12 @@ func NewCmdReplace(f *cmdutil.Factory, out io.Writer) *cobra.Command {
Example: replace_example,
Run: func(cmd *cobra.Command, args []string) {
cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd))
err := RunReplace(f, out, cmd, args)
err := RunReplace(f, out, cmd, args, options)
cmdutil.CheckErr(err)
},
}
usage := "Filename, directory, or URL to file to use to replace the resource."
kubectl.AddJsonFilenameFlag(cmd, usage)
kubectl.AddJsonFilenameFlag(cmd, &options.Filenames, usage)
cmd.MarkFlagRequired("filename")
cmd.Flags().Bool("force", false, "Delete and re-create the specified resource")
cmd.Flags().Bool("cascade", false, "Only relevant during a force replace. If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController).")
@@ -78,7 +86,7 @@ func NewCmdReplace(f *cmdutil.Factory, out io.Writer) *cobra.Command {
return cmd
}
func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string) error {
func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, options *ReplaceOptions) error {
if len(os.Args) > 1 && os.Args[1] == "update" {
printDeprecationWarning("replace", "update")
}
@@ -93,14 +101,13 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
}
force := cmdutil.GetFlagBool(cmd, "force")
filenames := cmdutil.GetFlagStringSlice(cmd, "filename")
if len(filenames) == 0 {
if len(options.Filenames) == 0 {
return cmdutil.UsageError(cmd, "Must specify --filename to replace")
}
shortOutput := cmdutil.GetFlagString(cmd, "output") == "name"
if force {
return forceReplace(f, out, cmd, args, filenames, shortOutput)
return forceReplace(f, out, cmd, args, shortOutput, options)
}
mapper, typer := f.Object()
@@ -108,7 +115,7 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, filenames...).
FilenameParam(enforceNamespace, options.Filenames...).
Flatten().
Do()
err = r.Err()
@@ -135,7 +142,7 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st
})
}
func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, filenames []string, shortOutput bool) error {
func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string, shortOutput bool, options *ReplaceOptions) error {
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
if err != nil {
return err
@@ -146,7 +153,7 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
return err
}
for i, filename := range filenames {
for i, filename := range options.Filenames {
if filename == "-" {
tempDir, err := ioutil.TempDir("", "kubectl_replace_")
if err != nil {
@@ -158,7 +165,7 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
if err != nil {
return err
}
filenames[i] = tempFilename
options.Filenames[i] = tempFilename
}
}
@@ -166,7 +173,7 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
r := resource.NewBuilder(mapper, typer, f.ClientMapperForCommand()).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, filenames...).
FilenameParam(enforceNamespace, options.Filenames...).
ResourceTypeOrNameArgs(false, args...).RequireObject(false).
Flatten().
Do()
@@ -191,7 +198,7 @@ func forceReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []
Schema(schema).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
FilenameParam(enforceNamespace, filenames...).
FilenameParam(enforceNamespace, options.Filenames...).
Flatten().
Do()
err = r.Err()