From 3a18373db7e2aeb457acf4999852b433e38187d0 Mon Sep 17 00:00:00 2001 From: Kyle Havlovitz Date: Wed, 8 Feb 2017 17:19:17 -0500 Subject: [PATCH] Convert keygen command to use base.Command --- command/keygen.go | 15 +++++++++++---- command/keygen_test.go | 8 +++++++- commands.go | 15 +++++++++------ 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/command/keygen.go b/command/keygen.go index f0f9d70c2..1fcfab168 100644 --- a/command/keygen.go +++ b/command/keygen.go @@ -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) } diff --git a/command/keygen_test.go b/command/keygen_test.go index f94392538..481a568e1 100644 --- a/command/keygen_test.go +++ b/command/keygen_test.go @@ -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) diff --git a/commands.go b/commands.go index 36a96c6c1..c3a21e990 100644 --- a/commands.go +++ b/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,