Skip to content

Commit 47a633b

Browse files
committed
Drop cli and meta packages
This centralizes all command-related things in the command package
1 parent 29702fc commit 47a633b

File tree

5 files changed

+61
-352
lines changed

5 files changed

+61
-352
lines changed

cli/main.go

-53
This file was deleted.

cli/help.go command/main.go

+59-23
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,87 @@
1-
package cli
1+
package command
22

33
import (
44
"bytes"
55
"fmt"
6+
"os"
67
"sort"
78
"strings"
89

910
"github.com/mitchellh/cli"
1011
)
1112

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 {
1447
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{}{},
2456
}
2557

2658
// Determine the maximum key length, and classify based on type
2759
commonCommands := make(map[string]cli.CommandFactory)
2860
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-
}
3461

62+
commonKeyLen, otherKeyLen := 0, 0
63+
for key, f := range commands {
3564
if _, ok := commonNames[key]; ok {
65+
if len(key) > commonKeyLen {
66+
commonKeyLen = len(key)
67+
}
3668
commonCommands[key] = f
3769
} else {
70+
if len(key) > otherKeyLen {
71+
otherKeyLen = len(key)
72+
}
3873
otherCommands[key] = f
3974
}
4075
}
4176

4277
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())
4985
}
5086

5187
// listCommands just lists the commands in the map with the

main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package main // import "github.com/hashicorp/vault"
33
import (
44
"os"
55

6-
"github.com/hashicorp/vault/cli"
6+
"github.com/hashicorp/vault/command"
77
)
88

99
func main() {
10-
os.Exit(cli.Run(os.Args[1:]))
10+
os.Exit(command.Run(os.Args[1:]))
1111
}

0 commit comments

Comments
 (0)