Skip to content

Commit

Permalink
add "ssh-key get --project-id $ID"
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Jul 23, 2022
1 parent a5e1018 commit 4d7f91a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
8 changes: 6 additions & 2 deletions docs/metal_ssh-key_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ metal ssh-key get [-i <SSH-key_UUID>] [flags]
# Returns the details of SSH key 5cb96463-88fd-4d68-94ba-2c9505ff265e:
metal ssh-key get --id 5cb96463-88fd-4d68-94ba-2c9505ff265e
# Retrieve all project SSH keys
metal ssh-key get --project-id [project_UUID]
```

### Options

```
-h, --help help for get
-i, --id string The UUID of an SSH key.
-h, --help help for get
-i, --id string The UUID of an SSH key.
-p, --project-id string List SSH Keys for the project identified by Project ID
```

### Options inherited from parent commands
Expand Down
24 changes: 22 additions & 2 deletions internal/ssh/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
package ssh

import (
"fmt"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -38,12 +41,28 @@ func (c *Client) Retrieve() *cobra.Command {
metal ssh-key get
# Returns the details of SSH key 5cb96463-88fd-4d68-94ba-2c9505ff265e:
metal ssh-key get --id 5cb96463-88fd-4d68-94ba-2c9505ff265e`,
metal ssh-key get --id 5cb96463-88fd-4d68-94ba-2c9505ff265e
# Retrieve all project SSH keys
metal ssh-key get --project-id [project_UUID]`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if sshKeyID == "" {
sshKeys, _, err := c.Service.List()
projectID, _ := cmd.LocalFlags().GetString("project-id")
fmt.Println(cmd.LocalFlags())
listFn := func() ([]packngo.SSHKey, *packngo.Response, error) {
fmt.Println("foooo")
return c.Service.List()
}

if projectID != "" && cmd.Flag("project-id").Changed {
listFn = func() ([]packngo.SSHKey, *packngo.Response, error) {
fmt.Println("baaar")
return c.Service.ProjectList(projectID)
}
}
sshKeys, _, err := listFn()

if err != nil {
return errors.Wrap(err, "Could not list SSH Keys")
}
Expand Down Expand Up @@ -73,5 +92,6 @@ func (c *Client) Retrieve() *cobra.Command {
}

retrieveSSHKeysCmd.Flags().StringVarP(&sshKeyID, "id", "i", "", "The UUID of an SSH key.")
retrieveSSHKeysCmd.Flags().StringP("project-id", "p", "", "List SSH Keys for the project identified by Project ID")
return retrieveSSHKeysCmd
}

0 comments on commit 4d7f91a

Please sign in to comment.