diff --git a/yaml.go b/yaml.go index 5355f11..22c505c 100644 --- a/yaml.go +++ b/yaml.go @@ -1,6 +1,7 @@ package kongyaml import ( + "errors" "fmt" "io" "strings" @@ -14,7 +15,7 @@ func Loader(r io.Reader) (kong.Resolver, error) { decoder := yaml.NewDecoder(r) config := map[interface{}]interface{}{} err := decoder.Decode(config) - if err != nil { + if err != nil && !errors.Is(err, io.EOF) { return nil, fmt.Errorf("YAML config decode error: %w", err) } return kong.ResolverFunc(func(context *kong.Context, parent *kong.Path, flag *kong.Flag) (interface{}, error) { diff --git a/yaml_test.go b/yaml_test.go index 1686d8e..b08c6ab 100644 --- a/yaml_test.go +++ b/yaml_test.go @@ -63,3 +63,21 @@ dict: } require.Equal(t, expected, cli) } + +func TestEmptyFile(t *testing.T) { + type CLI struct { + FlagName string + } + var cli CLI + r := strings.NewReader("") + resolver, err := Loader(r) + require.NoError(t, err) + parser, err := kong.New(&cli, kong.Resolvers(resolver)) + require.NoError(t, err) + _, err = parser.Parse([]string{}) + require.NoError(t, err) + expected := CLI{ + FlagName: "", + } + require.Equal(t, expected, cli) +}