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:
@@ -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})
|
||||
|
||||
Reference in New Issue
Block a user