From 39f3f3b284bb19d375c142fafac70c1ce0b718c2 Mon Sep 17 00:00:00 2001 From: Andrius Jucius Date: Thu, 1 Sep 2022 16:42:41 +0300 Subject: [PATCH] [CHANGE] named identification arguments to positionals --- internal/regions/get.go | 11 +++++------ internal/sshkeys/get.go | 11 ++++++----- internal/sshkeys/update.go | 8 ++++++-- internal/storages/attach.go | 12 +++++++----- internal/storages/create.go | 2 +- internal/storages/delete.go | 14 ++++++++------ internal/storages/detach.go | 13 +++++++------ internal/storages/get.go | 11 ++++++----- internal/storages/update.go | 11 ++++++----- internal/teams/delete.go | 16 ++++++++++++---- internal/teams/update.go | 15 +++++++++++---- internal/users/get.go | 11 ++++++----- 12 files changed, 81 insertions(+), 54 deletions(-) diff --git a/internal/regions/get.go b/internal/regions/get.go index 1618cb4..d7bd965 100644 --- a/internal/regions/get.go +++ b/internal/regions/get.go @@ -11,17 +11,19 @@ import ( func (c *Client) Get() *cobra.Command { var regionID string regionGetCmd := &cobra.Command{ - Use: `get [-i ]`, + 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"} @@ -32,8 +34,5 @@ func (c *Client) Get() *cobra.Command { }, } - regionGetCmd.Flags().StringVarP(®ionID, "region-id", "i", "", "The Slug or ID of region.") - _ = regionGetCmd.MarkFlagRequired("region-id") - return regionGetCmd } diff --git a/internal/sshkeys/get.go b/internal/sshkeys/get.go index 5fb6889..68822eb 100644 --- a/internal/sshkeys/get.go +++ b/internal/sshkeys/get.go @@ -10,13 +10,17 @@ import ( func (c *Client) Get() *cobra.Command { var sshKeyID int sshGetCmd := &cobra.Command{ - Use: `get [-i ]`, + 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) @@ -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 } diff --git a/internal/sshkeys/update.go b/internal/sshkeys/update.go index 8c4a3c3..982ec16 100644 --- a/internal/sshkeys/update.go +++ b/internal/sshkeys/update.go @@ -15,14 +15,18 @@ func (c *Client) Update() *cobra.Command { publicKey string ) sshKeyUpdateCmd := &cobra.Command{ - Use: `update -i [--label] [--key ]`, + Use: `update -i [--label ] [--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 AAAAB3N...user@domain.com`, + cherryctl ssh-key update 12345 --key AAAAB3N...user@domain.com`, 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 != "" { diff --git a/internal/storages/attach.go b/internal/storages/attach.go index bb80737..5a29c75 100644 --- a/internal/storages/attach.go +++ b/internal/storages/attach.go @@ -2,6 +2,7 @@ package storages import ( "fmt" + "strconv" "github.com/cherryservers/cherryctl/internal/utils" "github.com/cherryservers/cherrygo/v3" @@ -17,14 +18,18 @@ func (c *Client) Attach() *cobra.Command { projectID int ) storageAttachCmd := &cobra.Command{ - Use: `attach -i {--server-id | --server-hostname} [-p ]`, + Use: `attach ID {--server-id | --server-hostname } [-p ]`, + 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") @@ -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 } diff --git a/internal/storages/create.go b/internal/storages/create.go index 92a113c..223f215 100644 --- a/internal/storages/create.go +++ b/internal/storages/create.go @@ -17,7 +17,7 @@ func (c *Client) Create() *cobra.Command { region string ) storageCreateCmd := &cobra.Command{ - Use: `create [-p ] --size --region [--description]`, + Use: `create [-p ] --size --region [--description ]`, Short: "Create storage.", Long: "Create storage in speficied project.", Example: ` # Create storage volume with 500GB space in EU-Nord-1 location: diff --git a/internal/storages/delete.go b/internal/storages/delete.go index 797e7c2..7a008d1 100644 --- a/internal/storages/delete.go +++ b/internal/storages/delete.go @@ -2,6 +2,7 @@ package storages import ( "fmt" + "strconv" "github.com/manifoldco/promptui" "github.com/pkg/errors" @@ -12,19 +13,23 @@ func (c *Client) Delete() *cobra.Command { var storageID int var force bool deleteStorageCmd := &cobra.Command{ - Use: `delete -i `, + 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), @@ -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 } diff --git a/internal/storages/detach.go b/internal/storages/detach.go index 52c6dac..8404a0d 100644 --- a/internal/storages/detach.go +++ b/internal/storages/detach.go @@ -2,6 +2,7 @@ package storages import ( "fmt" + "strconv" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -12,14 +13,18 @@ func (c *Client) Detach() *cobra.Command { storageID int ) storageDetachCmd := &cobra.Command{ - Use: `detach -i `, + 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 { @@ -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 } diff --git a/internal/storages/get.go b/internal/storages/get.go index 5ecb6ff..dad2cd1 100644 --- a/internal/storages/get.go +++ b/internal/storages/get.go @@ -11,14 +11,18 @@ import ( func (c *Client) Get() *cobra.Command { var storageID int storagesGetCmd := &cobra.Command{ - Use: `get [-i ]`, + 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) @@ -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 } diff --git a/internal/storages/update.go b/internal/storages/update.go index ad1ab13..6ad0532 100644 --- a/internal/storages/update.go +++ b/internal/storages/update.go @@ -16,14 +16,18 @@ func (c *Client) Update() *cobra.Command { description string ) storageUpdateCmd := &cobra.Command{ - Use: `update -i [--size ] [--description]`, + Use: `update ID [--size ] [--description ]`, + 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, @@ -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 } diff --git a/internal/teams/delete.go b/internal/teams/delete.go index 4da9253..acfb878 100644 --- a/internal/teams/delete.go +++ b/internal/teams/delete.go @@ -2,6 +2,7 @@ package teams import ( "fmt" + "strconv" "github.com/manifoldco/promptui" "github.com/pkg/errors" @@ -12,11 +13,20 @@ func (c *Client) Delete() *cobra.Command { var teamID int var force bool deleteTeamCmd := &cobra.Command{ - Use: `delete -t `, + Use: `delete ID -t `, + 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 @@ -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 } diff --git a/internal/teams/update.go b/internal/teams/update.go index 029ff4a..8436fa7 100644 --- a/internal/teams/update.go +++ b/internal/teams/update.go @@ -17,11 +17,20 @@ func (c *Client) Update() *cobra.Command { teamType string ) teamUpdateCmd := &cobra.Command{ - Use: `update [-t ] [--name ] [--currency ] [--type ]`, + Use: `update ID [-t ] [--name ] [--currency ] [--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 @@ -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 } diff --git a/internal/users/get.go b/internal/users/get.go index ca91990..722eac6 100644 --- a/internal/users/get.go +++ b/internal/users/get.go @@ -12,19 +12,22 @@ import ( func (c *Client) Get() *cobra.Command { var userID int userGetCmd := &cobra.Command{ - Use: `get [-i ]`, + 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 { @@ -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 }