diff --git a/cmd/entities/ssh-keys/add.go b/cmd/entities/ssh-keys/add.go index 3348109..696a39c 100644 --- a/cmd/entities/ssh-keys/add.go +++ b/cmd/entities/ssh-keys/add.go @@ -1,15 +1,20 @@ package sshkeys import ( - "log" - serverscom "github.com/serverscom/serverscom-go-client/pkg" "github.com/serverscom/srvctl/cmd/base" "github.com/spf13/cobra" ) +type AddedFlags struct { + InputPath string + Name string + PublicKey string + Labels []string +} + func newAddCmd(cmdContext *base.CmdContext) *cobra.Command { - var path string + flags := &AddedFlags{} cmd := &cobra.Command{ Use: "add --input ", @@ -25,7 +30,19 @@ func newAddCmd(cmdContext *base.CmdContext) *cobra.Command { base.SetupProxy(cmd, manager) input := &serverscom.SSHKeyCreateInput{} - if err := base.ReadInputJSON(path, cmd.InOrStdin(), input); err != nil { + + if flags.InputPath != "" { + if err := base.ReadInputJSON(flags.InputPath, cmd.InOrStdin(), input); err != nil { + return err + } + } else { + required := []string{"name", "public-key"} + if err := base.ValidateFlags(cmd, required); err != nil { + return err + } + } + + if err := flags.FillInput(cmd, input); err != nil { return err } @@ -43,10 +60,29 @@ func newAddCmd(cmdContext *base.CmdContext) *cobra.Command { }, } - cmd.Flags().StringVarP(&path, "input", "i", "", "path to input file or '-' to read from stdin") - if err := cmd.MarkFlagRequired("input"); err != nil { - log.Fatal(err) - } + cmd.Flags().StringVarP(&flags.InputPath, "input", "i", "", "path to input file or '-' to read from stdin") + cmd.Flags().StringVarP(&flags.Name, "name", "n", "", "A name of a SSH key") + cmd.Flags().StringVarP(&flags.PublicKey, "public-key", "", "", "A public-key of a SSH key") + cmd.Flags().StringArrayVarP(&flags.Labels, "label", "l", []string{}, "string in key=value format") return cmd } + +func (f *AddedFlags) FillInput(cmd *cobra.Command, input *serverscom.SSHKeyCreateInput) error { + if cmd.Flags().Changed("name") { + input.Name = f.Name + } + if cmd.Flags().Changed("public-key") { + input.PublicKey = f.PublicKey + } + if cmd.Flags().Changed("label") { + labelsMap, err := base.ParseLabels(f.Labels) + if err != nil { + return err + } + + input.Labels = labelsMap + } + + return nil +} diff --git a/cmd/entities/ssh-keys/ssh_keys_test.go b/cmd/entities/ssh-keys/ssh_keys_test.go index 5324f24..2afd470 100644 --- a/cmd/entities/ssh-keys/ssh_keys_test.go +++ b/cmd/entities/ssh-keys/ssh_keys_test.go @@ -36,7 +36,7 @@ func TestAddSSHKeysCmd(t *testing.T) { expectError bool }{ { - name: "create ssh key", + name: "create ssh key with input", output: "json", expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.json")), args: []string{"--input", filepath.Join(fixtureBasePath, "create.json")}, @@ -50,6 +50,25 @@ func TestAddSSHKeysCmd(t *testing.T) { Return(&testSSHKey, nil) }, }, + { + name: "create ssh key", + output: "json", + expectedOutput: testutils.ReadFixture(filepath.Join(fixtureBasePath, "get.json")), + args: []string{ + "--name", "test-key", + "--public-key", "-----TEST public-key-----", + "--label", "foo=bar", + }, + configureMock: func(mock *mocks.MockSSHKeysService) { + mock.EXPECT(). + Create(gomock.Any(), serverscom.SSHKeyCreateInput{ + Name: "test-key", + PublicKey: "-----TEST public-key-----", + Labels: map[string]string{"foo": "bar"}, + }). + Return(&testSSHKey, nil) + }, + }, { name: "create ssh key with error", expectError: true,