Skip to content

Commit

Permalink
feat(sshkey): Added the ssh key cmd
Browse files Browse the repository at this point in the history
- Added the possibility the you can list, create, and delete ssh key
- Modify the instance create cmd now you can pass the name or the id of the ssh key
- Update README.md

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 6, 2020
1 parent 410abb5 commit 4f3a644
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 11 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@ Golang has no shortage of external libraries for various parts of this, but the
-~~Domain names~~
-~~Domain records~~
-~~Firewalls~~
- Kubernetes Clusters
- Kubernetes Applications
- Load balancers
- SSH keys
-~~Load balancers~~
-~~SSH keys~~
- Networks
- Snapshots
- Volumes

- Kubernetes Clusters
- Kubernetes Applications



- Templates
- Blueprints
- `curl | bash` installation mechanism
Expand Down
2 changes: 1 addition & 1 deletion cmd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
instanceCreateCmd.Flags().StringVarP(&snapshot, "snapshot", "n", "", "the instance's snapshot")
instanceCreateCmd.Flags().StringVarP(&publicip, "publicip", "p", "", "the instance's public ip")
instanceCreateCmd.Flags().StringVarP(&initialuser, "initialuser", "u", "", "the instance's initial user")
instanceCreateCmd.Flags().StringVarP(&sshkey, "sshkey", "k", "", "the instance's ssh key")
instanceCreateCmd.Flags().StringVarP(&sshkey, "sshkey", "k", "", "the instance's ssh key you can use the Name or the ID")
instanceCreateCmd.Flags().StringVarP(&tags, "tags", "g", "", "the instance's tags")

/*
Expand Down
7 changes: 6 additions & 1 deletion cmd/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ Example: civo instance create --hostname=foo.example.com`,
}

if sshkey != "" {
config.SSHKeyID = sshkey
sshKey, err := client.FindSSHKey(sshkey)
if err != nil {
fmt.Printf("Unable to find the ssh key: %s\n", aurora.Red(err))
os.Exit(1)
}
config.SSHKeyID = sshKey.ID
}

if tags != "" {
Expand Down
5 changes: 0 additions & 5 deletions cmd/loadbalancer.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package cmd

// loadbalancer list -- list all load balancers [ls, all] *
// loadbalancer create [OPTIONS] -- create a new load balancer with options [new]
// loadbalancer update ID [OPTIONS] -- update the load balancer ID with options provided [change]
// loadbalancer remove ID -- remove the load balancer with ID [delete, destroy, rm] *

import (
"github.com/spf13/cobra"
)
Expand Down
17 changes: 17 additions & 0 deletions cmd/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@ package cmd
// sshkey list -- list all SSH keys [ls, all]
// sshkey upload NAME FILENAME -- upload the SSH public key in FILENAME to a new key called NAME [create, new]
// sshkey remove ID -- remove the SSH public key with ID [delete, destroy, rm]

import (
"github.com/spf13/cobra"
)

var sshKeyCmd = &cobra.Command{
Use: "sshkey",
Aliases: []string{"ssh", "ssh-key"},
Short: "Details of Civo Ssh Keys",
}

func init() {
rootCmd.AddCommand(sshKeyCmd)
sshKeyCmd.AddCommand(sshKeyListCmd)
sshKeyCmd.AddCommand(sshKeyCreateCmd)
sshKeyCmd.AddCommand(sshKeyRemoveCmd)
}
55 changes: 55 additions & 0 deletions cmd/ssh_key_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"fmt"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"io/ioutil"
"os"
)

var sshKeyCreateCmd = &cobra.Command{
Use: "create",
Aliases: []string{"new", "add"},
Short: "Create a new ssh key",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if err != nil {
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err))
os.Exit(1)
}

// reading the file
data, err := ioutil.ReadFile(args[1])
if err != nil {
fmt.Printf("Unable to read the ssh key file: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.NewSSHKey(args[0], string(data))
if err != nil {
fmt.Printf("Unable to create the ssh key: %s\n", aurora.Red(err))
os.Exit(1)
}

sshKey, err := client.FindSSHKey(args[0])
if err != nil {
fmt.Printf("Unable to find the ssh key: %s\n", aurora.Red(err))
os.Exit(1)
}

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

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("Created a ssh key called %s with ID %s\n", aurora.Green(sshKey.Name), aurora.Green(sshKey.ID))
}
},
}
55 changes: 55 additions & 0 deletions cmd/ssh_key_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"fmt"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"os"
)

var sshKeyListCmd = &cobra.Command{
Use: "ls",
Aliases: []string{"list", "all"},
Short: "List all ssh keys",
Long: `List all current ssh keys.
If you wish to use a custom format, the available fields are:
* ID
* Name
* Fingerprint
Example: civo ssh ls -o custom -f "ID: Name"`,
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if err != nil {
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err))
os.Exit(1)
}

sshKeys, err := client.ListSSHKeys()
if err != nil {
fmt.Printf("Unable to list ssh keys: %s\n", aurora.Red(err))
os.Exit(1)
}

ow := utility.NewOutputWriter()
for _, sshkey := range sshKeys {
ow.StartLine()

ow.AppendData("ID", sshkey.ID)
ow.AppendData("Name", sshkey.Name)
ow.AppendDataWithLabel("Fingerprint", sshkey.Fingerprint, "Finger Print")
}

switch outputFormat {
case "json":
ow.WriteMultipleObjectsJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
ow.WriteTable()
}
},
}
43 changes: 43 additions & 0 deletions cmd/ssh_key_remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"fmt"
"github.com/civo/cli/config"
"github.com/civo/cli/utility"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"os"
)

var sshKeyRemoveCmd = &cobra.Command{
Use: "remove",
Aliases: []string{"rm", "delete", "destroy"},
Short: "Remove a ssh key",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
client, err := config.CivoAPIClient()
if err != nil {
fmt.Printf("Unable to create a Civo API Client: %s\n", aurora.Red(err))
os.Exit(1)
}

sshKey, err := client.FindSSHKey(args[0])
if err != nil {
fmt.Printf("Unable to find ssh key for your search: %s\n", aurora.Red(err))
os.Exit(1)
}

_, err = client.DeleteSSHKey(sshKey.ID)

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

switch outputFormat {
case "json":
ow.WriteSingleObjectJSON()
case "custom":
ow.WriteCustomOutput(outputFields)
default:
fmt.Printf("The ssh key called %s with ID %s was delete\n", aurora.Green(sshKey.Name), aurora.Green(sshKey.ID))
}
},
}

0 comments on commit 4f3a644

Please sign in to comment.