Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions cmd/entities/ssh-keys/add.go
Original file line number Diff line number Diff line change
@@ -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 <path>",
Expand All @@ -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
}

Expand All @@ -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
}
21 changes: 20 additions & 1 deletion cmd/entities/ssh-keys/ssh_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")},
Expand All @@ -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,
Expand Down
Loading