-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e66e146
commit e0f13a9
Showing
6 changed files
with
120 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package king | ||
|
||
import ( | ||
"io" | ||
"io/ioutil" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/alecthomas/kong" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// FileResolver represents a kong fileresolver. | ||
type FileResolver string | ||
|
||
// All supported file resolvers. | ||
const ( | ||
YAML FileResolver = "yaml" | ||
TOML FileResolver = "toml" | ||
) | ||
|
||
// NewFileResolver creates a new fileresolver. | ||
func NewFileResolver(f FileResolver) kong.ConfigurationLoader { | ||
switch f { | ||
case TOML: | ||
return tomlResolver | ||
default: | ||
return yamlResolver | ||
} | ||
} | ||
|
||
func yamlResolver(r io.Reader) (kong.Resolver, error) { | ||
values := map[string]interface{}{} | ||
|
||
err := yaml.NewDecoder(r).Decode(&values) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var f kong.ResolverFunc = func(ctx *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) { | ||
raw, ok := values[flag.Name] | ||
if !ok { | ||
return nil, nil | ||
} | ||
|
||
return raw, nil | ||
} | ||
|
||
return f, nil | ||
} | ||
|
||
func tomlResolver(r io.Reader) (kong.Resolver, error) { | ||
values := map[string]interface{}{} | ||
|
||
data, err := ioutil.ReadAll(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := toml.Unmarshal(data, &values); err != nil { | ||
return nil, err | ||
} | ||
|
||
var f kong.ResolverFunc = func(ctx *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) { | ||
raw, ok := values[flag.Name] | ||
if !ok { | ||
return nil, nil | ||
} | ||
return raw, nil | ||
} | ||
|
||
return f, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters