Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add "ssh-key get --project-id $ID" #156

Merged
merged 1 commit into from
Sep 26, 2022
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
11 changes: 8 additions & 3 deletions docs/metal_ssh-key_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Retrieves a list of SSH keys or a specified SSH key.
Retrieves a list of SSH keys associated with the current user's account or the details of single SSH key.

```
metal ssh-key get [-i <SSH-key_UUID>] [flags]
metal ssh-key get [-i <SSH-key_UUID>] [-P] [-p <project_id>] [flags]
```

### Examples
Expand All @@ -18,13 +18,18 @@ 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-ssh-keys --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 (ignored without -P)
-P, --project-ssh-keys List SSH Keys for projects
```

### Options inherited from parent commands
Expand Down
33 changes: 29 additions & 4 deletions internal/ssh/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,52 @@
package ssh

import (
"fmt"

"github.com/packethost/packngo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func (c *Client) Retrieve() *cobra.Command {
var sshKeyID string
var (
sshKeyID string
projKeys bool
)

// retrieveSshKeysCmd represents the retrieveSshKeys command
retrieveSSHKeysCmd := &cobra.Command{
Use: `get [-i <SSH-key_UUID>]`,
Use: `get [-i <SSH-key_UUID>] [-P] [-p <project_id>]`,
Aliases: []string{"list"},
Short: "Retrieves a list of SSH keys or a specified SSH key.",
Long: "Retrieves a list of SSH keys associated with the current user's account or the details of single SSH key.",
Example: ` # Retrieves the SSH keys of the current user:
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-ssh-keys --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")
listFn := func() ([]packngo.SSHKey, *packngo.Response, error) {
return c.Service.List()
}

if projKeys {
if projectID == "" {
return fmt.Errorf("Project (--project-id) is required with --project-keys")
}

listFn = func() ([]packngo.SSHKey, *packngo.Response, error) {
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 +96,7 @@ func (c *Client) Retrieve() *cobra.Command {
}

retrieveSSHKeysCmd.Flags().StringVarP(&sshKeyID, "id", "i", "", "The UUID of an SSH key.")
retrieveSSHKeysCmd.Flags().BoolVarP(&projKeys, "project-ssh-keys", "P", false, "List SSH Keys for projects")
retrieveSSHKeysCmd.Flags().StringP("project-id", "p", "", "List SSH Keys for the project identified by Project ID (ignored without -P)")
return retrieveSSHKeysCmd
}
7 changes: 4 additions & 3 deletions internal/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ type Client struct {

func (c *Client) NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: `ssh-key`,
Short: "SSH key operations. For more information on SSH keys, visit https://metal.equinix.com/developers/docs/accounts/ssh-keys/ in the Equinix Metal documentation.",
Long: "SSH key operations: create, get, update, and delete.",
Use: `ssh-key`,
Aliases: []string{`ssh-keys`},
Short: "SSH key operations. For more information on SSH keys, visit https://metal.equinix.com/developers/docs/accounts/ssh-keys/ in the Equinix Metal documentation.",
Long: "SSH key operations: create, get, update, and delete.",

PersistentPreRun: func(cmd *cobra.Command, args []string) {
if root := cmd.Root(); root != nil {
Expand Down