Convert keygen command to use base.Command
This commit is contained in:
parent
9e156286c7
commit
3a18373db7
|
@ -6,16 +6,21 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/hashicorp/consul/command/base"
|
||||
)
|
||||
|
||||
// KeygenCommand is a Command implementation that generates an encryption
|
||||
// key for use in `consul agent`.
|
||||
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)
|
||||
n, err := rand.Reader.Read(key)
|
||||
if err != nil {
|
||||
|
@ -42,6 +47,8 @@ Usage: consul keygen
|
|||
Generates a new encryption key that can be used to configure the
|
||||
agent to encrypt traffic. The output of this command is already
|
||||
in the proper format that the agent expects.
|
||||
`
|
||||
|
||||
` + c.Command.Help()
|
||||
|
||||
return strings.TrimSpace(helpText)
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package command
|
|||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"github.com/hashicorp/consul/command/base"
|
||||
"github.com/mitchellh/cli"
|
||||
"testing"
|
||||
)
|
||||
|
@ -12,7 +13,12 @@ func TestKeygenCommand_implements(t *testing.T) {
|
|||
|
||||
func TestKeygenCommand(t *testing.T) {
|
||||
ui := new(cli.MockUi)
|
||||
c := &KeygenCommand{Ui: ui}
|
||||
c := &KeygenCommand{
|
||||
Command: base.Command{
|
||||
Ui: ui,
|
||||
Flags: base.FlagSetNone,
|
||||
},
|
||||
}
|
||||
code := c.Run(nil)
|
||||
if code != 0 {
|
||||
t.Fatalf("bad: %d", code)
|
||||
|
|
15
commands.go
15
commands.go
|
@ -85,6 +85,15 @@ func init() {
|
|||
}, nil
|
||||
},
|
||||
|
||||
"keygen": func() (cli.Command, error) {
|
||||
return &command.KeygenCommand{
|
||||
Command: base.Command{
|
||||
Ui: ui,
|
||||
Flags: base.FlagSetNone,
|
||||
},
|
||||
}, nil
|
||||
},
|
||||
|
||||
"kv": func() (cli.Command, error) {
|
||||
return &command.KVCommand{
|
||||
Ui: ui,
|
||||
|
@ -121,12 +130,6 @@ func init() {
|
|||
}, nil
|
||||
},
|
||||
|
||||
"keygen": func() (cli.Command, error) {
|
||||
return &command.KeygenCommand{
|
||||
Ui: ui,
|
||||
}, nil
|
||||
},
|
||||
|
||||
"keyring": func() (cli.Command, error) {
|
||||
return &command.KeyringCommand{
|
||||
Ui: ui,
|
||||
|
|
Loading…
Reference in New Issue