Skip to content

Commit

Permalink
[CHANGE] named identification arguments to positionals
Browse files Browse the repository at this point in the history
  • Loading branch information
eijei521 committed Sep 1, 2022
1 parent af21375 commit 39f3f3b
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 54 deletions.
11 changes: 5 additions & 6 deletions internal/regions/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ import (
func (c *Client) Get() *cobra.Command {
var regionID string
regionGetCmd := &cobra.Command{
Use: `get [-i <region_slug>]`,
Use: `get {ID | SLUG}`,
Args: cobra.ExactArgs(1),
Short: "Retrieves region details.",
Long: "Retrieves the details of the specified region.",
Example: ` # Gets the details of the specified region:
cherryctl region get -i eu_nord_1`,
cherryctl region get eu_nord_1`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
regionID = args[0]
o, _, err := c.Service.Get(regionID, c.Servicer.GetOptions())
if err != nil {
return errors.Wrap(err, "Could not get region")
return errors.Wrap(err, "Could not get a region")
}

header := []string{"ID", "Slug", "Name", "BGP hosts", "BGP asn"}
Expand All @@ -32,8 +34,5 @@ func (c *Client) Get() *cobra.Command {
},
}

regionGetCmd.Flags().StringVarP(&regionID, "region-id", "i", "", "The Slug or ID of region.")
_ = regionGetCmd.MarkFlagRequired("region-id")

return regionGetCmd
}
11 changes: 6 additions & 5 deletions internal/sshkeys/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import (
func (c *Client) Get() *cobra.Command {
var sshKeyID int
sshGetCmd := &cobra.Command{
Use: `get [-i <ssh_key_id>]`,
Use: `get ID`,
Args: cobra.ExactArgs(1),
Short: "Retrieves ssh-key details.",
Long: "Retrieves the details of the specified ssh-key.",
Example: ` # Gets the details of the specified ssh-key:
cherryctl ssh-key get -i 12345`,
cherryctl ssh-key get 12345`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if sshID, err := strconv.Atoi(args[0]); err == nil {
sshKeyID = sshID
}
getOptions := c.Servicer.GetOptions()
getOptions.Fields = []string{"ssh_key", "email"}
o, _, err := c.Service.Get(sshKeyID, getOptions)
Expand All @@ -32,8 +36,5 @@ func (c *Client) Get() *cobra.Command {
},
}

sshGetCmd.Flags().IntVarP(&sshKeyID, "ssh-key-id", "i", 0, "The ID of ssh-key.")
_ = sshGetCmd.MarkFlagRequired("ssh-key-id")

return sshGetCmd
}
8 changes: 6 additions & 2 deletions internal/sshkeys/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ func (c *Client) Update() *cobra.Command {
publicKey string
)
sshKeyUpdateCmd := &cobra.Command{
Use: `update -i <ssh_key_id> [--label] [--key <public_key>]`,
Use: `update -i <ssh_key_id> [--label <text>] [--key <public_key>]`,
Args: cobra.ExactArgs(1),
Short: "Updates an SSH key.",
Long: "Updates an SSH key with either a new public key, a new label, or both.",
Example: ` # Update team to change currency to EUR:
cherryctl ssh-key update -i 12345 --key [email protected]`,
cherryctl ssh-key update 12345 --key [email protected]`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if sshID, err := strconv.Atoi(args[0]); err == nil {
sshKeyID = sshID
}
request := &cherrygo.UpdateSSHKey{}

if label != "" {
Expand Down
12 changes: 7 additions & 5 deletions internal/storages/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storages

import (
"fmt"
"strconv"

"github.com/cherryservers/cherryctl/internal/utils"
"github.com/cherryservers/cherrygo/v3"
Expand All @@ -17,14 +18,18 @@ func (c *Client) Attach() *cobra.Command {
projectID int
)
storageAttachCmd := &cobra.Command{
Use: `attach -i <storage_id> {--server-id | --server-hostname} [-p <project_id>]`,
Use: `attach ID {--server-id <id> | --server-hostname <hostname>} [-p <project_id>]`,
Args: cobra.ExactArgs(1),
Short: "Attach storage volume to a specified server.",
Long: "Attach storage volume to a specified server.",
Example: ` # Attach storage to specified server:
cherryctl storage attach -i 12345 -s 12345`,
cherryctl storage attach 12345 --server-id 12345`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if storID, err := strconv.Atoi(args[0]); err == nil {
storageID = storID
}

if serverHostname == "" && serverID == 0 {
return fmt.Errorf("either server-id or server-hostname should be set")
Expand Down Expand Up @@ -52,14 +57,11 @@ func (c *Client) Attach() *cobra.Command {
},
}

storageAttachCmd.Flags().IntVarP(&storageID, "storage-id", "i", 0, "The storage's ID.")
storageAttachCmd.Flags().IntVarP(&serverID, "server-id", "s", 0, "The server's ID.")
storageAttachCmd.Flags().StringVarP(&serverHostname, "server-hostname", "", "", "The Hostname of a server.")
storageAttachCmd.Flags().IntVarP(&projectID, "project-id", "p", 0, "The project's ID.")

storageAttachCmd.MarkFlagsMutuallyExclusive("server-id", "server-hostname")

storageAttachCmd.MarkFlagRequired("storage-id")

return storageAttachCmd
}
2 changes: 1 addition & 1 deletion internal/storages/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (c *Client) Create() *cobra.Command {
region string
)
storageCreateCmd := &cobra.Command{
Use: `create [-p <project_id>] --size <gigabytes> --region <region_slug> [--description]`,
Use: `create [-p <project_id>] --size <gigabytes> --region <region_slug> [--description <text>]`,
Short: "Create storage.",
Long: "Create storage in speficied project.",
Example: ` # Create storage volume with 500GB space in EU-Nord-1 location:
Expand Down
14 changes: 8 additions & 6 deletions internal/storages/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storages

import (
"fmt"
"strconv"

"github.com/manifoldco/promptui"
"github.com/pkg/errors"
Expand All @@ -12,19 +13,23 @@ func (c *Client) Delete() *cobra.Command {
var storageID int
var force bool
deleteStorageCmd := &cobra.Command{
Use: `delete -i <storage_id>`,
Use: `delete ID`,
Args: cobra.ExactArgs(1),
Short: "Delete a storage.",
Long: "Deletes the specified storage with a confirmation prompt. To skip the confirmation use --force.",
Example: ` # Deletes the specified storage:
cherryctl storage delete -i 12345
cherryctl storage delete 12345
>
✔ Are you sure you want to delete storage 12345: y
# Deletes a storage, skipping confirmation:
cherryctl storage delete -f -i 12345`,
cherryctl storage delete 12345 -f`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if storID, err := strconv.Atoi(args[0]); err == nil {
storageID = storID
}
if !force {
prompt := promptui.Prompt{
Label: fmt.Sprintf("Are you sure you want to delete storage %d? ", storageID),
Expand All @@ -47,10 +52,7 @@ func (c *Client) Delete() *cobra.Command {
},
}

deleteStorageCmd.Flags().IntVarP(&storageID, "storage-id", "i", 0, "The ID of a storage volume.")
deleteStorageCmd.Flags().BoolVarP(&force, "force", "f", false, "Skips confirmation for the storage deletion.")

_ = deleteStorageCmd.MarkFlagRequired("storage-id")

return deleteStorageCmd
}
13 changes: 7 additions & 6 deletions internal/storages/detach.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storages

import (
"fmt"
"strconv"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand All @@ -12,14 +13,18 @@ func (c *Client) Detach() *cobra.Command {
storageID int
)
storageDetachCmd := &cobra.Command{
Use: `detach -i <storage_id>`,
Use: `detach ID`,
Args: cobra.ExactArgs(1),
Short: "Detach storage volume from a server.",
Long: "Detach storage volume from a server.",
Example: ` # Detach storage:
cherryctl storage detach -i 12345`,
cherryctl storage detach 12345`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if storID, err := strconv.Atoi(args[0]); err == nil {
storageID = storID
}

_, err := c.Service.Detach(storageID)
if err != nil {
Expand All @@ -31,9 +36,5 @@ func (c *Client) Detach() *cobra.Command {
},
}

storageDetachCmd.Flags().IntVarP(&storageID, "storage-id", "i", 0, "The storage's ID.")

storageDetachCmd.MarkFlagRequired("storage-id")

return storageDetachCmd
}
11 changes: 6 additions & 5 deletions internal/storages/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import (
func (c *Client) Get() *cobra.Command {
var storageID int
storagesGetCmd := &cobra.Command{
Use: `get [-i <storage_id>]`,
Use: `get ID`,
Args: cobra.ExactArgs(1),
Short: "Retrieves storage details.",
Long: "Retrieves the details of the specified storage.",
Example: ` # Gets the details of the specified storage:
cherryctl storage get -i 12345`,
cherryctl storage get 12345`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if storID, err := strconv.Atoi(args[0]); err == nil {
storageID = storID
}
getOptions := c.Servicer.GetOptions()
getOptions.Fields = []string{"storage", "region", "id", "hostname"}
o, _, err := c.Service.Get(storageID, getOptions)
Expand All @@ -34,8 +38,5 @@ func (c *Client) Get() *cobra.Command {
},
}

storagesGetCmd.Flags().IntVarP(&storageID, "storage-id", "i", 0, "The ID of a storage volume.")
_ = storagesGetCmd.MarkFlagRequired("storage-id")

return storagesGetCmd
}
11 changes: 6 additions & 5 deletions internal/storages/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ func (c *Client) Update() *cobra.Command {
description string
)
storageUpdateCmd := &cobra.Command{
Use: `update -i <storage_id> [--size <gigabytes>] [--description]`,
Use: `update ID [--size <gigabytes>] [--description <text>]`,
Args: cobra.ExactArgs(1),
Short: "Update storage volume.",
Long: "Update storage size or description.",
Example: ` # Update storage size to 1000 gigabyte:
cherryctl storage update -i 12345 --size 1000`,
cherryctl storage update 12345 --size 1000`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
if storID, err := strconv.Atoi(args[0]); err == nil {
storageID = storID
}
request := &cherrygo.UpdateStorage{
StorageID: storageID,
Size: size,
Expand All @@ -43,11 +47,8 @@ func (c *Client) Update() *cobra.Command {
},
}

storageUpdateCmd.Flags().IntVarP(&storageID, "storage-id", "i", 0, "The storage's ID.")
storageUpdateCmd.Flags().IntVarP(&size, "size", "", 0, "Storage volume size in gigabytes. Value must be greater than current volume size.")
storageUpdateCmd.Flags().StringVarP(&description, "description", "", "", "Storage description.")

storageUpdateCmd.MarkFlagRequired("storage-id")

return storageUpdateCmd
}
16 changes: 12 additions & 4 deletions internal/teams/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package teams

import (
"fmt"
"strconv"

"github.com/manifoldco/promptui"
"github.com/pkg/errors"
Expand All @@ -12,11 +13,20 @@ func (c *Client) Delete() *cobra.Command {
var teamID int
var force bool
deleteTeamCmd := &cobra.Command{
Use: `delete -t <team_id>`,
Use: `delete ID -t <team_id>`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
tID, err := strconv.Atoi(args[0])
if err == nil {
teamID = tID
}
}
return nil
},
Short: "Delete a team.",
Long: "Deletes the specified team with a confirmation prompt. To skip the confirmation use --force.",
Example: ` # Deletes the specified team:
cherryctl team delete -t 12345
cherryctl team delete 12345
>
✔ Are you sure you want to delete team 12345: y
Expand Down Expand Up @@ -49,7 +59,5 @@ func (c *Client) Delete() *cobra.Command {
deleteTeamCmd.Flags().IntVarP(&teamID, "team-id", "t", 0, "The ID of a team.")
deleteTeamCmd.Flags().BoolVarP(&force, "force", "f", false, "Skips confirmation for the tean deletion.")

_ = deleteTeamCmd.MarkFlagRequired("team-id")

return deleteTeamCmd
}
15 changes: 11 additions & 4 deletions internal/teams/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@ func (c *Client) Update() *cobra.Command {
teamType string
)
teamUpdateCmd := &cobra.Command{
Use: `update [-t <team_id>] [--name <team_name>] [--currency <currency_code>] [--type <team_type>]`,
Use: `update ID [-t <team_id>] [--name <team_name>] [--currency <currency_code>] [--type <team_type>]`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
tID, err := strconv.Atoi(args[0])
if err == nil {
teamID = tID
}
}
return nil
},
Short: "Update a team.",
Long: "Update a team.",
Example: ` # Update a team to change currency to EUR:
cherryctl team update -t 12345 --currency EUR`,
cherryctl team update 12345 --currency EUR`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
Expand Down Expand Up @@ -58,7 +67,5 @@ func (c *Client) Update() *cobra.Command {
teamUpdateCmd.Flags().StringVarP(&teamType, "type", "", "", "Team type, available options: personal, business.")
teamUpdateCmd.Flags().StringVarP(&name, "name", "", "", "Team name.")

teamUpdateCmd.MarkFlagRequired("team-id")

return teamUpdateCmd
}
11 changes: 6 additions & 5 deletions internal/users/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@ import (
func (c *Client) Get() *cobra.Command {
var userID int
userGetCmd := &cobra.Command{
Use: `get [-i <user_id>]`,
Use: `get ID`,
Args: cobra.ExactArgs(1),
Short: "Retrieves information about the current user or a specified user.",
Long: "Returns either information about the current user or information about a specified user. Specified user information is only available if that user shares a project with the current user.",
Example: ` # Gets the current user's information:
cherryctl user get
# Returns information on user with ID 123:
cherryctl user get -i 123`,
cherryctl user get 123`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
var user cherrygo.User
var err error

if uID, err := strconv.Atoi(args[0]); err == nil {
userID = uID
}
if userID == 0 {
user, _, err = c.Service.CurrentUser(c.Servicer.GetOptions())
if err != nil {
Expand All @@ -46,7 +49,5 @@ func (c *Client) Get() *cobra.Command {
},
}

userGetCmd.Flags().IntVarP(&userID, "user-id", "i", 0, "The ID of the user.")

return userGetCmd
}

0 comments on commit 39f3f3b

Please sign in to comment.