Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
115 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package validator | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type Rule interface { | ||
Validate(cmd *cobra.Command) []error | ||
} |