Skip to content

Commit

Permalink
Reduce Args() complexity for 'config write' command (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
TylerHendrickson authored Jun 7, 2022
1 parent 0c011ff commit ed869f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
23 changes: 5 additions & 18 deletions cmd/mydyndns/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,7 @@ useful for generating config file templates).`,
if err := cobra.MinimumNArgs(1)(cmd, args); err != nil {
return err
}

supportedExts := internal.NewStringCollection(viper.SupportedExts...)
for _, arg := range args {
ext := filepath.Ext(arg)
if len(ext) == 0 {
ext = arg
} else {
ext = ext[1:]
}

if !supportedExts.Contains(ext) {
return viper.UnsupportedConfigError(ext)
}
}

return nil
return validateConfigFileNames(args)
},
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
seenArgs := internal.NewStringCollection(args...)
Expand Down Expand Up @@ -104,7 +89,8 @@ useful for generating config file templates).`,
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if viper.GetBool("validate") {
return firstValidationError(cmd, validateAPIKey, validateBaseURL, validatePollInterval)
return firstValidationError(cmd,
validateAPIKey, validateBaseURL, validatePollInterval)
}
return nil
},
Expand Down Expand Up @@ -279,7 +265,8 @@ func newConfigValidateCmd() *cobra.Command {
Long: `The validate subcommand isolates the configuration checks executed when the mydyndns agent starts. Use this to
check whether the agent would fail to start due to invalid configuration, without actually running the agent.`,
RunE: func(cmd *cobra.Command, args []string) error {
return firstValidationError(cmd, validateAPIKey, validateBaseURL, validatePollInterval)
return firstValidationError(cmd,
validateAPIKey, validateBaseURL, validatePollInterval)
},
}
}
21 changes: 21 additions & 0 deletions cmd/mydyndns/cli/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cli

import (
"fmt"
"path/filepath"
"strings"

"github.com/TylerHendrickson/mydyndns/internal"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -39,3 +41,22 @@ func firstValidationError(cmd *cobra.Command, validators ...func(*cobra.Command)
}
return nil
}

// validateConfigFileNames ensures that all strings represent a valid Viper extension.
// Each string must be a supported extension ("json") or end in a supported extension ("foo.json").
// The first value encountered that does not represent a valid Viper extension returns
// viper.UnsupportedConfigError.
func validateConfigFileNames(s []string) error {
supportedExts := internal.NewStringCollection(viper.SupportedExts...)
for _, toValidate := range s {
if ext := filepath.Ext(toValidate); len(ext) > 0 {
toValidate = ext[1:]
}

if !supportedExts.Contains(toValidate) {
return viper.UnsupportedConfigError(toValidate)
}
}

return nil
}

0 comments on commit ed869f3

Please sign in to comment.