gunk-dev/the-valley

help.go

source28abc5737165main's published tip history
3.0 KiB
1package main
2
3import (
4 "embed"
5 "fmt"
6 "io"
7 "strings"
8)
9
10// The help text, one file per part under help/. usage.txt is what
11// `valley help` prints; each public verb's topic is its own file and the
12// parts it shares with other topics: naming.txt, how a change is named,
13// requests.txt, what unready and withdraw do to a pending request, and
14// runtime.txt, what the command needs.
15//
16//go:embed help/*.txt
17var helpFiles embed.FS
18
19// helpTopics is each public verb's help topic, as the parts it prints in
20// order. The public verbs are exactly its keys: each is in usage.txt and
21// has a topic, and only they write a command record (command.go). flow,
22// whose helper replaces the process, writes none, and its help flags reach
23// that helper rather than its topic.
24var helpTopics = map[string][]string{
25 "pending": {"pending", "runtime"},
26 "changes": {"changes", "runtime"},
27 "review": {"review", "naming", "runtime"},
28 "approve": {"approve", "naming", "runtime"},
29 "rebase": {"rebase", "naming", "runtime"},
30 "status": {"status", "naming", "runtime"},
31 "ready": {"ready", "naming", "runtime"},
32 "unready": {"unready", "requests", "naming", "runtime"},
33 "withdraw": {"withdraw", "requests", "naming", "runtime"},
34 "knot": {"knot", "runtime"},
35 "plan": {"plan", "runtime"},
36 "checks": {"checks", "runtime"},
37 "tail": {"tail", "runtime"},
38 "replay": {"replay", "runtime"},
39 "self-update": {"self-update", "runtime"},
40 "telemetry": {"telemetry"},
41 "flow": {"flow"},
42}
43
44// publicVerb reports whether verb is one of valley's public verbs.
45func publicVerb(verb string) bool {
46 _, ok := helpTopics[verb]
47 return ok
48}
49
50// helpPart is the text of the help file name.txt.
51func helpPart(name string) string {
52 data, err := helpFiles.ReadFile("help/" + name + ".txt")
53 if err != nil {
54 panic(err) // every part is embedded, so this is a build that lost one
55 }
56 return string(data)
57}
58
59// helpText is the help topic verb prints, or the usage for "": its parts
60// with their trailing blank lines dropped and one newline after them.
61func helpText(verb string) string {
62 parts := []string{"usage"}
63 if verb != "" {
64 parts = helpTopics[verb]
65 }
66 var b strings.Builder
67 for _, p := range parts {
68 b.WriteString(helpPart(p))
69 }
70 return strings.TrimRight(b.String(), "\n") + "\n"
71}
72
73// runHelp is valley help [<verb>]. help, -h and --help name the usage too.
74// What it prints goes through droppable, so a reader that stops early,
75// as in `valley help review | head -n 1`, leaves the exit status as it was.
76func runHelp(args []string, stdout, stderr io.Writer) int {
77 if len(args) > 1 {
78 fmt.Fprintln(droppable(stderr), "valley: usage: valley help [<verb>]")
79 return 1
80 }
81 topic := ""
82 if len(args) == 1 {
83 topic = args[0]
84 }
85 switch {
86 case topic == "help" || topic == "-h" || topic == "--help":
87 topic = ""
88 case topic != "" && !publicVerb(topic):
89 fmt.Fprintf(droppable(stderr), "valley: unknown help topic: %s (try 'valley help')\n", topic)
90 return 1
91 }
92 io.WriteString(droppable(stdout), helpText(topic))
93 return 0
94}