From 25e9103ca662e89ccf8a47e7af2004d1a124577a Mon Sep 17 00:00:00 2001 From: Leo Antunes Date: Fri, 14 Jan 2022 16:16:25 +0100 Subject: [PATCH] fix: do not error on empty files this should work around the following go-yaml issue: https://github.com/go-yaml/yaml/issues/805 --- yaml.go | 3 ++- yaml_test.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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) +}