diff --git a/internal/inventory/fillimport_test.go b/internal/inventory/fillimport_test.go index c4c838b..887b189 100644 --- a/internal/inventory/fillimport_test.go +++ b/internal/inventory/fillimport_test.go @@ -33,6 +33,19 @@ func TestParseYAMLWebsiteOnly(t *testing.T) { } } +func TestParseYAMLDashWithoutSpace(t *testing.T) { + doc := "inventory:\n-url: https://listmonk.app/\n-url: https://matomo.org/\n relationship: competitor\n" + entries, err := ParseYAML([]byte(doc), Partner) + if err != nil || len(entries) != 2 || entries[0].Websites[0] != "https://listmonk.app/" || + entries[0].Relationship != Partner || entries[1].Relationship != Competitor { + t.Fatalf("%+v %v", entries, err) + } + // Still not a list: the error says what one looks like. + if _, err := ParseYAML([]byte("inventory:\n url: [\n"), Partner); err == nil || !strings.Contains(err.Error(), "a dash and a space") { + t.Errorf("error: %v", err) + } +} + func testDB(t *testing.T) *db.DB { t.Helper() d, err := db.Open(filepath.Join(t.TempDir(), "app.db")) diff --git a/internal/inventory/inventory.go b/internal/inventory/inventory.go index 8864357..55e7570 100644 --- a/internal/inventory/inventory.go +++ b/internal/inventory/inventory.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "regexp" "strings" "time" @@ -256,17 +257,19 @@ type ImportResult struct { // ParseYAML reads entries from YAML: a plain list, or a document with an // `inventory:` or (old config) `applications:` list. defaultRel is used for // entries without a relationship. +// +// A list typed by hand with no space after the dashes (`-url: …`) is read +// as if it had them. func ParseYAML(data []byte, defaultRel string) ([]Entry, error) { - var list []Entry - if err := yaml.Unmarshal(data, &list); err != nil { - var doc struct { - Inventory []Entry `yaml:"inventory"` - Applications []Entry `yaml:"applications"` + list, err := parseList(data) + if err != nil { + fixed := dashNoSpace.ReplaceAll(data, []byte("$1- $2")) + if string(fixed) == string(data) { + return nil, err } - if err2 := yaml.Unmarshal(data, &doc); err2 != nil { - return nil, fmt.Errorf("not a Market list: %w", err2) + if list, err = parseList(fixed); err != nil { + return nil, err } - list = append(doc.Inventory, doc.Applications...) } for i := range list { if strings.TrimSpace(list[i].Relationship) == "" { @@ -311,6 +314,24 @@ func Import(d *db.DB, entries []Entry, overwrite bool) ImportResult { return r } +// dashNoSpace is a list item's dash with no space before its first key. +var dashNoSpace = regexp.MustCompile(`(?m)^([ \t]*)-([A-Za-z_]+:)`) + +func parseList(data []byte) ([]Entry, error) { + var list []Entry + if err := yaml.Unmarshal(data, &list); err == nil { + return list, nil + } + var doc struct { + Inventory []Entry `yaml:"inventory"` + Applications []Entry `yaml:"applications"` + } + if err := yaml.Unmarshal(data, &doc); err != nil { + return nil, fmt.Errorf("not a Market list (each entry starts with a dash and a space, as in \"- url: https://…\"): %w", err) + } + return append(doc.Inventory, doc.Applications...), nil +} + // ExportYAML writes the entries in the format ParseYAML reads. func ExportYAML(entries []Entry) ([]byte, error) { return yaml.Marshal(map[string][]Entry{"inventory": entries})