Skip to content

Commit

Permalink
Drop cli and meta packages
Browse files Browse the repository at this point in the history
This centralizes all command-related things in the command package
  • Loading branch information
sethvargo committed Oct 24, 2017
1 parent 29702fc commit 47a633b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 352 deletions.
53 changes: 0 additions & 53 deletions cli/main.go

This file was deleted.

82 changes: 59 additions & 23 deletions cli/help.go → command/main.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,87 @@
package cli
package command

import (
"bytes"
"fmt"
"os"
"sort"
"strings"

"github.com/mitchellh/cli"
)

// HelpFunc is a cli.HelpFunc that can is used to output the help for Vault.
func HelpFunc(commands map[string]cli.CommandFactory) string {
func Run(args []string) int {
// Handle -v shorthand
for _, arg := range args {
if arg == "--" {
break
}

if arg == "-v" || arg == "-version" || arg == "--version" {
args = []string{"version"}
break
}
}

cli := &cli.CLI{
Name: "vault",
Args: args,
Commands: Commands,
HelpFunc: helpFunc,

Autocomplete: true,
AutocompleteNoDefaultFlags: true,
}

exitCode, err := cli.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
return 1
}

return exitCode
}

// helpFunc is a cli.HelpFunc that can is used to output the help for Vault.
func helpFunc(commands map[string]cli.CommandFactory) string {
commonNames := map[string]struct{}{
"delete": struct{}{},
"path-help": struct{}{},
"read": struct{}{},
"renew": struct{}{},
"revoke": struct{}{},
"write": struct{}{},
"server": struct{}{},
"status": struct{}{},
"unwrap": struct{}{},
"delete": struct{}{},
"read": struct{}{},
"renew": struct{}{},
"revoke": struct{}{},
"server": struct{}{},
"status": struct{}{},
"unwrap": struct{}{},
"write": struct{}{},
}

// Determine the maximum key length, and classify based on type
commonCommands := make(map[string]cli.CommandFactory)
otherCommands := make(map[string]cli.CommandFactory)
maxKeyLen := 0
for key, f := range commands {
if len(key) > maxKeyLen {
maxKeyLen = len(key)
}

commonKeyLen, otherKeyLen := 0, 0
for key, f := range commands {
if _, ok := commonNames[key]; ok {
if len(key) > commonKeyLen {
commonKeyLen = len(key)
}
commonCommands[key] = f
} else {
if len(key) > otherKeyLen {
otherKeyLen = len(key)
}
otherCommands[key] = f
}
}

var buf bytes.Buffer
buf.WriteString("usage: vault [-version] [-help] <command> [args]\n\n")
buf.WriteString("Common commands:\n")
buf.WriteString(listCommands(commonCommands, maxKeyLen))
buf.WriteString("\nAll other commands:\n")
buf.WriteString(listCommands(otherCommands, maxKeyLen))
return buf.String()
buf.WriteString("Usage: vault <command> [args]\n\n")
buf.WriteString("Common commands:\n\n")
buf.WriteString(listCommands(commonCommands, commonKeyLen))
buf.WriteString("\n")
buf.WriteString("Other commands:\n\n")
buf.WriteString(listCommands(otherCommands, otherKeyLen))
return strings.TrimSpace(buf.String())
}

// listCommands just lists the commands in the map with the
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main // import "github.com/hashicorp/vault"
import (
"os"

"github.com/hashicorp/vault/cli"
"github.com/hashicorp/vault/command"
)

func main() {
os.Exit(cli.Run(os.Args[1:]))
os.Exit(command.Run(os.Args[1:]))
}
Loading

0 comments on commit 47a633b

Please sign in to comment.