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:
@@ -20,6 +20,7 @@ func TestNewConfigCLIParser(t *testing.T) {
|
||||
|
||||
require.NotNil(t, parser)
|
||||
assert.Equal(t, args, parser.args)
|
||||
assert.Nil(t, parser.configFile)
|
||||
})
|
||||
|
||||
t.Run("NewConfigCLIParser with nil args", func(t *testing.T) {
|
||||
@@ -27,6 +28,7 @@ func TestNewConfigCLIParser(t *testing.T) {
|
||||
|
||||
require.NotNil(t, parser)
|
||||
assert.Equal(t, []string{}, parser.args)
|
||||
assert.Nil(t, parser.configFile)
|
||||
})
|
||||
|
||||
t.Run("NewConfigCLIParser with empty args", func(t *testing.T) {
|
||||
@@ -35,6 +37,21 @@ func TestNewConfigCLIParser(t *testing.T) {
|
||||
|
||||
require.NotNil(t, parser)
|
||||
assert.Equal(t, args, parser.args)
|
||||
assert.Nil(t, parser.configFile)
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigCLIParser_GetConfigFile(t *testing.T) {
|
||||
t.Run("GetConfigFile returns nil when no config file is set", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo"})
|
||||
assert.Nil(t, parser.GetConfigFile())
|
||||
})
|
||||
|
||||
t.Run("GetConfigFile returns config file when set", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo"})
|
||||
expectedConfigFile := &types.ConfigFile{}
|
||||
parser.configFile = expectedConfigFile
|
||||
assert.Equal(t, expectedConfigFile, parser.GetConfigFile())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -295,6 +312,58 @@ func TestConfigCLIParser_Parse(t *testing.T) {
|
||||
assert.Equal(t, 10*time.Second, *config.Timeout)
|
||||
})
|
||||
|
||||
t.Run("Parse with config-file flag valid YAML", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo", "-f", "/path/to/config.yaml"})
|
||||
config, err := parser.Parse()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
assert.NotNil(t, parser.GetConfigFile())
|
||||
})
|
||||
|
||||
t.Run("Parse with config-file flag using long form", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo", "--config-file", "/path/to/config.yml"})
|
||||
config, err := parser.Parse()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
assert.NotNil(t, parser.GetConfigFile())
|
||||
})
|
||||
|
||||
t.Run("Parse with config-file flag invalid extension", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo", "-f", "/path/to/config"})
|
||||
config, err := parser.Parse()
|
||||
|
||||
assert.Nil(t, config)
|
||||
var fieldErr types.FieldParseErrors
|
||||
require.ErrorAs(t, err, &fieldErr)
|
||||
assert.Len(t, fieldErr.Errors, 1)
|
||||
assert.Equal(t, "config-file", fieldErr.Errors[0].Field)
|
||||
assert.Contains(t, fieldErr.Errors[0].Err.Error(), "file extension not found")
|
||||
})
|
||||
|
||||
t.Run("Parse with config-file flag unsupported file type", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo", "-f", "/path/to/config.json"})
|
||||
config, err := parser.Parse()
|
||||
|
||||
assert.Nil(t, config)
|
||||
var fieldErr types.FieldParseErrors
|
||||
require.ErrorAs(t, err, &fieldErr)
|
||||
assert.Len(t, fieldErr.Errors, 1)
|
||||
assert.Equal(t, "config-file", fieldErr.Errors[0].Field)
|
||||
assert.Contains(t, fieldErr.Errors[0].Err.Error(), "file type")
|
||||
assert.Contains(t, fieldErr.Errors[0].Err.Error(), "not supported")
|
||||
})
|
||||
|
||||
t.Run("Parse with config-file flag remote URL", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{"dodo", "-f", "https://example.com/config.yaml"})
|
||||
config, err := parser.Parse()
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, config)
|
||||
assert.NotNil(t, parser.GetConfigFile())
|
||||
})
|
||||
|
||||
t.Run("Parse with all flags combined", func(t *testing.T) {
|
||||
parser := NewConfigCLIParser([]string{
|
||||
"dodo",
|
||||
@@ -311,6 +380,7 @@ func TestConfigCLIParser_Parse(t *testing.T) {
|
||||
"-c", "session=token123",
|
||||
"-b", `{"data": "test"}`,
|
||||
"-x", "http://proxy.example.com:3128",
|
||||
"-f", "/path/to/config.yaml",
|
||||
})
|
||||
config, err := parser.Parse()
|
||||
|
||||
@@ -341,6 +411,8 @@ func TestConfigCLIParser_Parse(t *testing.T) {
|
||||
|
||||
assert.Len(t, config.Proxies, 1)
|
||||
assert.Equal(t, "http://proxy.example.com:3128", config.Proxies[0].String())
|
||||
|
||||
assert.NotNil(t, parser.GetConfigFile())
|
||||
})
|
||||
|
||||
t.Run("Parse with multiple field parse errors", func(t *testing.T) {
|
||||
@@ -404,6 +476,7 @@ func TestConfigCLIParser_PrintHelp(t *testing.T) {
|
||||
assert.Contains(t, output, "-x, -proxy")
|
||||
assert.Contains(t, output, "-skip-verify")
|
||||
assert.Contains(t, output, "-y, -yes")
|
||||
assert.Contains(t, output, "-f, -config-file")
|
||||
|
||||
// Verify default values are included
|
||||
assert.Contains(t, output, Defaults.Method)
|
||||
|
Reference in New Issue
Block a user