commands: move keygen command to separate pkg

This commit is contained in:
Frank Schroeder 2017-10-11 14:51:25 +02:00 committed by Frank Schröder
parent b1c5ee39a5
commit aca803ca8d
3 changed files with 38 additions and 35 deletions

View File

@ -12,6 +12,7 @@ import (
execmd "github.com/hashicorp/consul/command/exec"
"github.com/hashicorp/consul/command/info"
"github.com/hashicorp/consul/command/join"
"github.com/hashicorp/consul/command/keygen"
"github.com/hashicorp/consul/command/validate"
"github.com/hashicorp/consul/version"
"github.com/mitchellh/cli"
@ -100,12 +101,7 @@ func init() {
},
"keygen": func() (cli.Command, error) {
return &KeygenCommand{
BaseCommand: BaseCommand{
Flags: FlagSetNone,
UI: ui,
},
}, nil
return keygen.New(ui), nil
},
"keyring": func() (cli.Command, error) {

View File

@ -1,20 +1,32 @@
package command
package keygen
import (
"crypto/rand"
"encoding/base64"
"flag"
"fmt"
"github.com/hashicorp/consul/command/flags"
"github.com/mitchellh/cli"
)
// KeygenCommand is a Command implementation that generates an encryption
// key for use in `consul agent`.
type KeygenCommand struct {
BaseCommand
func New(ui cli.Ui) *cmd {
c := &cmd{UI: ui}
c.initFlags()
return c
}
func (c *KeygenCommand) Run(args []string) int {
c.InitFlagSet()
if err := c.FlagSet.Parse(args); err != nil {
type cmd struct {
UI cli.Ui
flags *flag.FlagSet
}
func (c *cmd) initFlags() {
c.flags = flag.NewFlagSet("", flag.ContinueOnError)
}
func (c *cmd) Run(args []string) int {
if err := c.flags.Parse(args); err != nil {
return 1
}
@ -33,18 +45,16 @@ func (c *KeygenCommand) Run(args []string) int {
return 0
}
func (c *KeygenCommand) Help() string {
c.InitFlagSet()
return c.HelpCommand(`
Usage: consul keygen
func (c *cmd) Synopsis() string {
return "Generates a new encryption key"
}
func (c *cmd) Help() string {
s := `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.
in the proper format that the agent expects.`
`)
}
func (c *KeygenCommand) Synopsis() string {
return "Generates a new encryption key"
return flags.Usage(s, c.flags, nil, nil)
}

View File

@ -1,27 +1,24 @@
package command
package keygen
import (
"encoding/base64"
"strings"
"testing"
"github.com/mitchellh/cli"
)
func TestKeygenCommand_implements(t *testing.T) {
t.Parallel()
var _ cli.Command = &KeygenCommand{}
func TestKeygenCommand_noTabs(t *testing.T) {
if strings.ContainsRune(New(nil).Help(), '\t') {
t.Fatal("usage has tabs")
}
}
func TestKeygenCommand(t *testing.T) {
t.Parallel()
ui := cli.NewMockUi()
c := &KeygenCommand{
BaseCommand: BaseCommand{
UI: ui,
Flags: FlagSetNone,
},
}
code := c.Run(nil)
cmd := New(ui)
code := cmd.Run(nil)
if code != 0 {
t.Fatalf("bad: %d", code)
}