-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance_get.go
More file actions
92 lines (77 loc) · 2.02 KB
/
instance_get.go
File metadata and controls
92 lines (77 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package cmd
import (
"fmt"
"net/url"
"strconv"
"strings"
"cloudamqp-cli/client"
"github.com/spf13/cobra"
)
func maskPassword(urlStr string) string {
parsedURL, err := url.Parse(urlStr)
if err != nil {
return urlStr
}
password, _ := parsedURL.User.Password()
return strings.Replace(urlStr, password, "****", 1)
}
var instanceGetCmd = &cobra.Command{
Use: "get --id <id>",
Short: "Get details of a specific CloudAMQP instance",
Long: `Retrieves and displays detailed information about a specific CloudAMQP instance.`,
Example: ` cloudamqp instance get --id 1234`,
RunE: func(cmd *cobra.Command, args []string) error {
idFlag, _ := cmd.Flags().GetString("id")
if idFlag == "" {
return fmt.Errorf("instance ID is required. Use --id flag")
}
var err error
apiKey, err = getAPIKey()
if err != nil {
return fmt.Errorf("failed to get API key: %w", err)
}
instanceID, err := strconv.Atoi(idFlag)
if err != nil {
return fmt.Errorf("invalid instance ID: %v", err)
}
c := client.New(apiKey, Version)
instance, err := c.GetInstance(instanceID)
if err != nil {
fmt.Printf("Error getting instance: %v\n", err)
return err
}
p, err := getPrinter(cmd)
if err != nil {
return err
}
showURL, _ := cmd.Flags().GetBool("show-url")
ready := "No"
if instance.Ready {
ready = "Yes"
}
urlVal := maskPassword(instance.URL)
if showURL {
urlVal = instance.URL
}
p.PrintRecord(
[]string{"ID", "NAME", "PLAN", "REGION", "TAGS", "URL", "HOSTNAME", "READY"},
[]string{
strconv.Itoa(instance.ID),
instance.Name,
instance.Plan,
instance.Region,
strings.Join(instance.Tags, ","),
urlVal,
instance.HostnameExternal,
ready,
},
)
return nil
},
}
func init() {
instanceGetCmd.Flags().StringP("id", "", "", "Instance ID (required)")
instanceGetCmd.MarkFlagRequired("id")
instanceGetCmd.Flags().BoolP("show-url", "", false, "Show full connection URL with credentials")
instanceGetCmd.RegisterFlagCompletionFunc("id", completeInstanceIDFlag)
}