Skip to content

Commit

Permalink
Merge pull request #367 from uzaxirr/db-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrojnm authored Feb 26, 2024
2 parents 4c96844 + 89efd24 commit 6497376
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/sshkey/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ func init() {
SSHKeyCmd.AddCommand(sshKeyListCmd)
SSHKeyCmd.AddCommand(sshKeyCreateCmd)
SSHKeyCmd.AddCommand(sshKeyRemoveCmd)
SSHKeyCmd.AddCommand(sshKeyFindCmd)
SSHKeyCmd.AddCommand(sshKeyUpdateCmd)

sshKeyCreateCmd.Flags().StringVarP(&keyCreate, "key", "k", "", "The path of the key")
sshKeyCreateCmd.MarkFlagRequired("key")
Expand Down
41 changes: 41 additions & 0 deletions cmd/sshkey/ssh_key_find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sshkey

import (
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
"os"
)

var sshKeyFindCmd = &cobra.Command{
Use: "find",
Aliases: []string{"get"},
Example: `civo ssh find`,
Short: "Finds an SSH key by either part of the ID or part of the name",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

sshKey, err := client.FindSSHKey(args[0])
if err != nil {
utility.Error("SSHKey %s", err)
os.Exit(1)
}

ow := utility.NewOutputWriterWithMap(map[string]string{"id": sshKey.ID, "name": sshKey.Name})

switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
ow.WriteTable()
}
},
}
63 changes: 63 additions & 0 deletions cmd/sshkey/ssh_key_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package sshkey

import (
"errors"
"github.com/civo/civogo"
"github.com/civo/cli/common"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/spf13/cobra"
"os"
)

var sshKeyUpdateCmd = &cobra.Command{
Use: "update",
Aliases: []string{"modify", "rename"},
Example: `civo ssh rename my-mac-keys my-dell-keys
civo ip rename "1234-68gyd-8knw9" new-keys`,
Short: "Updates a SSH key record",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
utility.EnsureCurrentRegion()

client, err := config.CivoAPIClient()
if common.RegionSet != "" {
client.Region = common.RegionSet
}
if err != nil {
utility.Error("Creating the connection to Civo's API failed with %s", err)
os.Exit(1)
}

sshKey, err := client.FindSSHKey(args[0])
if err != nil {
if errors.Is(err, civogo.ZeroMatchesError) {
utility.Error("sorry there is no %s SSH key in your account", utility.Red(args[0]))
os.Exit(1)
} else if errors.Is(err, civogo.MultipleMatchesError) {
utility.Error("sorry we found more than one SSH keys with that value in your account")
os.Exit(1)
} else {
utility.Error("%s", err)
}
}

newName := args[1]
renamedKey, err := client.UpdateSSHKey(newName, sshKey.ID)
if err != nil {
utility.Error("%s", err)
os.Exit(1)
}

ow := utility.NewOutputWriterWithMap(map[string]string{"id": renamedKey.ID, "name": renamedKey.Name})

switch common.OutputFormat {
case "json":
ow.WriteSingleObjectJSON(common.PrettySet)
case "custom":
ow.WriteCustomOutput(common.OutputFields)
default:
ow.WriteTable()
}
},
}

0 comments on commit 6497376

Please sign in to comment.