mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
[Kubectl] Update RunCreate to follow more conventions
This commit is contained in:
parent
2175199646
commit
3d75e990c3
@ -42,6 +42,8 @@ type CreateOptions struct {
|
|||||||
Selector string
|
Selector string
|
||||||
EditBeforeCreate bool
|
EditBeforeCreate bool
|
||||||
Raw string
|
Raw string
|
||||||
|
Out io.Writer
|
||||||
|
ErrOut io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -62,7 +64,10 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdCreate(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
func NewCmdCreate(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
||||||
var options CreateOptions
|
options := &CreateOptions{
|
||||||
|
Out: out,
|
||||||
|
ErrOut: errOut,
|
||||||
|
}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "create -f FILENAME",
|
Use: "create -f FILENAME",
|
||||||
@ -76,7 +81,7 @@ func NewCmdCreate(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
cmdutil.CheckErr(options.ValidateArgs(cmd, args))
|
cmdutil.CheckErr(options.ValidateArgs(cmd, args))
|
||||||
cmdutil.CheckErr(RunCreate(f, cmd, out, errOut, &options))
|
cmdutil.CheckErr(options.RunCreate(f, cmd))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,36 +148,15 @@ func (o *CreateOptions) ValidateArgs(cmd *cobra.Command, args []string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, options *CreateOptions) error {
|
func (o *CreateOptions) RunCreate(f cmdutil.Factory, cmd *cobra.Command) error {
|
||||||
// raw only makes sense for a single file resource multiple objects aren't likely to do what you want.
|
// raw only makes sense for a single file resource multiple objects aren't likely to do what you want.
|
||||||
// the validator enforces this, so
|
// the validator enforces this, so
|
||||||
if len(options.Raw) > 0 {
|
if len(o.Raw) > 0 {
|
||||||
restClient, err := f.RESTClient()
|
return o.raw(f)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
var data io.ReadCloser
|
|
||||||
if options.FilenameOptions.Filenames[0] == "-" {
|
|
||||||
data = os.Stdin
|
|
||||||
} else {
|
|
||||||
data, err = os.Open(options.FilenameOptions.Filenames[0])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// TODO post content with stream. Right now it ignores body content
|
|
||||||
bytes, err := restClient.Post().RequestURI(options.Raw).Body(data).DoRaw()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(out, "%v", string(bytes))
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.EditBeforeCreate {
|
if o.EditBeforeCreate {
|
||||||
return RunEditOnCreate(f, out, errOut, cmd, &options.FilenameOptions)
|
return RunEditOnCreate(f, o.Out, o.ErrOut, cmd, &o.FilenameOptions)
|
||||||
}
|
}
|
||||||
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
|
schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -189,8 +173,8 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt
|
|||||||
Schema(schema).
|
Schema(schema).
|
||||||
ContinueOnError().
|
ContinueOnError().
|
||||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||||
FilenameParam(enforceNamespace, &options.FilenameOptions).
|
FilenameParam(enforceNamespace, &o.FilenameOptions).
|
||||||
LabelSelectorParam(options.Selector).
|
LabelSelectorParam(o.Selector).
|
||||||
Flatten().
|
Flatten().
|
||||||
Do()
|
Do()
|
||||||
err = r.Err()
|
err = r.Err()
|
||||||
@ -227,13 +211,13 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt
|
|||||||
|
|
||||||
shortOutput := output == "name"
|
shortOutput := output == "name"
|
||||||
if len(output) > 0 && !shortOutput {
|
if len(output) > 0 && !shortOutput {
|
||||||
return f.PrintResourceInfoForCommand(cmd, info, out)
|
return f.PrintResourceInfoForCommand(cmd, info, o.Out)
|
||||||
}
|
}
|
||||||
if !shortOutput {
|
if !shortOutput {
|
||||||
f.PrintObjectSpecificMessage(info.Object, out)
|
f.PrintObjectSpecificMessage(info.Object, o.Out)
|
||||||
}
|
}
|
||||||
|
|
||||||
f.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created")
|
f.PrintSuccess(mapper, shortOutput, o.Out, info.Mapping.Resource, info.Name, dryRun, "created")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -245,6 +229,33 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// raw makes a simple HTTP request to the provided path on the server using the default
|
||||||
|
// credentials.
|
||||||
|
func (o *CreateOptions) raw(f cmdutil.Factory) error {
|
||||||
|
restClient, err := f.RESTClient()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var data io.ReadCloser
|
||||||
|
if o.FilenameOptions.Filenames[0] == "-" {
|
||||||
|
data = os.Stdin
|
||||||
|
} else {
|
||||||
|
data, err = os.Open(o.FilenameOptions.Filenames[0])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO post content with stream. Right now it ignores body content
|
||||||
|
bytes, err := restClient.Post().RequestURI(o.Raw).Body(data).DoRaw()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(o.Out, "%v", string(bytes))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func RunEditOnCreate(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, options *resource.FilenameOptions) error {
|
func RunEditOnCreate(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, options *resource.FilenameOptions) error {
|
||||||
editOptions := &editor.EditOptions{
|
editOptions := &editor.EditOptions{
|
||||||
EditMode: editor.EditBeforeCreateMode,
|
EditMode: editor.EditBeforeCreateMode,
|
||||||
|
Loading…
Reference in New Issue
Block a user