mirror of
https://github.com/aykhans/dodo.git
synced 2025-09-05 10:43:37 +00:00
Add config file support to CLI parser
Add -f/--config-file flag for loading YAML configs from local or remote sources. Fix error handling to return unmatched errors.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -52,14 +53,19 @@ Flags:
|
||||
-skip-verify bool Skip SSL/TLS certificate verification (default %v)`
|
||||
|
||||
type ConfigCLIParser struct {
|
||||
args []string
|
||||
args []string
|
||||
configFile *types.ConfigFile
|
||||
}
|
||||
|
||||
func NewConfigCLIParser(args []string) *ConfigCLIParser {
|
||||
if args == nil {
|
||||
args = []string{}
|
||||
}
|
||||
return &ConfigCLIParser{args}
|
||||
return &ConfigCLIParser{args: args}
|
||||
}
|
||||
|
||||
func (parser ConfigCLIParser) GetConfigFile() *types.ConfigFile {
|
||||
return parser.configFile
|
||||
}
|
||||
|
||||
type stringSliceArg []string
|
||||
@@ -85,6 +91,7 @@ func (parser *ConfigCLIParser) Parse() (*Config, error) {
|
||||
|
||||
var (
|
||||
config = &Config{}
|
||||
configFile string
|
||||
yes bool
|
||||
skipVerify bool
|
||||
method string
|
||||
@@ -101,6 +108,9 @@ func (parser *ConfigCLIParser) Parse() (*Config, error) {
|
||||
)
|
||||
|
||||
{
|
||||
flagSet.StringVar(&configFile, "config-file", "", "Config file")
|
||||
flagSet.StringVar(&configFile, "f", "", "Config file")
|
||||
|
||||
flagSet.BoolVar(&yes, "yes", false, "Answer yes to all questions")
|
||||
flagSet.BoolVar(&yes, "y", false, "Answer yes to all questions")
|
||||
|
||||
@@ -160,6 +170,29 @@ func (parser *ConfigCLIParser) Parse() (*Config, error) {
|
||||
// Iterate over flags that were explicitly set on the command line.
|
||||
flagSet.Visit(func(flagVar *flag.Flag) {
|
||||
switch flagVar.Name {
|
||||
case "config-file", "f":
|
||||
var err error
|
||||
parser.configFile, err = types.ParseConfigFile(configFile)
|
||||
_ = utils.HandleErrorOrDie(err,
|
||||
utils.OnSentinelError(types.ErrConfigFileExtensionNotFound, func(err error) error {
|
||||
fieldParseErrors = append(fieldParseErrors, *types.NewFieldParseError("config-file", errors.New("file extension not found")))
|
||||
return nil
|
||||
}),
|
||||
utils.OnCustomError(func(err types.RemoteConfigFileParseError) error {
|
||||
fieldParseErrors = append(fieldParseErrors, *types.NewFieldParseError("config-file", fmt.Errorf("parse error: %w", err)))
|
||||
return nil
|
||||
}),
|
||||
utils.OnCustomError(func(err types.UnknownConfigFileTypeError) error {
|
||||
fieldParseErrors = append(
|
||||
fieldParseErrors,
|
||||
*types.NewFieldParseError(
|
||||
"config-file",
|
||||
fmt.Errorf("file type '%s' not supported (supported types: %s)", err.Type, types.ConfigFileTypeYAML.String()),
|
||||
),
|
||||
)
|
||||
return nil
|
||||
}),
|
||||
)
|
||||
case "yes", "y":
|
||||
config.Yes = utils.ToPtr(yes)
|
||||
case "skip-verify":
|
||||
|
Reference in New Issue
Block a user