| 
 | 1 | +package main  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"context"  | 
 | 5 | +	_ "embed"  | 
 | 6 | +	"fmt"  | 
 | 7 | + | 
 | 8 | +	docs "github.com/urfave/cli-docs/v3"  | 
 | 9 | +	"github.com/urfave/cli/v3"  | 
 | 10 | +)  | 
 | 11 | + | 
 | 12 | +//go:embed markdown_tabular.md.gotmpl  | 
 | 13 | +var markdownTabularDocTemplate string  | 
 | 14 | + | 
 | 15 | +// We have a copy of this template taken from  | 
 | 16 | +// https://github.com/urfave/cli-docs where we remove column  | 
 | 17 | +// "Environment variables" if it has no values.  | 
 | 18 | +// TODO: remove this when https://github.com/urfave/cli-docs/pull/15  | 
 | 19 | +// is merged.  | 
 | 20 | +func init() {  | 
 | 21 | +	docs.MarkdownTabularDocTemplate = markdownTabularDocTemplate  | 
 | 22 | +}  | 
 | 23 | + | 
 | 24 | +var printManCommand = &cli.Command{  | 
 | 25 | +	Name:        "man",  | 
 | 26 | +	Usage:       "prints man file",  | 
 | 27 | +	Description: "Prints documentation of loop CLI in man format",  | 
 | 28 | +	Action:      printMan,  | 
 | 29 | +	Hidden:      true,  | 
 | 30 | +}  | 
 | 31 | + | 
 | 32 | +func printMan(_ context.Context, cmd *cli.Command) error {  | 
 | 33 | +	root := filterNestedHelpCommands(cmd.Root())  | 
 | 34 | + | 
 | 35 | +	const userCommandsSection = 1  | 
 | 36 | +	man, err := docs.ToManWithSection(root, userCommandsSection)  | 
 | 37 | +	if err != nil {  | 
 | 38 | +		return fmt.Errorf("failed to produce man: %w", err)  | 
 | 39 | +	}  | 
 | 40 | + | 
 | 41 | +	fmt.Println(man)  | 
 | 42 | + | 
 | 43 | +	return nil  | 
 | 44 | +}  | 
 | 45 | + | 
 | 46 | +var printMarkdownCommand = &cli.Command{  | 
 | 47 | +	Name:        "markdown",  | 
 | 48 | +	Usage:       "prints markdown file",  | 
 | 49 | +	Description: "Prints documentation of loop CLI in markdown format",  | 
 | 50 | +	Action:      printMarkdown,  | 
 | 51 | +	Hidden:      true,  | 
 | 52 | +}  | 
 | 53 | + | 
 | 54 | +func printMarkdown(_ context.Context, cmd *cli.Command) error {  | 
 | 55 | +	root := filterNestedHelpCommands(cmd.Root())  | 
 | 56 | + | 
 | 57 | +	md, err := docs.ToTabularMarkdown(root, "loop")  | 
 | 58 | +	if err != nil {  | 
 | 59 | +		return fmt.Errorf("failed to produce markdown: %w", err)  | 
 | 60 | +	}  | 
 | 61 | + | 
 | 62 | +	fmt.Println(md)  | 
 | 63 | + | 
 | 64 | +	return nil  | 
 | 65 | +}  | 
 | 66 | + | 
 | 67 | +// filterNestedHelpCommands clones cmd, drops nested help commands, and normalises  | 
 | 68 | +// flag defaults so generated documentation avoids absolute paths.  | 
 | 69 | +func filterNestedHelpCommands(cmd *cli.Command) *cli.Command {  | 
 | 70 | +	cloned := cloneCommand(cmd, 0)  | 
 | 71 | +	overrideDocFlags(cloned)  | 
 | 72 | +	return cloned  | 
 | 73 | +}  | 
 | 74 | + | 
 | 75 | +// cloneCommand clones the command, filtering out nested "help" subcommands.  | 
 | 76 | +func cloneCommand(cmd *cli.Command, depth int) *cli.Command {  | 
 | 77 | +	if cmd == nil {  | 
 | 78 | +		return nil  | 
 | 79 | +	}  | 
 | 80 | + | 
 | 81 | +	cloned := *cmd  | 
 | 82 | +	if len(cmd.Commands) == 0 {  | 
 | 83 | +		return &cloned  | 
 | 84 | +	}  | 
 | 85 | + | 
 | 86 | +	filtered := make([]*cli.Command, 0, len(cmd.Commands))  | 
 | 87 | +	for _, sub := range cmd.Commands {  | 
 | 88 | +		if sub == nil {  | 
 | 89 | +			continue  | 
 | 90 | +		}  | 
 | 91 | +		childDepth := depth + 1  | 
 | 92 | + | 
 | 93 | +		// TODO: remove when https://github.com/urfave/cli-docs/pull/16  | 
 | 94 | +		if childDepth > 0 && sub.Name == "help" {  | 
 | 95 | +			continue  | 
 | 96 | +		}  | 
 | 97 | + | 
 | 98 | +		filtered = append(filtered, cloneCommand(sub, childDepth))  | 
 | 99 | +	}  | 
 | 100 | + | 
 | 101 | +	cloned.Commands = filtered  | 
 | 102 | +	return &cloned  | 
 | 103 | +}  | 
 | 104 | + | 
 | 105 | +// overrideDocFlags walks the command tree and replaces string flag defaults  | 
 | 106 | +// that leak user-specific filesystem paths, keeping generated docs stable.  | 
 | 107 | +func overrideDocFlags(cmd *cli.Command) {  | 
 | 108 | +	if cmd == nil {  | 
 | 109 | +		return  | 
 | 110 | +	}  | 
 | 111 | + | 
 | 112 | +	if len(cmd.Flags) > 0 {  | 
 | 113 | +		clonedFlags := make([]cli.Flag, len(cmd.Flags))  | 
 | 114 | +		for i, fl := range cmd.Flags {  | 
 | 115 | +			clonedFlags[i] = cloneFlagWithOverrides(fl)  | 
 | 116 | +		}  | 
 | 117 | +		cmd.Flags = clonedFlags  | 
 | 118 | +	}  | 
 | 119 | + | 
 | 120 | +	for _, sub := range cmd.Commands {  | 
 | 121 | +		overrideDocFlags(sub)  | 
 | 122 | +	}  | 
 | 123 | +}  | 
 | 124 | + | 
 | 125 | +// docFlagOverrides maps global flag names to the canonical values we want to  | 
 | 126 | +// show in documentation instead of user-specific absolute paths.  | 
 | 127 | +var docFlagOverrides = map[string]string{  | 
 | 128 | +	loopDirFlag.Name:      "~/.loop",  | 
 | 129 | +	tlsCertFlag.Name:      "~/.loop/mainnet/tls.cert",  | 
 | 130 | +	macaroonPathFlag.Name: "~/.loop/mainnet/loop.macaroon",  | 
 | 131 | +}  | 
 | 132 | + | 
 | 133 | +// cloneFlagWithOverrides returns a copy of flag with overridden default values  | 
 | 134 | +// when the flag participates in docFlagOverrides. Non-string flags are reused  | 
 | 135 | +// unchanged to minimise allocations.  | 
 | 136 | +func cloneFlagWithOverrides(flag cli.Flag) cli.Flag {  | 
 | 137 | +	sf, ok := flag.(*cli.StringFlag)  | 
 | 138 | +	if !ok {  | 
 | 139 | +		return flag  | 
 | 140 | +	}  | 
 | 141 | + | 
 | 142 | +	value, ok := docFlagOverrides[sf.Name]  | 
 | 143 | +	if !ok {  | 
 | 144 | +		return flag  | 
 | 145 | +	}  | 
 | 146 | + | 
 | 147 | +	cloned := *sf  | 
 | 148 | +	cloned.Value = value  | 
 | 149 | +	cloned.DefaultText = value  | 
 | 150 | + | 
 | 151 | +	return &cloned  | 
 | 152 | +}  | 
0 commit comments