Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output for 'cbdinocluster ps' #52

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ Useful for testing magma buckets, advanced search indexes (1536mb for KV, 1024mb
./cbdinocluster buckets load-sample {{CLUSTER_ID}} travel-sample
```

#### Use JSON output to get connection string of the first cluster

```
cbdinocluster connstr $(cbdinocluster ps --json | jq -r '.[0].id')
```

### Advanced Usage

#### Resetting Colima
Expand Down
84 changes: 65 additions & 19 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ type deployerCluster struct {
Info deployment.ClusterInfo
}

type ClusterListOutput []ClusterListOutput_Item

type ClusterListOutput_Item struct {
ID string `json:"id"`
State string `json:"state"`
Expiry *time.Time `json:"expiry,omitempty"`
Deployer string `json:"deployer"`
Nodes []ClusterListOutput_Node `json:"nodes"`
}

type ClusterListOutput_Node struct {
ID string `json:"id"`
Name string `json:"name"`
IPAddress string `json:"ip_address"`
ResourceID string `json:"resource_id"`
}

var listCmd = &cobra.Command{
Use: "list",
Aliases: []string{"ls", "ps"},
Expand All @@ -24,6 +41,8 @@ var listCmd = &cobra.Command{
logger := helper.GetLogger()
ctx := helper.GetContext()

outputJson, _ := cmd.Flags().GetBool("json")

var wg sync.WaitGroup
clustersCh := make(chan *deployerCluster, 1024)

Expand Down Expand Up @@ -57,29 +76,56 @@ var listCmd = &cobra.Command{
clusters = append(clusters, clusterInfo)
}

fmt.Printf("Clusters:\n")
for _, clusterInfo := range clusters {
deployerName := clusterInfo.DeployerName
cluster := clusterInfo.Info
if !outputJson {
fmt.Printf("Clusters:\n")
for _, clusterInfo := range clusters {
deployerName := clusterInfo.DeployerName
cluster := clusterInfo.Info

expiry := cluster.GetExpiry()
expiryStr := "none"
if !expiry.IsZero() {
expiryStr = time.Until(cluster.GetExpiry()).Round(time.Second).String()
expiry := cluster.GetExpiry()
expiryStr := "none"
if !expiry.IsZero() {
expiryStr = time.Until(cluster.GetExpiry()).Round(time.Second).String()
}

fmt.Printf(" %s [State: %s, Timeout: %s, Deployer: %s]\n",
cluster.GetID(),
cluster.GetState(),
expiryStr,
deployerName)
for _, node := range cluster.GetNodes() {
fmt.Printf(" %-16s %-20s %-20s %s\n",
node.GetID(),
node.GetName(),
node.GetIPAddress(),
node.GetResourceID())
}
}
} else {
var out ClusterListOutput
for _, cluster := range clusters {
clusterItem := ClusterListOutput_Item{
ID: cluster.Info.GetID(),
State: cluster.Info.GetState(),
Deployer: cluster.DeployerName,
}

fmt.Printf(" %s [State: %s, Timeout: %s, Deployer: %s]\n",
cluster.GetID(),
cluster.GetState(),
expiryStr,
deployerName)
for _, node := range cluster.GetNodes() {
fmt.Printf(" %-16s %-20s %-20s %s\n",
node.GetID(),
node.GetName(),
node.GetIPAddress(),
node.GetResourceID())
expiry := cluster.Info.GetExpiry()
if !expiry.IsZero() {
clusterItem.Expiry = &expiry
}

for _, node := range cluster.Info.GetNodes() {
clusterItem.Nodes = append(clusterItem.Nodes, ClusterListOutput_Node{
ID: node.GetID(),
Name: node.GetName(),
IPAddress: node.GetIPAddress(),
ResourceID: node.GetResourceID(),
})
}
out = append(out, clusterItem)
}
helper.OutputJson(out)
}
},
}
Expand Down
Loading