Skip to content

Commit

Permalink
fix: cmd validation status logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ankithans authored and wtrocki committed Jun 30, 2021
1 parent ba03e37 commit 4ba3da6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
40 changes: 19 additions & 21 deletions validator/example/cmd.go
Expand Up @@ -4,6 +4,13 @@ import (
"github.com/spf13/cobra"
)

type command struct {
name *cobra.Command
use string
short string
example string
}

func NewCommand() *cobra.Command {

cmd := &cobra.Command{
Expand All @@ -16,30 +23,21 @@ func NewCommand() *cobra.Command {
},
}

cmd1 := &cobra.Command{
Use: "subcmd01",
Short: "",
Example: "examples",
Run: func(cmd *cobra.Command, args []string) {},
var commands []command = []command{
{use: "subcmd01", short: "short01", example: "example01"},
{use: "subcmd12", short: "short12", example: "example12"},
{use: "subcmd03", short: "short03", example: "example03"},
}

cmd2 := &cobra.Command{
Use: "subcmd12",
Short: "",
Example: "examples",
Run: func(cmd *cobra.Command, args []string) {},
for _, cm := range commands {
cm.name = &cobra.Command{
Use: cm.use,
Short: cm.short,
Example: cm.example,
Run: func(cmd *cobra.Command, args []string) {},
}
cmd.AddCommand(cm.name)
}

cmd3 := &cobra.Command{
Use: "subcmd03",
Short: "",
Example: "examples mine",
Run: func(cmd *cobra.Command, args []string) {},
}

cmd1.AddCommand(cmd2)
cmd.AddCommand(cmd1)
cmd.AddCommand(cmd3)

return cmd
}
48 changes: 24 additions & 24 deletions validator/rules/executor.go
Expand Up @@ -2,7 +2,9 @@ package rules

import (
"encoding/json"
"fmt"
"log"
"os"

"github.com/aerogear/charmil/validator"
"github.com/spf13/cobra"
Expand All @@ -28,59 +30,57 @@ func (config *RuleConfig) ExecuteRules(cmd *cobra.Command) []validator.Validatio
// validate the root command
config.validate(cmd, &info)

return config.executeHelper(cmd, info)
return config.executeHelper(cmd, &info)
}

// executeRulesChildren execute rules on children of cmd
func (config *RuleConfig) executeRulesChildren(cmd *cobra.Command, info validator.StatusLog) []validator.ValidationError {

children := cmd.Commands()
for _, child := range children {
func (config *RuleConfig) executeHelper(cmd *cobra.Command, info *validator.StatusLog) []validator.ValidationError {
info.Errors = config.executeRecursive(cmd, info)

if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}
// prints additional info for the checks
fmt.Fprintf(os.Stderr, "commands checked: %d\nchecks failed: %d\n", info.TotalTested, info.TotalErrors)

config.validate(child, &info)
}
return info.Errors
}

// executeHelper recursively traverse over all the subcommands
// executeRecursive recursively traverse over all the subcommands
// and validate using executeRulesChildren function
func (config *RuleConfig) executeHelper(cmd *cobra.Command, info validator.StatusLog) []validator.ValidationError {
func (config *RuleConfig) executeRecursive(cmd *cobra.Command, info *validator.StatusLog) []validator.ValidationError {

for _, child := range cmd.Commands() {

// base case
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}

// recursive call
info.Errors = config.executeHelper(child, info)

info.Errors = config.executeRecursive(child, info)
}
info.Errors = config.executeRulesChildren(cmd, info)

// prints additional info in debug mode
if config.Verbose {
log.Printf("commands checked: %d\nchecks failed: %d\n", info.TotalTested, info.TotalErrors)
}
return info.Errors
}

info.Errors = config.executeRulesChildren(cmd, info)
// executeRulesChildren execute rules on children of cmd
func (config *RuleConfig) executeRulesChildren(cmd *cobra.Command, info *validator.StatusLog) []validator.ValidationError {
children := cmd.Commands()
for _, child := range children {

if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}
config.validate(child, info)
}
return info.Errors
}

// validate returns validation errors by executing the rules
func (config *RuleConfig) validate(cmd *cobra.Command, info *validator.StatusLog) {

// Length rule
lenErrs := validateLength(cmd, config.Length, config.Verbose)
lenErrs := validateLength(cmd, config)
info.TotalErrors += len(lenErrs)

// MustExist rule
mustExistErrs := validateMustExist(cmd, config.MustExist, config.Verbose)
mustExistErrs := validateMustExist(cmd, config)
info.TotalErrors += len(mustExistErrs)

info.Errors = append(info.Errors, lenErrs...)
Expand Down
6 changes: 3 additions & 3 deletions validator/rules/length.go
Expand Up @@ -34,10 +34,10 @@ type Limit struct {
Min, Max int
}

func validateLength(cmd *cobra.Command, l Length, verbose bool) []validator.ValidationError {
func validateLength(cmd *cobra.Command, config *RuleConfig) []validator.ValidationError {
var errors []validator.ValidationError

for fieldName, limits := range l.Limits {
for fieldName, limits := range config.Length.Limits {
// reflects the fieldName in cobra.Command struct
reflectValue := reflect.ValueOf(cmd).Elem().FieldByName(fieldName)

Expand All @@ -48,7 +48,7 @@ func validateLength(cmd *cobra.Command, l Length, verbose bool) []validator.Vali
}

// validate fieldName
err := validateField(cmd, limits, reflectValue.String(), cmd.CommandPath(), fieldName, verbose)
err := validateField(cmd, limits, reflectValue.String(), cmd.CommandPath(), fieldName, config.Verbose)
if err.Err != nil {
errors = append(errors, err)
}
Expand Down
6 changes: 3 additions & 3 deletions validator/rules/must_exist.go
Expand Up @@ -24,10 +24,10 @@ type MustExist struct {
Fields []string
}

func validateMustExist(cmd *cobra.Command, p MustExist, verbose bool) []validator.ValidationError {
func validateMustExist(cmd *cobra.Command, config *RuleConfig) []validator.ValidationError {
var errors []validator.ValidationError

for _, field := range p.Fields {
for _, field := range config.MustExist.Fields {
// reflects the field in cobra.Command struct
reflectValue := reflect.ValueOf(cmd).Elem().FieldByName(field)

Expand All @@ -38,7 +38,7 @@ func validateMustExist(cmd *cobra.Command, p MustExist, verbose bool) []validato
}

// validate field and append errors
errors = append(errors, validateByType(cmd, &reflectValue, field, cmd.CommandPath(), verbose)...)
errors = append(errors, validateByType(cmd, &reflectValue, field, cmd.CommandPath(), config.Verbose)...)
}
return errors
}
Expand Down

0 comments on commit 4ba3da6

Please sign in to comment.