| 1 | package main |
| 2 | |
| 3 | import ( |
| 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 |
| 17 | var 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). |
| 22 | var helpTopics = map[string][]string{ |
| 23 | "pending": {"pending", "runtime"}, |
| 24 | "changes": {"changes", "runtime"}, |
| 25 | "review": {"review", "naming", "runtime"}, |
| 26 | "approve": {"approve", "naming", "runtime"}, |
| 27 | "rebase": {"rebase", "naming", "runtime"}, |
| 28 | "status": {"status", "naming", "runtime"}, |
| 29 | "ready": {"ready", "naming", "runtime"}, |
| 30 | "unready": {"unready", "requests", "naming", "runtime"}, |
| 31 | "withdraw": {"withdraw", "requests", "naming", "runtime"}, |
| 32 | "knot": {"knot", "runtime"}, |
| 33 | "plan": {"plan", "runtime"}, |
| 34 | "checks": {"checks", "runtime"}, |
| 35 | "tail": {"tail", "runtime"}, |
| 36 | "replay": {"replay", "runtime"}, |
| 37 | "self-update": {"self-update", "runtime"}, |
| 38 | "telemetry": {"telemetry"}, |
| 39 | } |
| 40 | |
| 41 | // publicVerb reports whether verb is one of valley's public verbs. |
| 42 | func publicVerb(verb string) bool { |
| 43 | _, ok := helpTopics[verb] |
| 44 | return ok |
| 45 | } |
| 46 | |
| 47 | // helpPart is the text of the help file name.txt. |
| 48 | func helpPart(name string) string { |
| 49 | data, err := helpFiles.ReadFile("help/" + name + ".txt") |
| 50 | if err != nil { |
| 51 | panic(err) // every part is embedded, so this is a build that lost one |
| 52 | } |
| 53 | return string(data) |
| 54 | } |
| 55 | |
| 56 | // helpText is the help topic verb prints, or the usage for "": its parts |
| 57 | // with their trailing blank lines dropped and one newline after them. |
| 58 | func helpText(verb string) string { |
| 59 | parts := []string{"usage"} |
| 60 | if verb != "" { |
| 61 | parts = helpTopics[verb] |
| 62 | } |
| 63 | var b strings.Builder |
| 64 | for _, p := range parts { |
| 65 | b.WriteString(helpPart(p)) |
| 66 | } |
| 67 | return strings.TrimRight(b.String(), "\n") + "\n" |
| 68 | } |
| 69 | |
| 70 | // runHelp is valley help [<verb>]. help, -h and --help name the usage too. |
| 71 | // What it prints goes through droppable, so a reader that stops early, |
| 72 | // as in `valley help review | head -n 1`, leaves the exit status as it was. |
| 73 | func runHelp(args []string, stdout, stderr io.Writer) int { |
| 74 | if len(args) > 1 { |
| 75 | fmt.Fprintln(droppable(stderr), "valley: usage: valley help [<verb>]") |
| 76 | return 1 |
| 77 | } |
| 78 | topic := "" |
| 79 | if len(args) == 1 { |
| 80 | topic = args[0] |
| 81 | } |
| 82 | switch { |
| 83 | case topic == "help" || topic == "-h" || topic == "--help": |
| 84 | topic = "" |
| 85 | case topic != "" && !publicVerb(topic): |
| 86 | fmt.Fprintf(droppable(stderr), "valley: unknown help topic: %s (try 'valley help')\n", topic) |
| 87 | return 1 |
| 88 | } |
| 89 | io.WriteString(droppable(stdout), helpText(topic)) |
| 90 | return 0 |
| 91 | } |