Fix inconsistent validation of -f or -k flag in kubectl create command
This commit is contained in:
		@@ -109,12 +109,6 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobr
 | 
			
		||||
		Long:                  createLong,
 | 
			
		||||
		Example:               createExample,
 | 
			
		||||
		Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
			if cmdutil.IsFilenameSliceEmpty(o.FilenameOptions.Filenames, o.FilenameOptions.Kustomize) {
 | 
			
		||||
				ioStreams.ErrOut.Write([]byte("Error: must specify one of -f and -k\n\n"))
 | 
			
		||||
				defaultRunFunc := cmdutil.DefaultSubCommandRun(ioStreams.ErrOut)
 | 
			
		||||
				defaultRunFunc(cmd, args)
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			cmdutil.CheckErr(o.Complete(f, cmd, args))
 | 
			
		||||
			cmdutil.CheckErr(o.Validate())
 | 
			
		||||
			cmdutil.CheckErr(o.RunCreate(f, cmd))
 | 
			
		||||
@@ -159,8 +153,12 @@ func NewCmdCreate(f cmdutil.Factory, ioStreams genericiooptions.IOStreams) *cobr
 | 
			
		||||
	return cmd
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Validate makes sure there is no discrepency in command options
 | 
			
		||||
// Validate makes sure there is no discrepancy in command options
 | 
			
		||||
func (o *CreateOptions) Validate() error {
 | 
			
		||||
	if err := o.FilenameOptions.RequireFilenameOrKustomize(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(o.Raw) > 0 {
 | 
			
		||||
		if o.EditBeforeCreate {
 | 
			
		||||
			return fmt.Errorf("--raw and --edit are mutually exclusive")
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,7 @@ import (
 | 
			
		||||
	"k8s.io/cli-runtime/pkg/resource"
 | 
			
		||||
	"k8s.io/client-go/rest/fake"
 | 
			
		||||
	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
 | 
			
		||||
	cmdutil "k8s.io/kubectl/pkg/cmd/util"
 | 
			
		||||
	"k8s.io/kubectl/pkg/scheme"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -150,3 +151,35 @@ func TestCreateDirectory(t *testing.T) {
 | 
			
		||||
		t.Errorf("unexpected output: %s", buf.String())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestMissingFilenameError(t *testing.T) {
 | 
			
		||||
	var errStr string
 | 
			
		||||
	var exitCode int
 | 
			
		||||
	cmdutil.BehaviorOnFatal(func(str string, code int) {
 | 
			
		||||
		if errStr == "" {
 | 
			
		||||
			errStr = str
 | 
			
		||||
			exitCode = code
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	tf := cmdtesting.NewTestFactory().WithNamespace("test")
 | 
			
		||||
	defer tf.Cleanup()
 | 
			
		||||
 | 
			
		||||
	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
 | 
			
		||||
	cmd := NewCmdCreate(tf, ioStreams)
 | 
			
		||||
	cmd.Run(cmd, []string{})
 | 
			
		||||
 | 
			
		||||
	if buf.Len() > 0 {
 | 
			
		||||
		t.Errorf("unexpected output: %s", buf.String())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(errStr) == 0 {
 | 
			
		||||
		t.Errorf("unexpected non-error")
 | 
			
		||||
	} else if errStr != "error: must specify one of -f and -k" {
 | 
			
		||||
		t.Errorf("unexpected error: %s", errStr)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if exitCode != 1 {
 | 
			
		||||
		t.Errorf("unexpected exit code: %d", exitCode)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user