gunk-dev/the-valley

host.go

sourcec78ce0b36d72main's published tip history
3.9 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 Group string `json:"group"`
37 Name string `json:"name"`
38 FormerNames []string `json:"formerNames"`
39 Aliases []string `json:"aliases"`
40}
41
42type hostGroup struct {
43 Name string `json:"name"`
44 Aliases []string `json:"aliases"`
45}
46
47// webPlaces are where the instance places its projects on the web: its
48// groups, in identifier order, and where it places each declared project.
49type webPlaces struct {
50 groups []export.Group
51 placements map[string]export.Placement
52}
53
54// readHost reads the host declaration at path and holds its groups and
55// projects to export.CheckNames. A declaration that is not an object
56// with projects and groups, or whose project or group is not an object,
57// is refused, as is one whose project's identifier is not a name, whose
58// names collide, or that names a group it does not declare. No pass runs
59// then: a pass over a declaration it cannot read would publish an export
60// without the projects it lost, and a project whose URL is in doubt is
61// not published.
62func readHost(path string) (webPlaces, error) {
63 var w webPlaces
64 data, err := os.ReadFile(path)
65 if err != nil {
66 return w, fmt.Errorf("--host: %w", err)
67 }
68 refuse := func(err error) (webPlaces, error) {
69 return webPlaces{}, fmt.Errorf("--host %s: invalid host declaration: %w", path, err)
70 }
71 // encoding/json reads a null into a struct as nothing at all, so the
72 // document opens an object, or it is refused.
73 if !bytes.HasPrefix(bytes.TrimLeft(data, " \t\r\n"), []byte("{")) {
74 return refuse(errors.New("the document must be a JSON object"))
75 }
76 var doc hostDoc
77 if err := exactjson.Decode(data, &doc); err != nil {
78 return refuse(err)
79 }
80 if doc.Projects == nil || doc.Groups == nil {
81 return refuse(errors.New("projects and groups are required and must be objects"))
82 }
83 w.placements = map[string]export.Placement{}
84 for name, p := range *doc.Projects {
85 switch {
86 case p == nil:
87 return refuse(fmt.Errorf("project %q is null", name))
88 case p.Group == "" && p.Name != "" && p.Name != name:
89 return refuse(fmt.Errorf("project %s sets web name %q without a group", name, p.Name))
90 case p.Group == "":
91 // cue exports a project's identifier as its web name. A
92 // project in no group has none.
93 w.placements[name] = export.Placement{FormerNames: p.FormerNames, Aliases: p.Aliases}
94 default:
95 w.placements[name] = export.Placement{Group: p.Group, Name: p.Name, FormerNames: p.FormerNames, Aliases: p.Aliases}
96 }
97 }
98 for id, g := range *doc.Groups {
99 if g == nil {
100 return refuse(fmt.Errorf("group %q is null", id))
101 }
102 w.groups = append(w.groups, export.Group{ID: id, Name: g.Name, Aliases: g.Aliases})
103 }
104 slices.SortFunc(w.groups, func(a, b export.Group) int { return strings.Compare(a.ID, b.ID) })
105 if err := export.CheckNames(context.Background(), w.groups, export.Sorted(w.placements)); err != nil {
106 return webPlaces{}, fmt.Errorf("--host %s is refused: %w", path, err)
107 }
108 return w, nil
109}