Convert keygen command to use base.Command

This commit is contained in:
Kyle Havlovitz 2017-02-08 17:19:17 -05:00
parent 8775b031d3
commit d67151908d
No known key found for this signature in database
GPG Key ID: 8A5E6B173056AD6C
3 changed files with 27 additions and 11 deletions

View File

@ -6,16 +6,21 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/mitchellh/cli" "github.com/hashicorp/consul/command/base"
) )
// KeygenCommand is a Command implementation that generates an encryption // KeygenCommand is a Command implementation that generates an encryption
// key for use in `consul agent`. // key for use in `consul agent`.
type KeygenCommand struct { type KeygenCommand struct {
Ui cli.Ui base.Command
} }
func (c *KeygenCommand) Run(_ []string) int { func (c *KeygenCommand) Run(args []string) int {
c.Command.NewFlagSet(c)
if err := c.Command.Parse(args); err != nil {
return 1
}
key := make([]byte, 16) key := make([]byte, 16)
n, err := rand.Reader.Read(key) n, err := rand.Reader.Read(key)
if err != nil { if err != nil {
@ -42,6 +47,8 @@ Usage: consul keygen
Generates a new encryption key that can be used to configure the Generates a new encryption key that can be used to configure the
agent to encrypt traffic. The output of this command is already agent to encrypt traffic. The output of this command is already
in the proper format that the agent expects. in the proper format that the agent expects.
`
` + c.Command.Help()
return strings.TrimSpace(helpText) return strings.TrimSpace(helpText)
} }

View File

@ -2,6 +2,7 @@ package command
import ( import (
"encoding/base64" "encoding/base64"
"github.com/hashicorp/consul/command/base"
"github.com/mitchellh/cli" "github.com/mitchellh/cli"
"testing" "testing"
) )
@ -12,7 +13,12 @@ func TestKeygenCommand_implements(t *testing.T) {
func TestKeygenCommand(t *testing.T) { func TestKeygenCommand(t *testing.T) {
ui := new(cli.MockUi) ui := new(cli.MockUi)
c := &KeygenCommand{Ui: ui} c := &KeygenCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetNone,
},
}
code := c.Run(nil) code := c.Run(nil)
if code != 0 { if code != 0 {
t.Fatalf("bad: %d", code) t.Fatalf("bad: %d", code)

View File

@ -85,6 +85,15 @@ func init() {
}, nil }, nil
}, },
"keygen": func() (cli.Command, error) {
return &command.KeygenCommand{
Command: base.Command{
Ui: ui,
Flags: base.FlagSetNone,
},
}, nil
},
"kv": func() (cli.Command, error) { "kv": func() (cli.Command, error) {
return &command.KVCommand{ return &command.KVCommand{
Ui: ui, Ui: ui,
@ -121,12 +130,6 @@ func init() {
}, nil }, nil
}, },
"keygen": func() (cli.Command, error) {
return &command.KeygenCommand{
Ui: ui,
}, nil
},
"keyring": func() (cli.Command, error) { "keyring": func() (cli.Command, error) {
return &command.KeyringCommand{ return &command.KeyringCommand{
Ui: ui, Ui: ui,