Commit da03746b authored by feihujiang's avatar feihujiang

Aggregate errors when validate kubectl required parameters

parent 2f9652c7
...@@ -28,6 +28,7 @@ import ( ...@@ -28,6 +28,7 @@ import (
"k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util" "k8s.io/kubernetes/pkg/util"
"k8s.io/kubernetes/pkg/util/errors"
) )
const ( const (
...@@ -78,12 +79,13 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { ...@@ -78,12 +79,13 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command {
} }
func validateNoOverwrites(meta *api.ObjectMeta, labels map[string]string) error { func validateNoOverwrites(meta *api.ObjectMeta, labels map[string]string) error {
allErrs := []error{}
for key := range labels { for key := range labels {
if value, found := meta.Labels[key]; found { if value, found := meta.Labels[key]; found {
return fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value) allErrs = append(allErrs, fmt.Errorf("'%s' already has a value (%s), and --overwrite is false", key, value))
} }
} }
return nil return errors.NewAggregate(allErrs)
} }
func parseLabels(spec []string) (map[string]string, []string, error) { func parseLabels(spec []string) (map[string]string, []string, error) {
......
...@@ -24,6 +24,7 @@ import ( ...@@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util/errors"
) )
// GeneratorParam is a parameter for a generator // GeneratorParam is a parameter for a generator
...@@ -50,15 +51,16 @@ func IsZero(i interface{}) bool { ...@@ -50,15 +51,16 @@ func IsZero(i interface{}) bool {
// ValidateParams ensures that all required params are present in the params map // ValidateParams ensures that all required params are present in the params map
func ValidateParams(paramSpec []GeneratorParam, params map[string]interface{}) error { func ValidateParams(paramSpec []GeneratorParam, params map[string]interface{}) error {
allErrs := []error{}
for ix := range paramSpec { for ix := range paramSpec {
if paramSpec[ix].Required { if paramSpec[ix].Required {
value, found := params[paramSpec[ix].Name] value, found := params[paramSpec[ix].Name]
if !found || IsZero(value) { if !found || IsZero(value) {
return fmt.Errorf("Parameter: %s is required", paramSpec[ix].Name) allErrs = append(allErrs, fmt.Errorf("Parameter: %s is required", paramSpec[ix].Name))
} }
} }
} }
return nil return errors.NewAggregate(allErrs)
} }
// MakeParams is a utility that creates generator parameters from a command line // MakeParams is a utility that creates generator parameters from a command line
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment