mirror of https://github.com/status-im/consul.git
Convert keygen command to use base.Command
This commit is contained in:
parent
8775b031d3
commit
d67151908d
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
|
15
commands.go
15
commands.go
|
@ -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,
|
||||||
|
|
Loading…
Reference in New Issue