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