gunk-dev/the-valley

host.go

source3af64cbb9cebmain's published tip history
3.0 KiB
1package main
2
3import (
4 "context"
5 "encoding/json"
6 "fmt"
7 "os"
8 "slices"
9 "strings"
10
11 "the-valley/exactjson"
12 "the-valley/web/export"
13)
14
15// hostDoc is the host declaration as cue exports it under
16// schema/valley.cue. The publisher reads the groups, and each project's
17// group, web name, former names and short aliases. The other fields are
18// taken as raw JSON, and exactjson holds every key to the schema's
19// spelling, so a Group beside a group is refused rather than read as it.
20type hostDoc struct {
21 Projects map[string]hostProject `json:"projects"`
22 Groups map[string]hostGroup `json:"groups"`
23 Backup json.RawMessage `json:"backup"`
24}
25
26type hostProject struct {
27 Git json.RawMessage `json:"git"`
28 Mirrors json.RawMessage `json:"mirrors"`
29 Protection json.RawMessage `json:"protection"`
30 Grants json.RawMessage `json:"grants"`
31 Group string `json:"group"`
32 Name string `json:"name"`
33 FormerNames []string `json:"formerNames"`
34 Aliases []string `json:"aliases"`
35}
36
37type hostGroup struct {
38 Name string `json:"name"`
39 Aliases []string `json:"aliases"`
40}
41
42// webPlaces are where the instance places its projects on the web: its
43// groups, in identifier order, and where it places each declared project.
44type webPlaces struct {
45 groups []export.Group
46 placements map[string]export.Placement
47}
48
49// readHost reads the host declaration at path and holds its groups and
50// projects to export.CheckNames. A declaration whose project's
51// identifier is not a name, whose names collide, or that names a group it
52// does not declare, is refused, and no pass runs: a project whose URL is
53// in doubt is not published.
54func readHost(path string) (webPlaces, error) {
55 var w webPlaces
56 data, err := os.ReadFile(path)
57 if err != nil {
58 return w, fmt.Errorf("--host: %w", err)
59 }
60 refuse := func(err error) (webPlaces, error) {
61 return webPlaces{}, fmt.Errorf("--host %s is not a host declaration as cue exports it: %w", path, err)
62 }
63 var doc hostDoc
64 if err := exactjson.Decode(data, &doc); err != nil {
65 return refuse(err)
66 }
67 w.placements = map[string]export.Placement{}
68 for name, p := range doc.Projects {
69 switch {
70 case p.Group == "" && p.Name != "" && p.Name != name:
71 return refuse(fmt.Errorf("the project %s is in no group, and its web name is %q", name, p.Name))
72 case p.Group == "":
73 // cue exports a project's identifier as its web name. A
74 // project in no group has none.
75 w.placements[name] = export.Placement{FormerNames: p.FormerNames, Aliases: p.Aliases}
76 default:
77 w.placements[name] = export.Placement{Group: p.Group, Name: p.Name, FormerNames: p.FormerNames, Aliases: p.Aliases}
78 }
79 }
80 for id, g := range doc.Groups {
81 w.groups = append(w.groups, export.Group{ID: id, Name: g.Name, Aliases: g.Aliases})
82 }
83 slices.SortFunc(w.groups, func(a, b export.Group) int { return strings.Compare(a.ID, b.ID) })
84 if err := export.CheckNames(context.Background(), w.groups, export.Sorted(w.placements)); err != nil {
85 return webPlaces{}, fmt.Errorf("--host %s is refused: %w", path, err)
86 }
87 return w, nil
88}