-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstance_list.go
More file actions
119 lines (106 loc) · 2.58 KB
/
instance_list.go
File metadata and controls
119 lines (106 loc) · 2.58 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package cmd
import (
"fmt"
"strconv"
"strings"
"sync"
"cloudamqp-cli/client"
"github.com/spf13/cobra"
)
var instanceListCmd = &cobra.Command{
Use: "list",
Short: "List all CloudAMQP instances",
Long: `Retrieves and displays all CloudAMQP instances in your account.`,
Example: ` cloudamqp instance list`,
RunE: func(cmd *cobra.Command, args []string) error {
var err error
apiKey, err = getAPIKey()
if err != nil {
return fmt.Errorf("failed to get API key: %w", err)
}
c := client.New(apiKey, Version)
instances, err := c.ListInstances()
if err != nil {
fmt.Printf("Error listing instances: %v\n", err)
return err
}
if len(instances) == 0 {
fmt.Println("No instances found.")
return nil
}
p, err := getPrinter(cmd)
if err != nil {
return err
}
details, _ := cmd.Flags().GetBool("details")
if details {
showURL, _ := cmd.Flags().GetBool("show-url")
detailed := make([]*client.Instance, len(instances))
headers := []string{"ID", "NAME", "PLAN", "REGION", "TAGS", "URL", "HOSTNAME", "READY"}
rows := make([][]string, len(instances))
var (
mu sync.Mutex
firstErr error
wg sync.WaitGroup
)
for i, instance := range instances {
wg.Add(1)
go func(idx, id int) {
defer wg.Done()
det, err := c.GetInstance(id)
mu.Lock()
defer mu.Unlock()
if err != nil {
if firstErr == nil {
firstErr = fmt.Errorf("error fetching instance %d: %w", id, err)
}
return
}
detailed[idx] = det
}(i, instance.ID)
}
wg.Wait()
if firstErr != nil {
return firstErr
}
for i, inst := range detailed {
ready := "No"
if inst.Ready {
ready = "Yes"
}
urlVal := maskPassword(inst.URL)
if showURL {
urlVal = inst.URL
}
rows[i] = []string{
strconv.Itoa(inst.ID),
inst.Name,
inst.Plan,
inst.Region,
strings.Join(inst.Tags, ","),
urlVal,
inst.HostnameExternal,
ready,
}
}
p.PrintRecords(headers, rows)
return nil
}
headers := []string{"ID", "NAME", "PLAN", "REGION"}
rows := make([][]string, len(instances))
for i, instance := range instances {
rows[i] = []string{
strconv.Itoa(instance.ID),
instance.Name,
instance.Plan,
instance.Region,
}
}
p.PrintRecords(headers, rows)
return nil
},
}
func init() {
instanceListCmd.Flags().BoolP("details", "", false, "Fetch full details for each instance (one GET request per instance)")
instanceListCmd.Flags().BoolP("show-url", "", false, "Show full connection URL with credentials (requires --details)")
}