-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement list command for Cluster (#355)
- Loading branch information
Showing
8 changed files
with
173 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package api_types | ||
|
||
type RESTResponse[T any] struct { | ||
Data []T `json:"data"` | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cluster | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"sync" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
"github.com/buildkite/cli/v3/internal/cluster" | ||
"github.com/buildkite/cli/v3/pkg/cmd/factory" | ||
"github.com/buildkite/go-buildkite/v3/buildkite" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdClusterList(f *factory.Factory) *cobra.Command { | ||
cmd := cobra.Command{ | ||
DisableFlagsInUseLine: true, | ||
Use: "list", | ||
Short: "List all clusters.", | ||
Long: heredoc.Doc(` | ||
List the clusters for an organization. | ||
`), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var listOptions buildkite.ClustersListOptions | ||
|
||
clusters, err := listClusters(&listOptions, f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
summary := cluster.ClusterViewTable(clusters...) | ||
fmt.Fprintf(os.Stdout, "%v\n", summary) | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return &cmd | ||
} | ||
|
||
func listClusters(lo *buildkite.ClustersListOptions, f *factory.Factory) ([]buildkite.Cluster, error) { | ||
clusters, _, err := f.RestAPIClient.Clusters.List(f.Config.OrganizationSlug(), lo) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching cluster list: %v", err) | ||
} | ||
|
||
if len(clusters) < 1 { | ||
return nil, errors.New("no clusters found in organization") | ||
} | ||
|
||
clusterList := make([]buildkite.Cluster, len(clusters)) | ||
var wg sync.WaitGroup | ||
errChan := make(chan error, len(clusters)) | ||
for i, c := range clusters { | ||
wg.Add(1) | ||
go func(i int, c buildkite.Cluster) { | ||
defer wg.Done() | ||
clusterList[i] = buildkite.Cluster{ | ||
Color: c.Color, | ||
CreatedAt: c.CreatedAt, | ||
CreatedBy: c.CreatedBy, | ||
DefaultQueueID: c.DefaultQueueID, | ||
DefaultQueueURL: c.DefaultQueueURL, | ||
Description: c.Description, | ||
Emoji: c.Emoji, | ||
GraphQLID: c.GraphQLID, | ||
ID: c.ID, | ||
Name: c.Name, | ||
QueuesURL: c.QueuesURL, | ||
URL: c.URL, | ||
WebURL: c.WebURL, | ||
} | ||
}(i, c) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(errChan) | ||
}() | ||
|
||
for err := range errChan { | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return clusterList, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters