diff --git a/internal/config.go b/internal/config.go index a76c9ff..e3870eb 100644 --- a/internal/config.go +++ b/internal/config.go @@ -195,15 +195,23 @@ func contains(s []string, e string) bool { // ParseConfigFromFolder parses the given folder for // a valid ew config, or returns the default (empty) // config if none can be found. -func ParseConfigFromFolder(path string) Config { +func ParseConfigFromFolder(output io.Writer, path string) Config { yamlConf, err := parseConfigFromYaml(path) if err == nil { return yamlConf + } else if errors.Is(err, io.EOF) { + fmt.Fprintln(output, color.YellowString("Skipping empty yaml config in %s", path)) + } else if !errors.Is(err, os.ErrNotExist) { + fmt.Fprintln(output, color.RedString("Failed to read yaml config in %s: %s", path, err)) } jsonConf, err := parseConfigFromJson(path) if err == nil { return jsonConf + } else if errors.Is(err, io.EOF) { + fmt.Fprintln(output, color.YellowString("Skipping empty json config in %s", path)) + } else if !errors.Is(err, os.ErrNotExist) { + fmt.Fprintln(output, color.RedString("Failed to read json config in %s: %s", path, err)) } // If no config is found, use default yaml diff --git a/internal/config_test.go b/internal/config_test.go index 9726680..b9a4f30 100644 --- a/internal/config_test.go +++ b/internal/config_test.go @@ -1,6 +1,8 @@ package internal import ( + "bytes" + "fmt" "io/ioutil" "os" "path/filepath" @@ -233,9 +235,16 @@ func writeTempFile(t *testing.T, filename string, fileString string) string { func TestParseConfigFromFolder(t *testing.T) { t.Run("no config found", func(t *testing.T) { want := Config{Source: YamlSrc, LoadedFrom: "does-not-exist", Tags: map[string][]string{}} - if got := ParseConfigFromFolder("does-not-exist"); !reflect.DeepEqual(got, want) { + wantOutput := "" + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, "does-not-exist"); !reflect.DeepEqual(got, want) { t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } }) t.Run("json config found, and ok", func(t *testing.T) { @@ -247,9 +256,16 @@ func TestParseConfigFromFolder(t *testing.T) { }() want := Config{Source: JsonSrc, LoadedFrom: folder, Tags: Tags{"some-tag": []string{"path1", "path2"}}} - if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) { + wantOutput := "" + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) { t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } }) t.Run("json config found, but borked", func(t *testing.T) { @@ -259,9 +275,41 @@ func TestParseConfigFromFolder(t *testing.T) { }() want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}} - if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) { + wantOutput := fmt.Sprintf( + "Failed to read json config in %s: invalid character ']' looking for beginning of object key string\n", + folder, + ) + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) { t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } + }) + + t.Run("json config found, but EOF", func(t *testing.T) { + folder := writeTempFile(t, ".ewconfig.json", ``) + defer func() { + clearFolder(t, folder) + }() + + want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}} + wantOutput := fmt.Sprintf( + "Skipping empty json config in %s\n", + folder, + ) + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) { + t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) + } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } }) t.Run("yaml config found, and ok", func(t *testing.T) { @@ -278,9 +326,16 @@ tags: }() want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: Tags{"some-tag": []string{"path1", "path2"}}} - if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) { + wantOutput := "" + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) { t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } }) t.Run("yaml config found, but borked", func(t *testing.T) { @@ -290,8 +345,40 @@ tags: }() want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}} - if got := ParseConfigFromFolder(folder); !reflect.DeepEqual(got, want) { + wantOutput := fmt.Sprintf( + "Failed to read yaml config in %s: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `t{]` into internal.Config\n", + folder, + ) + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) { t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } + }) + + t.Run("yaml config found, but EOF", func(t *testing.T) { + folder := writeTempFile(t, ".ewconfig.yml", ``) + defer func() { + clearFolder(t, folder) + }() + + want := Config{Source: YamlSrc, LoadedFrom: folder, Tags: map[string][]string{}} + wantOutput := fmt.Sprintf( + "Skipping empty yaml config in %s\n", + folder, + ) + + output := &bytes.Buffer{} + if got := ParseConfigFromFolder(output, folder); !reflect.DeepEqual(got, want) { + t.Errorf("ParseConfigFromFolder() = %v, want %v", got, want) + } + + if got := output.String(); got != wantOutput { + t.Errorf("ParseConfigFromFolder() output = %v, want %v", got, wantOutput) + } }) } diff --git a/main.go b/main.go index f753e53..d3392fe 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ func main() { os.Exit(1) } - conf := internal.ParseConfigFromFolder(home) + conf := internal.ParseConfigFromFolder(color.Output, home) exec, err := cmd.ParseCommand(color.Output, conf, os.Args[1:]) if err != nil {