|
| 1 | +// Command docgen renders the CLI's own help into the website's markdown. |
| 2 | +// |
| 3 | +// Usage: |
| 4 | +// |
| 5 | +// go run ./cmd/docgen -out website/content |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "flag" |
| 11 | + "fmt" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "strings" |
| 15 | + |
| 16 | + "github.com/spf13/cobra" |
| 17 | + "github.com/spf13/cobra/doc" |
| 18 | + |
| 19 | + "github.com/Flagsmith/flagsmith-cli/v2/internal/cmd" |
| 20 | +) |
| 21 | + |
| 22 | +func main() { |
| 23 | + if err := run(os.Args); err != nil { |
| 24 | + fmt.Fprintln(os.Stderr, err) |
| 25 | + os.Exit(1) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func run(args []string) error { |
| 30 | + flags := flag.NewFlagSet(filepath.Base(args[0]), flag.ContinueOnError) |
| 31 | + out := flags.String("out", "", "directory to write markdown pages to (required)") |
| 32 | + if err := flags.Parse(args[1:]); err != nil { |
| 33 | + return err |
| 34 | + } |
| 35 | + if *out == "" { |
| 36 | + flags.Usage() |
| 37 | + return fmt.Errorf("-out not set") |
| 38 | + } |
| 39 | + |
| 40 | + // The directory is generated in full, so a deleted command leaves no page. |
| 41 | + if err := os.RemoveAll(*out); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + return write(cmd.Root(), *out) |
| 45 | +} |
| 46 | + |
| 47 | +// write renders cmd and its subcommands beneath dir. |
| 48 | +func write(cmd *cobra.Command, dir string) error { |
| 49 | + // Otherwise the page ends with cobra's "Auto generated ... on <date>", which |
| 50 | + // makes the build non-reproducible. |
| 51 | + cmd.DisableAutoGenTag = true |
| 52 | + |
| 53 | + var page bytes.Buffer |
| 54 | + // Hextra renders the front matter title as the page heading, and cobra opens |
| 55 | + // with one of its own, so drop cobra's to avoid printing it twice. |
| 56 | + fmt.Fprintf(&page, "---\ntitle: %q\nlinkTitle: %q\ndescription: %q\n", |
| 57 | + cmd.CommandPath(), cmd.Name(), cmd.Short) |
| 58 | + if !cmd.HasParent() { |
| 59 | + fmt.Fprint(&page, "sidebar:\n open: true\n") |
| 60 | + } |
| 61 | + fmt.Fprint(&page, "---\n\n") |
| 62 | + |
| 63 | + var body bytes.Buffer |
| 64 | + if err := doc.GenMarkdownCustom(cmd, &body, linker(cmd)); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + page.Write(dropHeading(body.Bytes())) |
| 68 | + |
| 69 | + // A command with subcommands owns a directory, so its page is that |
| 70 | + // directory's index; a leaf is a plain page beside its siblings. |
| 71 | + file := filepath.Join(dir, url(cmd.CommandPath())+".md") |
| 72 | + if cmd.HasAvailableSubCommands() { |
| 73 | + file = filepath.Join(dir, url(cmd.CommandPath()), "_index.md") |
| 74 | + } |
| 75 | + if err := os.MkdirAll(filepath.Dir(file), 0o755); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + if err := os.WriteFile(file, page.Bytes(), 0o644); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + for _, sub := range cmd.Commands() { |
| 83 | + if !sub.IsAvailableCommand() || sub.IsAdditionalHelpTopicCommand() { |
| 84 | + continue |
| 85 | + } |
| 86 | + if err := write(sub, dir); err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + } |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// url is a command path's page relative to the site root, so |
| 94 | +// "flagsmith flag update" is "flag/update" and "flagsmith" is ".". |
| 95 | +func url(commandPath string) string { |
| 96 | + return filepath.Join(strings.Fields(commandPath)[1:]...) |
| 97 | +} |
| 98 | + |
| 99 | +// linker turns the flat filenames cobra writes in its "SEE ALSO" links |
| 100 | +// (flagsmith_flag_update.md) into paths relative to the page being rendered, so |
| 101 | +// they hold up under the /flagsmith-cli/ prefix a project site is served under. |
| 102 | +// Command names contain no underscores, which TestCommandNamesAreURLSafe holds |
| 103 | +// true, so splitting on them recovers the command path. |
| 104 | +func linker(from *cobra.Command) func(string) string { |
| 105 | + return func(link string) string { |
| 106 | + to := strings.ReplaceAll(strings.TrimSuffix(link, ".md"), "_", " ") |
| 107 | + rel, err := filepath.Rel(url(from.CommandPath()), url(to)) |
| 108 | + if err != nil { |
| 109 | + return link |
| 110 | + } |
| 111 | + return filepath.ToSlash(rel) + "/" |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// dropHeading removes the "## <command path>" line cobra opens with, and the |
| 116 | +// blank line after it. |
| 117 | +func dropHeading(md []byte) []byte { |
| 118 | + if !bytes.HasPrefix(md, []byte("## ")) { |
| 119 | + return md |
| 120 | + } |
| 121 | + _, rest, found := bytes.Cut(md, []byte("\n")) |
| 122 | + if !found { |
| 123 | + return md |
| 124 | + } |
| 125 | + return bytes.TrimLeft(rest, "\n") |
| 126 | +} |
0 commit comments