|
1 |
| -package cli |
| 1 | +package command |
2 | 2 |
|
3 | 3 | import (
|
4 | 4 | "bytes"
|
5 | 5 | "fmt"
|
| 6 | + "os" |
6 | 7 | "sort"
|
7 | 8 | "strings"
|
8 | 9 |
|
9 | 10 | "github.com/mitchellh/cli"
|
10 | 11 | )
|
11 | 12 |
|
12 |
| -// HelpFunc is a cli.HelpFunc that can is used to output the help for Vault. |
13 |
| -func HelpFunc(commands map[string]cli.CommandFactory) string { |
| 13 | +func Run(args []string) int { |
| 14 | + // Handle -v shorthand |
| 15 | + for _, arg := range args { |
| 16 | + if arg == "--" { |
| 17 | + break |
| 18 | + } |
| 19 | + |
| 20 | + if arg == "-v" || arg == "-version" || arg == "--version" { |
| 21 | + args = []string{"version"} |
| 22 | + break |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + cli := &cli.CLI{ |
| 27 | + Name: "vault", |
| 28 | + Args: args, |
| 29 | + Commands: Commands, |
| 30 | + HelpFunc: helpFunc, |
| 31 | + |
| 32 | + Autocomplete: true, |
| 33 | + AutocompleteNoDefaultFlags: true, |
| 34 | + } |
| 35 | + |
| 36 | + exitCode, err := cli.Run() |
| 37 | + if err != nil { |
| 38 | + fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error()) |
| 39 | + return 1 |
| 40 | + } |
| 41 | + |
| 42 | + return exitCode |
| 43 | +} |
| 44 | + |
| 45 | +// helpFunc is a cli.HelpFunc that can is used to output the help for Vault. |
| 46 | +func helpFunc(commands map[string]cli.CommandFactory) string { |
14 | 47 | commonNames := map[string]struct{}{
|
15 |
| - "delete": struct{}{}, |
16 |
| - "path-help": struct{}{}, |
17 |
| - "read": struct{}{}, |
18 |
| - "renew": struct{}{}, |
19 |
| - "revoke": struct{}{}, |
20 |
| - "write": struct{}{}, |
21 |
| - "server": struct{}{}, |
22 |
| - "status": struct{}{}, |
23 |
| - "unwrap": struct{}{}, |
| 48 | + "delete": struct{}{}, |
| 49 | + "read": struct{}{}, |
| 50 | + "renew": struct{}{}, |
| 51 | + "revoke": struct{}{}, |
| 52 | + "server": struct{}{}, |
| 53 | + "status": struct{}{}, |
| 54 | + "unwrap": struct{}{}, |
| 55 | + "write": struct{}{}, |
24 | 56 | }
|
25 | 57 |
|
26 | 58 | // Determine the maximum key length, and classify based on type
|
27 | 59 | commonCommands := make(map[string]cli.CommandFactory)
|
28 | 60 | otherCommands := make(map[string]cli.CommandFactory)
|
29 |
| - maxKeyLen := 0 |
30 |
| - for key, f := range commands { |
31 |
| - if len(key) > maxKeyLen { |
32 |
| - maxKeyLen = len(key) |
33 |
| - } |
34 | 61 |
|
| 62 | + commonKeyLen, otherKeyLen := 0, 0 |
| 63 | + for key, f := range commands { |
35 | 64 | if _, ok := commonNames[key]; ok {
|
| 65 | + if len(key) > commonKeyLen { |
| 66 | + commonKeyLen = len(key) |
| 67 | + } |
36 | 68 | commonCommands[key] = f
|
37 | 69 | } else {
|
| 70 | + if len(key) > otherKeyLen { |
| 71 | + otherKeyLen = len(key) |
| 72 | + } |
38 | 73 | otherCommands[key] = f
|
39 | 74 | }
|
40 | 75 | }
|
41 | 76 |
|
42 | 77 | var buf bytes.Buffer
|
43 |
| - buf.WriteString("usage: vault [-version] [-help] <command> [args]\n\n") |
44 |
| - buf.WriteString("Common commands:\n") |
45 |
| - buf.WriteString(listCommands(commonCommands, maxKeyLen)) |
46 |
| - buf.WriteString("\nAll other commands:\n") |
47 |
| - buf.WriteString(listCommands(otherCommands, maxKeyLen)) |
48 |
| - return buf.String() |
| 78 | + buf.WriteString("Usage: vault <command> [args]\n\n") |
| 79 | + buf.WriteString("Common commands:\n\n") |
| 80 | + buf.WriteString(listCommands(commonCommands, commonKeyLen)) |
| 81 | + buf.WriteString("\n") |
| 82 | + buf.WriteString("Other commands:\n\n") |
| 83 | + buf.WriteString(listCommands(otherCommands, otherKeyLen)) |
| 84 | + return strings.TrimSpace(buf.String()) |
49 | 85 | }
|
50 | 86 |
|
51 | 87 | // listCommands just lists the commands in the map with the
|
|
0 commit comments