gunk-dev/the-valley

host.go

sourcecfedff357a41main's published tip history
4.0 KiB
1package main
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "fmt"
9 "os"
10 "slices"
11 "strings"
12
13 "the-valley/exactjson"
14 "the-valley/web/export"
15)
16
17// hostDoc is the host declaration as cue exports it under
18// schema/valley.cue. The publisher reads the groups, and each project's
19// group, web name, former names and short aliases. The other fields are
20// taken as raw JSON, and exactjson holds every key to the schema's
21// spelling, so a Group beside a group is refused rather than read as it.
22// cue exports projects and groups always, and each declaration as an
23// object. Pointers tell an object from a null or a missing field, which
24// the publisher refuses.
25type hostDoc struct {
26 Projects *map[string]*hostProject `json:"projects"`
27 Groups *map[string]*hostGroup `json:"groups"`
28 Backup json.RawMessage `json:"backup"`
29}
30
31type hostProject struct {
32 Git json.RawMessage `json:"git"`
33 Mirrors json.RawMessage `json:"mirrors"`
34 Protection json.RawMessage `json:"protection"`
35 Grants json.RawMessage `json:"grants"`
36 Retain json.RawMessage `json:"retainChangeRecords"`
37 Group string `json:"group"`
38 Name string `json:"name"`
39 FormerNames []string `json:"formerNames"`
40 Aliases []string `json:"aliases"`
41}
42
43type hostGroup struct {
44 Name string `json:"name"`
45 Aliases []string `json:"aliases"`
46}
47
48// webPlaces are where the instance places its projects on the web: its
49// groups, in identifier order, and where it places each declared project.
50type webPlaces struct {
51 groups []export.Group
52 placements map[string]export.Placement
53}
54
55// readHost reads the host declaration at path and holds its groups and
56// projects to export.CheckNames. A declaration that is not an object
57// with projects and groups, or whose project or group is not an object,
58// is refused, as is one whose project's identifier is not a name, whose
59// names collide, or that names a group it does not declare. No pass runs
60// then: a pass over a declaration it cannot read would publish an export
61// without the projects it lost, and a project whose URL is in doubt is
62// not published.
63func readHost(path string) (webPlaces, error) {
64 var w webPlaces
65 data, err := os.ReadFile(path)
66 if err != nil {
67 return w, fmt.Errorf("--host: %w", err)
68 }
69 refuse := func(err error) (webPlaces, error) {
70 return webPlaces{}, fmt.Errorf("--host %s: invalid host declaration: %w", path, err)
71 }
72 // encoding/json reads a null into a struct as nothing at all, so the
73 // document opens an object, or it is refused.
74 if !bytes.HasPrefix(bytes.TrimLeft(data, " \t\r\n"), []byte("{")) {
75 return refuse(errors.New("the document must be a JSON object"))
76 }
77 var doc hostDoc
78 if err := exactjson.Decode(data, &doc); err != nil {
79 return refuse(err)
80 }
81 if doc.Projects == nil || doc.Groups == nil {
82 return refuse(errors.New("projects and groups are required and must be objects"))
83 }
84 w.placements = map[string]export.Placement{}
85 for name, p := range *doc.Projects {
86 switch {
87 case p == nil:
88 return refuse(fmt.Errorf("project %q is null", name))
89 case p.Group == "" && p.Name != "" && p.Name != name:
90 return refuse(fmt.Errorf("project %s sets web name %q without a group", name, p.Name))
91 case p.Group == "":
92 // cue exports a project's identifier as its web name. A
93 // project in no group has none.
94 w.placements[name] = export.Placement{FormerNames: p.FormerNames, Aliases: p.Aliases}
95 default:
96 w.placements[name] = export.Placement{Group: p.Group, Name: p.Name, FormerNames: p.FormerNames, Aliases: p.Aliases}
97 }
98 }
99 for id, g := range *doc.Groups {
100 if g == nil {
101 return refuse(fmt.Errorf("group %q is null", id))
102 }
103 w.groups = append(w.groups, export.Group{ID: id, Name: g.Name, Aliases: g.Aliases})
104 }
105 slices.SortFunc(w.groups, func(a, b export.Group) int { return strings.Compare(a.ID, b.ID) })
106 if err := export.CheckNames(context.Background(), w.groups, export.Sorted(w.placements)); err != nil {
107 return webPlaces{}, fmt.Errorf("--host %s is refused: %w", path, err)
108 }
109 return w, nil
110}