diff --git a/internal/ips/assign.go b/internal/ips/assign.go index 66068ab..36161b8 100644 --- a/internal/ips/assign.go +++ b/internal/ips/assign.go @@ -1,9 +1,10 @@ package ips import ( + "fmt" + "github.com/cherryservers/cherryctl/internal/utils" "github.com/cherryservers/cherrygo/v3" - "github.com/google/uuid" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -17,18 +18,26 @@ func (c *Client) Assign() *cobra.Command { targetHostname string ) ipAssignCmd := &cobra.Command{ - Use: `assign -i {--target-hostname | --target-id | --target-ip-id} [-p ]`, + Use: `assign UUID {--target-hostname | --target-id | --target-ip-id } [-p ]`, + Args: cobra.ExactArgs(1), Aliases: []string{"attach"}, Short: "Assign an IP address to a specified server or other IP address.", Long: "Assign an IP address to a specified server or another IP address. IP address assignment to another IP is possible only if routed IP type is floating and target IP is subnet or primary-ip type.", Example: ` # Assign an IP address to a server: - cherryctl ip assign -i 30c15082-a06e-4c43-bfc3-252616b46eba --server-id 12345`, + cherryctl ip assign 30c15082-a06e-4c43-bfc3-252616b46eba --server-id 12345`, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true request := &cherrygo.AssignIPAddress{} - if targetIPID != "" && IsValidUUID(targetIPID) { + if utils.IsValidUUID(args[0]) { + ipID = args[0] + } else { + fmt.Println("IP address with ID %s was not found.", args[0]) + return nil + } + + if targetIPID != "" && utils.IsValidUUID(targetIPID) { request.IpID = targetIPID } else if targetHostname != "" { srvID, err := utils.ServerHostnameToID(targetHostname, projectID, c.ServerService) @@ -57,20 +66,12 @@ func (c *Client) Assign() *cobra.Command { }, } - ipAssignCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.") ipAssignCmd.Flags().IntVarP(&projectID, "project-id", "p", 0, "The project's ID.") - ipAssignCmd.Flags().StringVarP(&targetHostname, "target-hostname", "", "", "The hostname of the server to assign IP to.") ipAssignCmd.Flags().IntVarP(&targetID, "target-id", "", 0, "The ID of the server to assign IP to.") ipAssignCmd.Flags().StringVarP(&targetIPID, "target-ip-id", "", "", "Subnet or primary-ip type IP ID to route IP to.") ipAssignCmd.MarkFlagsMutuallyExclusive("target-hostname", "target-id", "target-ip-id") - ipAssignCmd.MarkFlagRequired("ip-address-id") return ipAssignCmd } - -func IsValidUUID(u string) bool { - _, err := uuid.Parse(u) - return err == nil -} diff --git a/internal/ips/create.go b/internal/ips/create.go index b0f711d..98f92fd 100644 --- a/internal/ips/create.go +++ b/internal/ips/create.go @@ -24,7 +24,7 @@ func (c *Client) Create() *cobra.Command { tags []string ) ipCreateCmd := &cobra.Command{ - Use: `create [-p ] --region [--target-hostname | --target-id | --target-ip-id] [--ptr-record] [--a-record] [--tags]`, + Use: `create [-p ] --region [--target-hostname | --target-id | --target-ip-id ] [--ptr-record ] [--a-record ] [--tags ]`, Short: "Create floating IP address.", Long: "Create floating IP address in speficied project.", Example: ` # Create a floating IP address in EU-Nord-1 location: diff --git a/internal/ips/delete.go b/internal/ips/delete.go index 8bd2fad..b25802e 100644 --- a/internal/ips/delete.go +++ b/internal/ips/delete.go @@ -3,6 +3,7 @@ package ips import ( "fmt" + "github.com/cherryservers/cherryctl/internal/utils" "github.com/manifoldco/promptui" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -12,19 +13,27 @@ func (c *Client) Delete() *cobra.Command { var ipID string var force bool deleteIpCmd := &cobra.Command{ - Use: `delete -i `, + Use: `delete UUID`, + Args: cobra.ExactArgs(1), Short: "Delete an IP address.", Long: "Deletes the specified IP address with a confirmation prompt. To skip the confirmation use --force.", Example: ` # Deletes the specified IP: - cherryctl ip delete -i 30c15082-a06e-4c43-bfc3-252616b46eba + cherryctl ip delete 30c15082-a06e-4c43-bfc3-252616b46eba > ✔ Are you sure you want to delete IP address 30c15082-a06e-4c43-bfc3-252616b46eba: y # Deletes a server, skipping confirmation: - cherryctl ip delete -f -i 30c15082-a06e-4c43-bfc3-252616b46eba`, + cherryctl ip delete 30c15082-a06e-4c43-bfc3-252616b46eba -f`, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + if utils.IsValidUUID(args[0]) { + ipID = args[0] + } else { + fmt.Println("IP address with ID %s was not found.", args[0]) + return nil + } + if !force { prompt := promptui.Prompt{ Label: fmt.Sprintf("Are you sure you want to delete IP address %s: ", ipID), @@ -46,10 +55,7 @@ func (c *Client) Delete() *cobra.Command { }, } - deleteIpCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.") deleteIpCmd.Flags().BoolVarP(&force, "force", "f", false, "Skips confirmation for the server deletion.") - _ = deleteIpCmd.MarkFlagRequired("ip-address-id") - return deleteIpCmd } diff --git a/internal/ips/get.go b/internal/ips/get.go index 0df4fea..ae7278d 100644 --- a/internal/ips/get.go +++ b/internal/ips/get.go @@ -3,6 +3,7 @@ package ips import ( "fmt" + "github.com/cherryservers/cherryctl/internal/utils" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -10,14 +11,22 @@ import ( func (c *Client) Get() *cobra.Command { var ipID string ipGetCmd := &cobra.Command{ - Use: `get [-i ]`, + Use: `get UUID`, + Args: cobra.ExactArgs(1), Short: "Get an IP address details.", Long: "Get the details of the specified IP address.", Example: ` # Gets the details of the specified IP address: - cherryctl ip get -i 12345`, + cherryctl ip get 30c15082-a06e-4c43-bfc3-252616b46eba`, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + if utils.IsValidUUID(args[0]) { + ipID = args[0] + } else { + fmt.Println("IP address with ID %s was not found.", args[0]) + return nil + } + getOptions := c.Servicer.GetOptions() getOptions.Fields = []string{"ip", "region", "hostname"} i, _, err := c.Service.Get(ipID, getOptions) @@ -33,8 +42,5 @@ func (c *Client) Get() *cobra.Command { }, } - ipGetCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.") - _ = ipGetCmd.MarkFlagRequired("ip-address-id") - return ipGetCmd } diff --git a/internal/ips/unassign.go b/internal/ips/unassign.go index 9c642c4..5849ac4 100644 --- a/internal/ips/unassign.go +++ b/internal/ips/unassign.go @@ -3,6 +3,7 @@ package ips import ( "fmt" + "github.com/cherryservers/cherryctl/internal/utils" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -12,15 +13,22 @@ func (c *Client) Unassign() *cobra.Command { ipID string ) ipDetachCmd := &cobra.Command{ - Use: `unassign -i `, + Use: `unassign UUID`, + Args: cobra.ExactArgs(1), Aliases: []string{"detach", "unasign"}, Short: "Unassign an IP address.", Long: "Unassign an IP address.", Example: ` # Unassign an IP address: - cherryctl ip unassign -i 12345`, + cherryctl ip unassign 30c15082-a06e-4c43-bfc3-252616b46eba`, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + if utils.IsValidUUID(args[0]) { + ipID = args[0] + } else { + fmt.Println("IP address with ID %s was not found.", args[0]) + return nil + } _, err := c.Service.Unassign(ipID) if err != nil { @@ -32,9 +40,5 @@ func (c *Client) Unassign() *cobra.Command { }, } - ipDetachCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of an IP address.") - - ipDetachCmd.MarkFlagRequired("ip-address-id") - return ipDetachCmd } diff --git a/internal/ips/update.go b/internal/ips/update.go index d4dee97..b1f7d65 100644 --- a/internal/ips/update.go +++ b/internal/ips/update.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/cherryservers/cherryctl/internal/utils" "github.com/cherryservers/cherrygo/v3" "github.com/pkg/errors" "github.com/spf13/cobra" @@ -17,14 +18,21 @@ func (c *Client) Update() *cobra.Command { tags []string ) ipUpdateCmd := &cobra.Command{ - Use: `update -i [--ptr-record] [--a-record] [--tags]`, + Use: `update UUID [--ptr-record ] [--a-record ] [--tags ]`, + Args: cobra.ExactArgs(1), Short: "Update IP address.", Long: "Update tags, ptr record, a record or target server of a IP address.", Example: ` # Updates a record and tags: - cherryctl ip update -i 30c15082-a06e-4c43-bfc3-252616b46eba --a-record stage --tags="env=stage"`, + cherryctl ip update 30c15082-a06e-4c43-bfc3-252616b46eba --a-record stage --tags="env=stage"`, RunE: func(cmd *cobra.Command, args []string) error { cmd.SilenceUsage = true + if utils.IsValidUUID(args[0]) { + ipID = args[0] + } else { + fmt.Println("IP address with ID %s was not found.", args[0]) + return nil + } tagsArr := make(map[string]string) for _, kv := range tags { @@ -60,12 +68,9 @@ func (c *Client) Update() *cobra.Command { }, } - ipUpdateCmd.Flags().StringVarP(&ipID, "ip-address-id", "i", "", "The ID of a IP address.") ipUpdateCmd.Flags().StringVarP(&ptrRecord, "ptr-record", "", "", "Slug of the region from where IP address will requested.") ipUpdateCmd.Flags().StringVarP(&aRecord, "a-record", "", "", "Slug of the region from where IP address will requested.") ipUpdateCmd.Flags().StringSliceVarP(&tags, "tags", "", []string{}, `Tag or list of tags for the server: --tags="key=value,env=prod".`) - ipUpdateCmd.MarkFlagRequired("ip-address-id") - return ipUpdateCmd } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index c29edfc..e3df30d 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -5,6 +5,7 @@ import ( "strings" "github.com/cherryservers/cherrygo/v3" + "github.com/google/uuid" "github.com/pkg/errors" ) @@ -39,3 +40,8 @@ func serverList(projectID int, ServerService cherrygo.ServersService) ([]cherryg return serverList, err } + +func IsValidUUID(u string) bool { + _, err := uuid.Parse(u) + return err == nil +}