diff --git a/examples/plugins/date/main.go b/examples/plugins/date/main.go index 2afc465..bf09cc1 100644 --- a/examples/plugins/date/main.go +++ b/examples/plugins/date/main.go @@ -1,6 +1,7 @@ package date import ( + "fmt" "time" "github.com/aerogear/charmil/pkg/factory" @@ -23,7 +24,7 @@ type Options struct { func DateCommand() (*cobra.Command, error) { // Initialize localizer providing the language, locals and format of locals file - loc, err := localize.InitLocalizer(localize.Config{Language: language.English, Path: "examples/plugins/date/locals/en/en.yaml", Format: "yaml"}) + loc, err := localize.InitLocalizer(localize.Config{Language: language.English, Path: "examples/plugins/date/locales/en/en.yaml", Format: "yaml"}) if err != nil { return nil, err } @@ -53,11 +54,14 @@ func DateCommand() (*cobra.Command, error) { }, } - // default validation provided by validator package - validationErr := validator.Validate(cmd) - if validationErr != nil { - return nil, validationErr + var r validator.Rule = &validator.LengthRule{ + Use: validator.Limit{Min: 2, Max: 5}, + Short: validator.Limit{Min: 4, Max: 5}, + Long: validator.Limit{Min: 5, Max: 20}, + Example: validator.Limit{Min: 5, Max: 30}, } + errr := r.Validate(cmd) + fmt.Println(errr) return cmd, nil } diff --git a/validator/length.go b/validator/length.go new file mode 100644 index 0000000..d3c5048 --- /dev/null +++ b/validator/length.go @@ -0,0 +1,97 @@ +package validator + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +type LengthRule struct { + Use Limit + Short Limit + Long Limit + Example Limit +} + +type Limit struct { + Min, Max int +} + +func (l *LengthRule) Validate(cmd *cobra.Command) []error { + + var errors []error + err := validateCobraCommand(cmd, l) + errors = append(errors, err...) + + for _, child := range cmd.Commands() { + err := validateCobraCommand(child, l) + errors = append(errors, err...) + + } + + return errors +} + +func validateCobraCommand(cmd *cobra.Command, l *LengthRule) []error { + var errors []error + + cmdPath := cmd.CommandPath() + + use := cmd.Use + useErr := validateField(l.Use, use, cmdPath) + if useErr != nil { + errors = append(errors, useErr) + } + + short := cmd.Short + shortErr := validateField(l.Short, short, cmdPath) + if shortErr != nil { + errors = append(errors, shortErr) + } + + long := cmd.Long + longErr := validateField(l.Long, long, cmdPath) + if longErr != nil { + errors = append(errors, longErr) + } + + example := cmd.Example + exampleErr := validateField(l.Example, example, cmdPath) + if exampleErr != nil { + errors = append(errors, exampleErr) + } + + return errors +} + +func validateField(limit Limit, value string, path string) error { + length := len(value) + + _, err := isLimitSet(limit) + if err != nil { + return err + } + + if length < limit.Min { + return fmt.Errorf("%s in %s: length should be atleast %d", value, path, limit.Min) + } + if length > limit.Max { + return fmt.Errorf("%s in %s: length should be less than %d", value, path, limit.Max) + } + + return nil +} + +func isLimitSet(limit Limit) (bool, error) { + if limit.Max < 0 || limit.Min < 0 { + return true, fmt.Errorf("max and min must be greater than 0") + } + if limit.Max == 0 && limit.Min == 0 { + return false, fmt.Errorf("limit not set") + } + if limit.Max < limit.Min { + return true, fmt.Errorf("max limit must be greater than min limit") + } + + return true, nil +} diff --git a/validator/std_validator.go b/validator/std_validator.go deleted file mode 100644 index 3c90373..0000000 --- a/validator/std_validator.go +++ /dev/null @@ -1,70 +0,0 @@ -package validator - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -// Validate validates the cobra commands -// and all the descendants -func Validate(cmd *cobra.Command) error { - err := handleDefaultErrors(cmd) - if err != nil { - return err - } - - // handeling errors for subcommands of a command - subCommmands := cmd.Commands() - for _, child := range subCommmands { - err := handleDefaultErrors(child) - if err != nil { - return err - } - } - - return nil -} - -// ValidateCustom will allow user to -// write their own custom rules for validation -// over the default rules -func ValidateCustom(cmd *cobra.Command, handleErrors func() error) error { - err1 := Validate(cmd) - if err1 != nil { - return err1 - } - err2 := handleErrors() - if err2 != nil { - return err2 - } - - return nil -} - -// handleDefaultErrors handles the default errors -func handleDefaultErrors(cmd *cobra.Command) error { - cmdPath := cmd.CommandPath() - - if len(cmd.Use) < 1 { - return fmt.Errorf("%s: length of cmd.Use should be more than 1", cmdPath) - } - - if len(cmd.Short) < 5 { - return fmt.Errorf("%s: length of cmd.Short should be more than 5", cmdPath) - } - - if len(cmd.Long) < 10 { - return fmt.Errorf("%s: length of cmd.Long should be more than 10", cmdPath) - } - - if len(cmd.Example) < 1 { - return fmt.Errorf("%s: length of cmd.Example should be more than 1", cmdPath) - } - - if cmd.Args == nil { - return fmt.Errorf("%s: provide Args function to the command", cmdPath) - } - - return nil -} diff --git a/validator/validator.go b/validator/validator.go new file mode 100644 index 0000000..507051f --- /dev/null +++ b/validator/validator.go @@ -0,0 +1,9 @@ +package validator + +import ( + "github.com/spf13/cobra" +) + +type Rule interface { + Validate(cmd *cobra.Command) []error +}