Skip to content

Commit 7663342

Browse files
authored
Standardize resource ID parameters to use --id flag (#7)
Changed instance and vpc commands to consistently use --id flag instead of positional arguments for resource identification.
1 parent 4f04bf2 commit 7663342

File tree

6 files changed

+79
-40
lines changed

6 files changed

+79
-40
lines changed

cmd/command_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func TestInstanceCommand(t *testing.T) {
3333
assert.Contains(t, commandNames, "create")
3434
assert.Contains(t, commandNames, "list")
3535
assert.Contains(t, commandNames, "get --id <id>")
36-
assert.Contains(t, commandNames, "update <id>")
37-
assert.Contains(t, commandNames, "delete <id>")
38-
assert.Contains(t, commandNames, "resize <id>")
36+
assert.Contains(t, commandNames, "update --id <id>")
37+
assert.Contains(t, commandNames, "delete --id <id>")
38+
assert.Contains(t, commandNames, "resize --id <id>")
3939
}
4040

4141
func TestVPCCommand(t *testing.T) {
@@ -54,8 +54,8 @@ func TestVPCCommand(t *testing.T) {
5454
assert.Contains(t, commandNames, "create")
5555
assert.Contains(t, commandNames, "list")
5656
assert.Contains(t, commandNames, "get --id <id>")
57-
assert.Contains(t, commandNames, "update <id>")
58-
assert.Contains(t, commandNames, "delete <id>")
57+
assert.Contains(t, commandNames, "update --id <id>")
58+
assert.Contains(t, commandNames, "delete --id <id>")
5959
}
6060

6161
func TestInstanceCreateCommand_Validation(t *testing.T) {

cmd/instance_delete.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,32 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
var forceDelete bool
14+
var (
15+
deleteInstanceID string
16+
forceDelete bool
17+
)
1518

1619
var instanceDeleteCmd = &cobra.Command{
17-
Use: "delete <id>",
20+
Use: "delete --id <id>",
1821
Short: "Delete a CloudAMQP instance",
1922
Long: `Delete a CloudAMQP instance permanently.
2023
2124
WARNING: This action cannot be undone. All data will be lost.`,
22-
Example: ` cloudamqp instance delete 1234
23-
cloudamqp instance delete 1234 --force`,
24-
Args: cobra.ExactArgs(1),
25-
ValidArgsFunction: completeInstances,
25+
Example: ` cloudamqp instance delete --id 1234
26+
cloudamqp instance delete --id 1234 --force`,
27+
Args: cobra.NoArgs,
2628
RunE: func(cmd *cobra.Command, args []string) error {
2729
var err error
2830
apiKey, err = getAPIKey()
2931
if err != nil {
3032
return fmt.Errorf("failed to get API key: %w", err)
3133
}
3234

33-
instanceID, err := strconv.Atoi(args[0])
35+
if deleteInstanceID == "" {
36+
return fmt.Errorf("--id is required")
37+
}
38+
39+
instanceID, err := strconv.Atoi(deleteInstanceID)
3440
if err != nil {
3541
return fmt.Errorf("invalid instance ID: %v", err)
3642
}
@@ -64,5 +70,8 @@ WARNING: This action cannot be undone. All data will be lost.`,
6470
}
6571

6672
func init() {
73+
instanceDeleteCmd.Flags().StringVar(&deleteInstanceID, "id", "", "Instance ID (required)")
6774
instanceDeleteCmd.Flags().BoolVar(&forceDelete, "force", false, "Skip confirmation prompt")
75+
instanceDeleteCmd.MarkFlagRequired("id")
76+
instanceDeleteCmd.RegisterFlagCompletionFunc("id", completeInstances)
6877
}

cmd/instance_resize.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
)
1010

1111
var (
12-
diskSize int
13-
allowDowntime bool
12+
resizeInstanceID string
13+
diskSize int
14+
allowDowntime bool
1415
)
1516

1617
var instanceResizeCmd = &cobra.Command{
17-
Use: "resize <id>",
18+
Use: "resize --id <id>",
1819
Short: "Resize instance disk",
1920
Long: `Resize the disk size of an instance. Default behavior is to expand the disk without any downtime.
2021
Currently limited to instances in Amazon Web Services (AWS) and Google Compute Engine (GCE).
@@ -24,18 +25,21 @@ Note: This action is asynchronous. The request will return almost immediately. T
2425
Note: Due to restrictions from cloud providers, it's only possible to resize the disk every 8 hours unless --allow-downtime is set.
2526
2627
Available disk sizes: 0, 25, 50, 100, 250, 500, 1000, 2000 GB`,
27-
Example: ` cloudamqp instance resize 1234 --disk-size=100
28-
cloudamqp instance resize 1234 --disk-size=250 --allow-downtime`,
29-
Args: cobra.ExactArgs(1),
30-
ValidArgsFunction: completeInstances,
28+
Example: ` cloudamqp instance resize --id 1234 --disk-size=100
29+
cloudamqp instance resize --id 1234 --disk-size=250 --allow-downtime`,
30+
Args: cobra.NoArgs,
3131
RunE: func(cmd *cobra.Command, args []string) error {
3232
var err error
3333
apiKey, err = getAPIKey()
3434
if err != nil {
3535
return fmt.Errorf("failed to get API key: %w", err)
3636
}
3737

38-
instanceID, err := strconv.Atoi(args[0])
38+
if resizeInstanceID == "" {
39+
return fmt.Errorf("--id is required")
40+
}
41+
42+
instanceID, err := strconv.Atoi(resizeInstanceID)
3943
if err != nil {
4044
return fmt.Errorf("invalid instance ID: %v", err)
4145
}
@@ -75,7 +79,10 @@ Available disk sizes: 0, 25, 50, 100, 250, 500, 1000, 2000 GB`,
7579
}
7680

7781
func init() {
82+
instanceResizeCmd.Flags().StringVar(&resizeInstanceID, "id", "", "Instance ID (required)")
7883
instanceResizeCmd.Flags().IntVar(&diskSize, "disk-size", 0, "Disk size to add in gigabytes (0, 25, 50, 100, 250, 500, 1000, 2000)")
7984
instanceResizeCmd.Flags().BoolVar(&allowDowntime, "allow-downtime", false, "Allow cluster downtime if needed when resizing disk")
85+
instanceResizeCmd.MarkFlagRequired("id")
8086
instanceResizeCmd.MarkFlagRequired("disk-size")
87+
instanceResizeCmd.RegisterFlagCompletionFunc("id", completeInstances)
8188
}

cmd/instance_update.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,37 @@ import (
99
)
1010

1111
var (
12+
updateInstanceID string
1213
updateInstanceName string
1314
updateInstancePlan string
1415
updateInstanceTags []string
1516
)
1617

1718
var instanceUpdateCmd = &cobra.Command{
18-
Use: "update <id>",
19+
Use: "update --id <id>",
1920
Short: "Update a CloudAMQP instance",
2021
Long: `Update an existing CloudAMQP instance with new configuration.
2122
2223
You can update the following fields:
2324
--name: Instance name
2425
--plan: Subscription plan
2526
--tags: Instance tags (replaces existing tags)`,
26-
Example: ` cloudamqp instance update 1234 --name=new-name
27-
cloudamqp instance update 1234 --plan=rabbit-1
28-
cloudamqp instance update 1234 --tags=production --tags=updated`,
29-
Args: cobra.ExactArgs(1),
30-
ValidArgsFunction: completeInstances,
27+
Example: ` cloudamqp instance update --id 1234 --name=new-name
28+
cloudamqp instance update --id 1234 --plan=rabbit-1
29+
cloudamqp instance update --id 1234 --tags=production --tags=updated`,
30+
Args: cobra.NoArgs,
3131
RunE: func(cmd *cobra.Command, args []string) error {
3232
var err error
3333
apiKey, err = getAPIKey()
3434
if err != nil {
3535
return fmt.Errorf("failed to get API key: %w", err)
3636
}
3737

38-
instanceID, err := strconv.Atoi(args[0])
38+
if updateInstanceID == "" {
39+
return fmt.Errorf("--id is required")
40+
}
41+
42+
instanceID, err := strconv.Atoi(updateInstanceID)
3943
if err != nil {
4044
return fmt.Errorf("invalid instance ID: %v", err)
4145
}
@@ -64,8 +68,11 @@ You can update the following fields:
6468
}
6569

6670
func init() {
71+
instanceUpdateCmd.Flags().StringVar(&updateInstanceID, "id", "", "Instance ID (required)")
6772
instanceUpdateCmd.Flags().StringVar(&updateInstanceName, "name", "", "New instance name")
6873
instanceUpdateCmd.Flags().StringVar(&updateInstancePlan, "plan", "", "New subscription plan")
6974
instanceUpdateCmd.Flags().StringSliceVar(&updateInstanceTags, "tags", []string{}, "New instance tags")
75+
instanceUpdateCmd.MarkFlagRequired("id")
76+
instanceUpdateCmd.RegisterFlagCompletionFunc("id", completeInstances)
7077
instanceUpdateCmd.RegisterFlagCompletionFunc("plan", completePlans)
7178
}

cmd/vpc_delete.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,32 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14-
var forceDeleteVPC bool
14+
var (
15+
deleteVPCID string
16+
forceDeleteVPC bool
17+
)
1518

1619
var vpcDeleteCmd = &cobra.Command{
17-
Use: "delete <id>",
20+
Use: "delete --id <id>",
1821
Short: "Delete a CloudAMQP VPC",
1922
Long: `Delete a CloudAMQP VPC permanently.
2023
2124
WARNING: This action cannot be undone. All instances in the VPC must be deleted first.`,
22-
Example: ` cloudamqp vpc delete 5678
23-
cloudamqp vpc delete 5678 --force`,
24-
Args: cobra.ExactArgs(1),
25-
ValidArgsFunction: completeVPCArgs,
25+
Example: ` cloudamqp vpc delete --id 5678
26+
cloudamqp vpc delete --id 5678 --force`,
27+
Args: cobra.NoArgs,
2628
RunE: func(cmd *cobra.Command, args []string) error {
2729
var err error
2830
apiKey, err = getAPIKey()
2931
if err != nil {
3032
return fmt.Errorf("failed to get API key: %w", err)
3133
}
3234

33-
vpcID, err := strconv.Atoi(args[0])
35+
if deleteVPCID == "" {
36+
return fmt.Errorf("--id is required")
37+
}
38+
39+
vpcID, err := strconv.Atoi(deleteVPCID)
3440
if err != nil {
3541
return fmt.Errorf("invalid VPC ID: %v", err)
3642
}
@@ -64,5 +70,8 @@ WARNING: This action cannot be undone. All instances in the VPC must be deleted
6470
}
6571

6672
func init() {
73+
vpcDeleteCmd.Flags().StringVar(&deleteVPCID, "id", "", "VPC ID (required)")
6774
vpcDeleteCmd.Flags().BoolVar(&forceDeleteVPC, "force", false, "Skip confirmation prompt")
75+
vpcDeleteCmd.MarkFlagRequired("id")
76+
vpcDeleteCmd.RegisterFlagCompletionFunc("id", completeVPCArgs)
6877
}

cmd/vpc_update.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,34 @@ import (
99
)
1010

1111
var (
12+
updateVPCID string
1213
updateVPCName string
1314
updateVPCTags []string
1415
)
1516

1617
var vpcUpdateCmd = &cobra.Command{
17-
Use: "update <id>",
18+
Use: "update --id <id>",
1819
Short: "Update a CloudAMQP VPC",
1920
Long: `Update an existing CloudAMQP VPC with new configuration.
2021
2122
You can update the following fields:
2223
--name: VPC name
2324
--tags: VPC tags (replaces existing tags)`,
24-
Example: ` cloudamqp vpc update 5678 --name=new-vpc-name
25-
cloudamqp vpc update 5678 --tags=production --tags=updated`,
26-
Args: cobra.ExactArgs(1),
27-
ValidArgsFunction: completeVPCArgs,
25+
Example: ` cloudamqp vpc update --id 5678 --name=new-vpc-name
26+
cloudamqp vpc update --id 5678 --tags=production --tags=updated`,
27+
Args: cobra.NoArgs,
2828
RunE: func(cmd *cobra.Command, args []string) error {
2929
var err error
3030
apiKey, err = getAPIKey()
3131
if err != nil {
3232
return fmt.Errorf("failed to get API key: %w", err)
3333
}
3434

35-
vpcID, err := strconv.Atoi(args[0])
35+
if updateVPCID == "" {
36+
return fmt.Errorf("--id is required")
37+
}
38+
39+
vpcID, err := strconv.Atoi(updateVPCID)
3640
if err != nil {
3741
return fmt.Errorf("invalid VPC ID: %v", err)
3842
}
@@ -60,6 +64,9 @@ You can update the following fields:
6064
}
6165

6266
func init() {
67+
vpcUpdateCmd.Flags().StringVar(&updateVPCID, "id", "", "VPC ID (required)")
6368
vpcUpdateCmd.Flags().StringVar(&updateVPCName, "name", "", "New VPC name")
6469
vpcUpdateCmd.Flags().StringSliceVar(&updateVPCTags, "tags", []string{}, "New VPC tags")
70+
vpcUpdateCmd.MarkFlagRequired("id")
71+
vpcUpdateCmd.RegisterFlagCompletionFunc("id", completeVPCArgs)
6572
}

0 commit comments

Comments
 (0)