carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package changelog
2
3import (
4 "bytes"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "io/fs"
9 "os"
10 "strings"
11)
12
13// LoadPeople reads people.json, a map from address to display name.
14// Addresses are matched case-insensitively.
15func LoadPeople(path string) (People, error) {
16 raw, err := os.ReadFile(path)
17 if err != nil {
18 return nil, err
19 }
20 var m map[string]string
21 if err := json.Unmarshal(raw, &m); err != nil {
22 return nil, fmt.Errorf("%s: %w", path, err)
23 }
24 p := People{}
25 for k, v := range m {
26 if k == "//" {
27 continue
28 }
29 p[strings.ToLower(k)] = v
30 }
31 return p, nil
32}
33
34// LoadLinks reads changelog-links.json. A missing file is no links.
35func LoadLinks(path string) (Links, error) {
36 raw, err := os.ReadFile(path)
37 if errors.Is(err, fs.ErrNotExist) {
38 return Links{}, nil
39 }
40 if err != nil {
41 return nil, err
42 }
43 var m map[string]json.RawMessage
44 if err := json.Unmarshal(raw, &m); err != nil {
45 return nil, fmt.Errorf("%s: %w", path, err)
46 }
47 l := Links{}
48 for tag, v := range m {
49 if tag == "//" {
50 continue
51 }
52 var byLead map[string][]string
53 if err := json.Unmarshal(v, &byLead); err != nil {
54 return nil, fmt.Errorf("%s: %s: %w", path, tag, err)
55 }
56 l[tag] = byLead
57 }
58 return l, nil
59}
60
61// Load reads a committed changelog.json.
62func Load(path string) (*Changelog, error) {
63 raw, err := os.ReadFile(path)
64 if err != nil {
65 return nil, err
66 }
67 var c Changelog
68 dec := json.NewDecoder(bytes.NewReader(raw))
69 dec.DisallowUnknownFields()
70 if err := dec.Decode(&c); err != nil {
71 return nil, fmt.Errorf("%s: %w", path, err)
72 }
73 return &c, nil
74}
75
76// Marshal is the committed form: indented, no HTML escaping (the notes
77// carry "<app>" placeholders), and a trailing newline.
78func Marshal(c *Changelog) ([]byte, error) {
79 var b bytes.Buffer
80 enc := json.NewEncoder(&b)
81 enc.SetEscapeHTML(false)
82 enc.SetIndent("", " ")
83 if err := enc.Encode(c); err != nil {
84 return nil, err
85 }
86 return b.Bytes(), nil
87}
88