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:
@@ -15,7 +15,7 @@ const (
|
||||
func (t ConfigFileType) String() string {
|
||||
switch t {
|
||||
case ConfigFileTypeYAML:
|
||||
return "yaml"
|
||||
return "yaml/yml"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
@@ -57,6 +57,13 @@ func (configFile ConfigFile) LocationType() ConfigFileLocationType {
|
||||
return configFile.locationType
|
||||
}
|
||||
|
||||
// ParseConfigFile parses a raw string representing a configuration file
|
||||
// path or URL and returns a ConfigFile struct.
|
||||
// It determines the file's type and location (local or remote) based on the string.
|
||||
// It can return the following errors:
|
||||
// - ErrConfigFileExtensionNotFound
|
||||
// - RemoteConfigFileParseError
|
||||
// - UnknownConfigFileTypeError
|
||||
func ParseConfigFile(configFileRaw string) (*ConfigFile, error) {
|
||||
configFileParsed := &ConfigFile{
|
||||
path: configFileRaw,
|
||||
|
@@ -10,7 +10,7 @@ import (
|
||||
func TestConfigFileType_String(t *testing.T) {
|
||||
t.Run("ConfigFileTypeYAML returns yaml", func(t *testing.T) {
|
||||
configType := ConfigFileTypeYAML
|
||||
assert.Equal(t, "yaml", configType.String())
|
||||
assert.Equal(t, "yaml/yml", configType.String())
|
||||
})
|
||||
|
||||
t.Run("Unknown config file type returns unknown", func(t *testing.T) {
|
||||
@@ -146,7 +146,7 @@ func TestParseConfigFile(t *testing.T) {
|
||||
configFile, err := ParseConfigFile("config.json")
|
||||
|
||||
require.Error(t, err)
|
||||
assert.IsType(t, &UnknownConfigFileTypeError{}, err)
|
||||
assert.IsType(t, UnknownConfigFileTypeError{}, err)
|
||||
assert.Contains(t, err.Error(), "json")
|
||||
assert.Nil(t, configFile)
|
||||
})
|
||||
@@ -155,7 +155,7 @@ func TestParseConfigFile(t *testing.T) {
|
||||
configFile, err := ParseConfigFile("http://192.168.1.%30/config.yaml")
|
||||
|
||||
require.Error(t, err)
|
||||
assert.IsType(t, &RemoteConfigFileParseError{}, err)
|
||||
assert.IsType(t, RemoteConfigFileParseError{}, err)
|
||||
assert.Nil(t, configFile)
|
||||
})
|
||||
|
||||
@@ -171,7 +171,7 @@ func TestParseConfigFile(t *testing.T) {
|
||||
configFile, err := ParseConfigFile("https://example.com/config.txt")
|
||||
|
||||
require.Error(t, err)
|
||||
assert.IsType(t, &UnknownConfigFileTypeError{}, err)
|
||||
assert.IsType(t, UnknownConfigFileTypeError{}, err)
|
||||
assert.Contains(t, err.Error(), "txt")
|
||||
assert.Nil(t, configFile)
|
||||
})
|
||||
|
@@ -85,11 +85,11 @@ type RemoteConfigFileParseError struct {
|
||||
error error
|
||||
}
|
||||
|
||||
func NewRemoteConfigFileParseError(err error) *RemoteConfigFileParseError {
|
||||
func NewRemoteConfigFileParseError(err error) RemoteConfigFileParseError {
|
||||
if err == nil {
|
||||
err = ErrNoError
|
||||
}
|
||||
return &RemoteConfigFileParseError{err}
|
||||
return RemoteConfigFileParseError{err}
|
||||
}
|
||||
|
||||
func (e RemoteConfigFileParseError) Error() string {
|
||||
@@ -104,8 +104,8 @@ type UnknownConfigFileTypeError struct {
|
||||
Type string
|
||||
}
|
||||
|
||||
func NewUnknownConfigFileTypeError(_type string) *UnknownConfigFileTypeError {
|
||||
return &UnknownConfigFileTypeError{_type}
|
||||
func NewUnknownConfigFileTypeError(_type string) UnknownConfigFileTypeError {
|
||||
return UnknownConfigFileTypeError{_type}
|
||||
}
|
||||
|
||||
func (e UnknownConfigFileTypeError) Error() string {
|
||||
|
Reference in New Issue
Block a user