Removed factory from apply command flag struct

This commit is contained in:
Chok Yip Lau 2022-11-20 15:53:05 -05:00
parent 789dc88fd5
commit cdd84a28bf
2 changed files with 15 additions and 18 deletions

View File

@ -55,8 +55,6 @@ import (
// reflect the runtime requirements for the command. This structure reduces the transformation to wiring and makes // reflect the runtime requirements for the command. This structure reduces the transformation to wiring and makes
// the logic itself easy to unit test // the logic itself easy to unit test
type ApplyFlags struct { type ApplyFlags struct {
Factory cmdutil.Factory
RecordFlags *genericclioptions.RecordFlags RecordFlags *genericclioptions.RecordFlags
PrintFlags *genericclioptions.PrintFlags PrintFlags *genericclioptions.PrintFlags
@ -172,9 +170,8 @@ var (
) )
// NewApplyFlags returns a default ApplyFlags // NewApplyFlags returns a default ApplyFlags
func NewApplyFlags(f cmdutil.Factory, streams genericclioptions.IOStreams) *ApplyFlags { func NewApplyFlags(streams genericclioptions.IOStreams) *ApplyFlags {
return &ApplyFlags{ return &ApplyFlags{
Factory: f,
RecordFlags: genericclioptions.NewRecordFlags(), RecordFlags: genericclioptions.NewRecordFlags(),
DeleteFlags: delete.NewDeleteFlags("The files that contain the configurations to apply."), DeleteFlags: delete.NewDeleteFlags("The files that contain the configurations to apply."),
PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme), PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
@ -188,7 +185,7 @@ func NewApplyFlags(f cmdutil.Factory, streams genericclioptions.IOStreams) *Appl
// NewCmdApply creates the `apply` command // NewCmdApply creates the `apply` command
func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
flags := NewApplyFlags(f, ioStreams) flags := NewApplyFlags(ioStreams)
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "apply (-f FILENAME | -k DIRECTORY)", Use: "apply (-f FILENAME | -k DIRECTORY)",
@ -197,7 +194,7 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions
Long: applyLong, Long: applyLong,
Example: applyExample, Example: applyExample,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
o, err := flags.ToOptions(cmd, baseName, args) o, err := flags.ToOptions(f, cmd, baseName, args)
cmdutil.CheckErr(err) cmdutil.CheckErr(err)
cmdutil.CheckErr(o.Validate()) cmdutil.CheckErr(o.Validate())
cmdutil.CheckErr(o.Run()) cmdutil.CheckErr(o.Run())
@ -207,9 +204,9 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions
flags.AddFlags(cmd) flags.AddFlags(cmd)
// apply subcommands // apply subcommands
cmd.AddCommand(NewCmdApplyViewLastApplied(flags.Factory, flags.IOStreams)) cmd.AddCommand(NewCmdApplyViewLastApplied(f, flags.IOStreams))
cmd.AddCommand(NewCmdApplySetLastApplied(flags.Factory, flags.IOStreams)) cmd.AddCommand(NewCmdApplySetLastApplied(f, flags.IOStreams))
cmd.AddCommand(NewCmdApplyEditLastApplied(flags.Factory, flags.IOStreams)) cmd.AddCommand(NewCmdApplyEditLastApplied(f, flags.IOStreams))
return cmd return cmd
} }
@ -237,7 +234,7 @@ func (flags *ApplyFlags) AddFlags(cmd *cobra.Command) {
} }
// ToOptions converts from CLI inputs to runtime inputs // ToOptions converts from CLI inputs to runtime inputs
func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []string) (*ApplyOptions, error) { func (flags *ApplyFlags) ToOptions(f cmdutil.Factory, cmd *cobra.Command, baseName string, args []string) (*ApplyOptions, error) {
if len(args) != 0 { if len(args) != 0 {
return nil, cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args) return nil, cmdutil.UsageErrorf(cmd, "Unexpected args: %v", args)
} }
@ -249,7 +246,7 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
return nil, err return nil, err
} }
dynamicClient, err := flags.Factory.DynamicClient() dynamicClient, err := f.DynamicClient()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -279,23 +276,23 @@ func (flags *ApplyFlags) ToOptions(cmd *cobra.Command, baseName string, args []s
return nil, err return nil, err
} }
openAPISchema, _ := flags.Factory.OpenAPISchema() openAPISchema, _ := f.OpenAPISchema()
validationDirective, err := cmdutil.GetValidationDirective(cmd) validationDirective, err := cmdutil.GetValidationDirective(cmd)
if err != nil { if err != nil {
return nil, err return nil, err
} }
validator, err := flags.Factory.Validator(validationDirective) validator, err := f.Validator(validationDirective, fieldValidationVerifier)
if err != nil { if err != nil {
return nil, err return nil, err
} }
builder := flags.Factory.NewBuilder() builder := f.NewBuilder()
mapper, err := flags.Factory.ToRESTMapper() mapper, err := f.ToRESTMapper()
if err != nil { if err != nil {
return nil, err return nil, err
} }
namespace, enforceNamespace, err := flags.Factory.ToRawKubeConfigLoader().Namespace() namespace, enforceNamespace, err := f.ToRawKubeConfigLoader().Namespace()
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -151,13 +151,13 @@ func TestApplyFlagValidation(t *testing.T) {
for _, test := range tests { for _, test := range tests {
cmd := &cobra.Command{} cmd := &cobra.Command{}
flags := NewApplyFlags(f, genericclioptions.NewTestIOStreamsDiscard()) flags := NewApplyFlags(genericclioptions.NewTestIOStreamsDiscard())
flags.AddFlags(cmd) flags.AddFlags(cmd)
cmd.Flags().Set("filename", "unused") cmd.Flags().Set("filename", "unused")
for _, arg := range test.args { for _, arg := range test.args {
cmd.Flags().Set(arg[0], arg[1]) cmd.Flags().Set(arg[0], arg[1])
} }
o, err := flags.ToOptions(cmd, "kubectl", []string{}) o, err := flags.ToOptions(f, cmd, "kubectl", []string{})
if err != nil { if err != nil {
t.Fatalf("unexpected error creating apply options: %s", err) t.Fatalf("unexpected error creating apply options: %s", err)
} }