|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "cmp" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/sourcegraph/sourcegraph/lib/docgen" |
| 10 | +) |
| 11 | + |
| 12 | +// rootCommand is a top-level 'src' command as shown in 'src help'. It is the |
| 13 | +// single source for the command list in the help text and for the tests that |
| 14 | +// keep 'src help' and the 'src doc' root index in sync. |
| 15 | +type rootCommand struct { |
| 16 | + name string |
| 17 | + aliases []string |
| 18 | + description string |
| 19 | +} |
| 20 | + |
| 21 | +// rootCommands returns every visible top-level command, whether it is |
| 22 | +// registered with the legacy commander (commands) or with urfave/cli |
| 23 | +// (migratedCommands), sorted by name. |
| 24 | +func rootCommands() []rootCommand { |
| 25 | + var root []rootCommand |
| 26 | + |
| 27 | + for _, cmd := range commands { |
| 28 | + if cmd.hidden { |
| 29 | + continue |
| 30 | + } |
| 31 | + name := cmd.flagSet.Name() |
| 32 | + var aliases []string |
| 33 | + for _, alias := range cmd.aliases { |
| 34 | + // Some legacy commands register their own name as an alias. |
| 35 | + if alias != name { |
| 36 | + aliases = append(aliases, alias) |
| 37 | + } |
| 38 | + } |
| 39 | + root = append(root, rootCommand{ |
| 40 | + name: name, |
| 41 | + aliases: aliases, |
| 42 | + description: cmd.description, |
| 43 | + }) |
| 44 | + } |
| 45 | + |
| 46 | + for _, cmd := range docgen.VisibleCommands(migratedRootCommand().Commands) { |
| 47 | + root = append(root, rootCommand{ |
| 48 | + name: cmd.Name, |
| 49 | + aliases: slices.Clone(cmd.Aliases), |
| 50 | + description: cmd.Usage, |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + slices.SortFunc(root, func(a, b rootCommand) int { |
| 55 | + return cmp.Compare(a.name, b.name) |
| 56 | + }) |
| 57 | + return root |
| 58 | +} |
| 59 | + |
| 60 | +// formatCommandList renders the "The commands are:" block of 'src help': |
| 61 | +// one tab-indented line per command with the name padded to a common width, |
| 62 | +// the description, and any aliases in parentheses. |
| 63 | +func formatCommandList(cmds []rootCommand) string { |
| 64 | + width := 0 |
| 65 | + for _, cmd := range cmds { |
| 66 | + width = max(width, len(cmd.name)) |
| 67 | + } |
| 68 | + |
| 69 | + var b strings.Builder |
| 70 | + for _, cmd := range cmds { |
| 71 | + fmt.Fprintf(&b, "\t%-*s %s", width, cmd.name, cmd.description) |
| 72 | + if len(cmd.aliases) > 0 { |
| 73 | + fmt.Fprintf(&b, " (alias: %s)", strings.Join(cmd.aliases, ", ")) |
| 74 | + } |
| 75 | + b.WriteString("\n") |
| 76 | + } |
| 77 | + return b.String() |
| 78 | +} |
| 79 | + |
| 80 | +// usageText renders the top-level 'src help' output. |
| 81 | +func usageText() string { |
| 82 | + return usageHeader + formatCommandList(rootCommands()) + usageFooter |
| 83 | +} |
| 84 | + |
| 85 | +const usageHeader = `src is a tool that provides access to Sourcegraph instances. |
| 86 | +For more information, see https://github.com/sourcegraph/src-cli |
| 87 | +
|
| 88 | +Usage: |
| 89 | +
|
| 90 | + src [options] command [command options] |
| 91 | +
|
| 92 | +Environment variables |
| 93 | + SRC_ACCESS_TOKEN Sourcegraph access token |
| 94 | + SRC_ENDPOINT endpoint to use, if unset will default to "https://sourcegraph.com" |
| 95 | + SRC_PROXY A proxy to use for proxying requests to the Sourcegraph endpoint. |
| 96 | + Supports HTTP(S), SOCKS5/5h, and UNIX Domain Socket proxies. |
| 97 | + If a UNIX Domain Socket, the path can be either an absolute path, |
| 98 | + or can start with ~/ or %USERPROFILE%\ for a path in the user's home directory. |
| 99 | + Examples: |
| 100 | + - https://localhost:3080 |
| 101 | + - https://<user>:<password>localhost:8080 |
| 102 | + - socks5h://localhost:1080 |
| 103 | + - socks5://<username>:<password>@localhost:1080 |
| 104 | + - unix://~/src-proxy.sock |
| 105 | + - unix://%USERPROFILE%\src-proxy.sock |
| 106 | + - ~/src-proxy.sock |
| 107 | + - %USERPROFILE%\src-proxy.sock |
| 108 | + - C:\some\path\src-proxy.sock |
| 109 | +
|
| 110 | +The options are: |
| 111 | +
|
| 112 | + -v print verbose output |
| 113 | +
|
| 114 | +The commands are: |
| 115 | +
|
| 116 | +` |
| 117 | + |
| 118 | +const usageFooter = ` |
| 119 | +Use "src [command] -h" for more information about a command. |
| 120 | +
|
| 121 | +` |
0 commit comments