|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + _ "embed" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + docs "github.com/urfave/cli-docs/v3" |
| 10 | + "github.com/urfave/cli/v3" |
| 11 | +) |
| 12 | + |
| 13 | +//go:embed markdown_tabular.md.gotmpl |
| 14 | +var markdownTabularDocTemplate string |
| 15 | + |
| 16 | +// We have a copy of this template taken from |
| 17 | +// https://github.com/urfave/cli-docs where we remove column |
| 18 | +// "Environment variables" if it has no values. |
| 19 | +// TODO: remove this when https://github.com/urfave/cli-docs/pull/15 |
| 20 | +// is merged. |
| 21 | +func init() { |
| 22 | + docs.MarkdownTabularDocTemplate = markdownTabularDocTemplate |
| 23 | +} |
| 24 | + |
| 25 | +var printManCommand = &cli.Command{ |
| 26 | + Name: "man", |
| 27 | + Usage: "prints man file", |
| 28 | + Description: "Prints documentation of loop CLI in man format", |
| 29 | + Action: printMan, |
| 30 | + Hidden: true, |
| 31 | +} |
| 32 | + |
| 33 | +func printMan(_ context.Context, cmd *cli.Command) error { |
| 34 | + root := filterNestedHelpCommands(cmd.Root()) |
| 35 | + |
| 36 | + const userCommandsSection = 1 |
| 37 | + man, err := docs.ToManWithSection(root, userCommandsSection) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf("failed to produce man: %w", err) |
| 40 | + } |
| 41 | + |
| 42 | + // Replace the absolute path with "~/.loop" to make it reproducible. |
| 43 | + loopDir := cmd.String(loopDirFlag.Name) |
| 44 | + man = strings.ReplaceAll(man, loopDir, "~/.loop") |
| 45 | + |
| 46 | + fmt.Println(man) |
| 47 | + |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +var printMarkdownCommand = &cli.Command{ |
| 52 | + Name: "markdown", |
| 53 | + Usage: "prints markdown file", |
| 54 | + Description: "Prints documentation of loop CLI in markdown format", |
| 55 | + Action: printMarkdown, |
| 56 | + Hidden: true, |
| 57 | +} |
| 58 | + |
| 59 | +func printMarkdown(_ context.Context, cmd *cli.Command) error { |
| 60 | + root := filterNestedHelpCommands(cmd.Root()) |
| 61 | + |
| 62 | + md, err := docs.ToTabularMarkdown(root, "loop") |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to produce man: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + // Replace the absolute path with "~/.loop" to make it reproducible. |
| 68 | + // Add spaces to keep table alignment in markdown. |
| 69 | + loopDir := cmd.String(loopDirFlag.Name) |
| 70 | + from := loopDir |
| 71 | + to := "~/.loop" |
| 72 | + numSpaces := len(from) - len(to) |
| 73 | + from = "`" + from |
| 74 | + to = strings.Repeat(" ", numSpaces) + "`" + to |
| 75 | + md = strings.ReplaceAll(md, from, to) |
| 76 | + |
| 77 | + fmt.Println(md) |
| 78 | + |
| 79 | + return nil |
| 80 | +} |
| 81 | + |
| 82 | +// filterNestedHelpCommands returns a copy of cmd with nested "help" |
| 83 | +// sub-commands removed. |
| 84 | +// TODO: remove when https://github.com/urfave/cli-docs/pull/16 |
| 85 | +func filterNestedHelpCommands(cmd *cli.Command) *cli.Command { |
| 86 | + return cloneCommand(cmd, 0) |
| 87 | +} |
| 88 | + |
| 89 | +// cloneCommand clones the command, filtering out nested "help" subcommands. |
| 90 | +// TODO: remove when https://github.com/urfave/cli-docs/pull/16 |
| 91 | +func cloneCommand(cmd *cli.Command, depth int) *cli.Command { |
| 92 | + if cmd == nil { |
| 93 | + return nil |
| 94 | + } |
| 95 | + |
| 96 | + cloned := *cmd |
| 97 | + if len(cmd.Commands) == 0 { |
| 98 | + return &cloned |
| 99 | + } |
| 100 | + |
| 101 | + filtered := make([]*cli.Command, 0, len(cmd.Commands)) |
| 102 | + for _, sub := range cmd.Commands { |
| 103 | + if sub == nil { |
| 104 | + continue |
| 105 | + } |
| 106 | + childDepth := depth + 1 |
| 107 | + if childDepth > 0 && sub.Name == "help" { |
| 108 | + continue |
| 109 | + } |
| 110 | + filtered = append(filtered, cloneCommand(sub, childDepth)) |
| 111 | + } |
| 112 | + |
| 113 | + cloned.Commands = filtered |
| 114 | + return &cloned |
| 115 | +} |
0 commit comments