Market import: read "-url:" typed without a space after the dash
A hand-typed list with no space after its dashes was an error about a key defined twice. It's now read as meant; a list that still can't be read says what an entry looks like. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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 {
|
func testDB(t *testing.T) *db.DB {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -256,17 +257,19 @@ type ImportResult struct {
|
|||||||
// ParseYAML reads entries from YAML: a plain list, or a document with an
|
// ParseYAML reads entries from YAML: a plain list, or a document with an
|
||||||
// `inventory:` or (old config) `applications:` list. defaultRel is used for
|
// `inventory:` or (old config) `applications:` list. defaultRel is used for
|
||||||
// entries without a relationship.
|
// 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) {
|
func ParseYAML(data []byte, defaultRel string) ([]Entry, error) {
|
||||||
var list []Entry
|
list, err := parseList(data)
|
||||||
if err := yaml.Unmarshal(data, &list); err != nil {
|
if err != nil {
|
||||||
var doc struct {
|
fixed := dashNoSpace.ReplaceAll(data, []byte("$1- $2"))
|
||||||
Inventory []Entry `yaml:"inventory"`
|
if string(fixed) == string(data) {
|
||||||
Applications []Entry `yaml:"applications"`
|
return nil, err
|
||||||
}
|
}
|
||||||
if err2 := yaml.Unmarshal(data, &doc); err2 != nil {
|
if list, err = parseList(fixed); err != nil {
|
||||||
return nil, fmt.Errorf("not a Market list: %w", err2)
|
return nil, err
|
||||||
}
|
}
|
||||||
list = append(doc.Inventory, doc.Applications...)
|
|
||||||
}
|
}
|
||||||
for i := range list {
|
for i := range list {
|
||||||
if strings.TrimSpace(list[i].Relationship) == "" {
|
if strings.TrimSpace(list[i].Relationship) == "" {
|
||||||
@@ -311,6 +314,24 @@ func Import(d *db.DB, entries []Entry, overwrite bool) ImportResult {
|
|||||||
return r
|
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.
|
// ExportYAML writes the entries in the format ParseYAML reads.
|
||||||
func ExportYAML(entries []Entry) ([]byte, error) {
|
func ExportYAML(entries []Entry) ([]byte, error) {
|
||||||
return yaml.Marshal(map[string][]Entry{"inventory": entries})
|
return yaml.Marshal(map[string][]Entry{"inventory": entries})
|
||||||
|
|||||||
Reference in New Issue
Block a user