mirror of https://gitlab.com/gitlab-org/cli.git
feat(delete): Add perpage and page flags to ssh-key delete command
This commit is contained in:
parent
b547ec3b39
commit
db1a57e157
|
@ -2,6 +2,7 @@ package delete
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/AlecAivazis/survey/v2"
|
||||
"github.com/MakeNowJust/heredoc"
|
||||
|
@ -20,7 +21,9 @@ type DeleteOpts struct {
|
|||
IO *iostreams.IOStreams
|
||||
BaseRepo func() (glrepo.Interface, error)
|
||||
|
||||
KeyID int
|
||||
KeyID int
|
||||
PerPage int
|
||||
Page int
|
||||
}
|
||||
|
||||
func NewCmdDelete(f *cmdutils.Factory, runE func(*DeleteOpts) error) *cobra.Command {
|
||||
|
@ -31,13 +34,17 @@ func NewCmdDelete(f *cmdutils.Factory, runE func(*DeleteOpts) error) *cobra.Comm
|
|||
Use: "delete <key-id>",
|
||||
Short: "Deletes a single SSH key specified by the ID.",
|
||||
Long: ``,
|
||||
Example: heredoc.Doc(`
|
||||
Example: heredoc.Doc(
|
||||
`
|
||||
# Delete SSH key with ID as argument
|
||||
$ glab ssh-key delete 7750633
|
||||
|
||||
# Interactive
|
||||
$ glab ssh-key delete
|
||||
`),
|
||||
|
||||
# Interactive, with pagination
|
||||
$ glab ssh-key delete -P 50 -p 2`,
|
||||
),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.HTTPClient = f.HttpClient
|
||||
|
@ -63,6 +70,9 @@ func NewCmdDelete(f *cmdutils.Factory, runE func(*DeleteOpts) error) *cobra.Comm
|
|||
},
|
||||
}
|
||||
|
||||
cmd.Flags().IntVarP(&opts.Page, "page", "p", 1, "Page number")
|
||||
cmd.Flags().IntVarP(&opts.PerPage, "per-page", "P", 30, "Number of items to list per page")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -92,16 +102,22 @@ func keySelectPrompt(opts *DeleteOpts) (int, error) {
|
|||
return 0, cmdutils.FlagError{Err: errors.New("<key-id> argument is required when prompts are disabled")}
|
||||
}
|
||||
|
||||
sshKeyListOptions := &gitlab.ListSSHKeysOptions{}
|
||||
sshKeyListOptions := &gitlab.ListSSHKeysOptions{
|
||||
PerPage: opts.PerPage,
|
||||
Page: opts.Page,
|
||||
}
|
||||
|
||||
httpClient, err := opts.HTTPClient()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
keys, _, err := httpClient.Users.ListSSHKeys(sshKeyListOptions)
|
||||
keys, resp, err := httpClient.Users.ListSSHKeys(sshKeyListOptions)
|
||||
if err != nil {
|
||||
return 0, cmdutils.WrapError(err, "getting list of SSH keys to prompt with")
|
||||
return 0, cmdutils.WrapError(err, "Retrieving list of SSH keys")
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
return 0, cmdutils.WrapError(errors.New("no keys found"), "Retrieving list of SSH keys")
|
||||
}
|
||||
|
||||
keyOpts := map[string]int{}
|
||||
|
@ -112,7 +128,13 @@ func keySelectPrompt(opts *DeleteOpts) (int, error) {
|
|||
}
|
||||
|
||||
keySelectQuestion := &survey.Select{
|
||||
Message: "Select Key",
|
||||
Message: fmt.Sprintf(
|
||||
"Select key to delete - Showing %d/%d keys - page %d/%d",
|
||||
len(keys),
|
||||
resp.TotalItems,
|
||||
opts.Page,
|
||||
resp.TotalPages,
|
||||
),
|
||||
Options: surveyOpts,
|
||||
}
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/MakeNowJust/heredoc"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/xanzy/go-gitlab"
|
||||
|
||||
"gitlab.com/gitlab-org/cli/commands/cmdutils"
|
||||
"gitlab.com/gitlab-org/cli/internal/glrepo"
|
||||
"gitlab.com/gitlab-org/cli/pkg/iostreams"
|
||||
|
@ -33,7 +34,8 @@ func NewCmdGet(f *cmdutils.Factory, runE func(*GetOpts) error) *cobra.Command {
|
|||
Use: "get <key-id>",
|
||||
Short: "Returns a single SSH key specified by the ID.",
|
||||
Long: ``,
|
||||
Example: heredoc.Doc(`
|
||||
Example: heredoc.Doc(
|
||||
`
|
||||
# Get ssh key with ID as argument
|
||||
$ glab ssh-key get 7750633
|
||||
|
||||
|
@ -42,14 +44,19 @@ func NewCmdGet(f *cmdutils.Factory, runE func(*GetOpts) error) *cobra.Command {
|
|||
|
||||
# Interactive, with pagination
|
||||
$ glab ssh-key get -P 50 -p 2
|
||||
`),
|
||||
`,
|
||||
),
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.HTTPClient = f.HttpClient
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
|
||||
if len(args) == 0 && !opts.IO.PromptEnabled() {
|
||||
return cmdutils.FlagError{Err: errors.New("<key-id> argument is required when running in non-ttys")}
|
||||
if len(args) == 0 {
|
||||
keyID, err := keySelectPrompt(opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
opts.KeyID = keyID
|
||||
}
|
||||
|
||||
if len(args) == 1 {
|
||||
|
@ -76,16 +83,9 @@ func getRun(opts *GetOpts) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if opts.KeyID == 0 {
|
||||
opts.KeyID, err = keySelectPrompt(httpClient, opts)
|
||||
if err != nil {
|
||||
return cmdutils.WrapError(err, "failed to prompt")
|
||||
}
|
||||
}
|
||||
|
||||
key, _, err := httpClient.Users.GetSSHKey(opts.KeyID)
|
||||
if err != nil {
|
||||
return cmdutils.WrapError(err, "failed to get ssh key")
|
||||
return cmdutils.WrapError(err, "getting SSH key")
|
||||
}
|
||||
|
||||
opts.IO.LogInfo(key.Key)
|
||||
|
@ -93,17 +93,29 @@ func getRun(opts *GetOpts) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func keySelectPrompt(client *gitlab.Client, opts *GetOpts) (int, error) {
|
||||
func keySelectPrompt(opts *GetOpts) (int, error) {
|
||||
if !opts.IO.PromptEnabled() {
|
||||
return 0, cmdutils.FlagError{Err: errors.New("<key-id> argument is required when prompts are disabled")}
|
||||
}
|
||||
|
||||
sshKeyListOptions := &gitlab.ListSSHKeysOptions{
|
||||
PerPage: opts.PerPage,
|
||||
Page: opts.Page,
|
||||
}
|
||||
|
||||
keys, response, err := client.Users.ListSSHKeys(sshKeyListOptions)
|
||||
httpClient, err := opts.HTTPClient()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
keys, response, err := httpClient.Users.ListSSHKeys(sshKeyListOptions)
|
||||
if err != nil {
|
||||
return 0, cmdutils.WrapError(err, "Retrieving list of SSH keys to prompt with")
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
return 0, cmdutils.WrapError(errors.New("no keys were found"), "Retrieving list of SSH keys")
|
||||
}
|
||||
|
||||
keyOpts := map[string]int{}
|
||||
surveyOpts := make([]string, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
|
@ -124,5 +136,9 @@ func keySelectPrompt(client *gitlab.Client, opts *GetOpts) (int, error) {
|
|||
|
||||
var result string
|
||||
err = prompt.AskOne(keySelectQuestion, &result)
|
||||
return keyOpts[result], err
|
||||
if err != nil {
|
||||
return 0, cmdutils.WrapError(err, "prompting for SSH key to delete")
|
||||
}
|
||||
|
||||
return keyOpts[result], nil
|
||||
}
|
||||
|
|
|
@ -26,6 +26,15 @@ $ glab ssh-key delete 7750633
|
|||
# Interactive
|
||||
$ glab ssh-key delete
|
||||
|
||||
# Interactive, with pagination
|
||||
$ glab ssh-key delete -P 50 -p 2
|
||||
```
|
||||
|
||||
## Options
|
||||
|
||||
```plaintext
|
||||
-p, --page int Page number (default 1)
|
||||
-P, --per-page int Number of items to list per page (default 30)
|
||||
```
|
||||
|
||||
## Options inherited from parent commands
|
||||
|
|
Loading…
Reference in New Issue