Skip to content

Commit

Permalink
fix: do not error on empty files
Browse files Browse the repository at this point in the history
this should work around the following go-yaml issue:
go-yaml/yaml#805
  • Loading branch information
costela committed Jan 14, 2022
1 parent 429470e commit 93dd765
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion yaml.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kongyaml

import (
"errors"
"fmt"
"io"
"strings"
Expand All @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 93dd765

Please sign in to comment.