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

View File

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

View File

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