diff --git a/commands/generate.go b/commands/generate.go index bfb2c832..57c1eb01 100644 --- a/commands/generate.go +++ b/commands/generate.go @@ -1,6 +1,8 @@ package commands import ( + "errors" + "github.com/pivotal-cf/credhub-cli/actions" "github.com/pivotal-cf/credhub-cli/client" "github.com/pivotal-cf/credhub-cli/config" @@ -37,9 +39,14 @@ type GenerateCommand struct { } func (cmd GenerateCommand) Execute([]string) error { + validTypes := []string{"value", "password", "ssh", "rsa", "certificate"} + if cmd.SecretType == "" { cmd.SecretType = "password" } + if !contains(validTypes, cmd.SecretType) { + return errors.New("The request does not include a valid type. Valid values include 'value', 'password', 'certificate', 'ssh' and 'rsa'.") + } cfg := config.ReadConfig() repository := repositories.NewSecretRepository(client.NewHttpClient(cfg)) diff --git a/commands/generate_test.go b/commands/generate_test.go index daf044eb..35df3720 100644 --- a/commands/generate_test.go +++ b/commands/generate_test.go @@ -374,6 +374,18 @@ var _ = Describe("Generate", func() { } }) + It("displays an error when invalid type is specified", func() { + server.AppendHandlers( + RespondWith(http.StatusBadRequest, `{"error": "you fail."}`), + ) + + session := runCommand("generate", "-t", "potato", "-n", "my-value") + + Eventually(session).Should(Exit(1)) + + Expect(session.Err).To(Say("The request does not include a valid type. Valid values include 'value', 'password', 'certificate', 'ssh' and 'rsa'.")) + }) + It("displays the server provided error when an error is received", func() { server.AppendHandlers( RespondWith(http.StatusBadRequest, `{"error": "you fail."}`),