Skip to content

Commit

Permalink
Extract cluster selector to a separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Oct 17, 2024
1 parent ecdd943 commit d414bff
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
3 changes: 2 additions & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/pterm/pterm"
"github.com/sestrella/interactive-ecs/internal/selector"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func runExec(ctx context.Context, clusterId string, taskId string, containerId s
return fmt.Errorf("Error loading AWS configuration: %w", err)
}
client := ecs.NewFromConfig(cfg)
cluster, err := selectCluster(ctx, client, clusterId)
cluster, err := selector.SelectCluster(ctx, client, clusterId)
if err != nil {
return err
}
Expand Down
38 changes: 0 additions & 38 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
package cmd

import (
"context"
"errors"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

Expand All @@ -22,38 +16,6 @@ var rootCmd = &cobra.Command{
`,
}

func selectCluster(ctx context.Context, client *ecs.Client, clusterId string) (*types.Cluster, error) {
if clusterId == "" {
clusters, err := client.ListClusters(ctx, &ecs.ListClustersInput{})
if err != nil {
return nil, fmt.Errorf("Error listing clusters: %w", err)
}
if len(clusters.ClusterArns) == 0 {
return nil, errors.New("No clusters found")
}
clusterArn, err := pterm.DefaultInteractiveSelect.WithOptions(clusters.ClusterArns).Show("Select a cluster")
if err != nil {
return nil, fmt.Errorf("Error selecting a cluster: %w", err)
}
return describeCluster(ctx, client, clusterArn)
}
return describeCluster(ctx, client, clusterId)
}

func describeCluster(ctx context.Context, client *ecs.Client, clusterId string) (*types.Cluster, error) {
clusters, err := client.DescribeClusters(ctx, &ecs.DescribeClustersInput{
Clusters: []string{clusterId},
})
if err != nil {
return nil, fmt.Errorf("Error describing cluster '%s': %w", clusterId, err)
}
if len(clusters.Clusters) == 0 {
return nil, fmt.Errorf("No cluster '%s' found", clusterId)
}
cluster := clusters.Clusters[0]
return &cluster, nil
}

func Execute() {
err := rootCmd.Execute()
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions internal/selector/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package selector

import (
"context"
"errors"
"fmt"

"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/pterm/pterm"
)

func SelectCluster(ctx context.Context, client *ecs.Client, clusterId string) (*types.Cluster, error) {
if clusterId == "" {
clusters, err := client.ListClusters(ctx, &ecs.ListClustersInput{})
if err != nil {
return nil, fmt.Errorf("Error listing clusters: %w", err)
}
if len(clusters.ClusterArns) == 0 {
return nil, errors.New("No clusters found")
}
clusterArn, err := pterm.DefaultInteractiveSelect.WithOptions(clusters.ClusterArns).Show("Select a cluster")
if err != nil {
return nil, fmt.Errorf("Error selecting a cluster: %w", err)
}
return describeCluster(ctx, client, clusterArn)
}
return describeCluster(ctx, client, clusterId)
}

func describeCluster(ctx context.Context, client *ecs.Client, clusterId string) (*types.Cluster, error) {
clusters, err := client.DescribeClusters(ctx, &ecs.DescribeClustersInput{
Clusters: []string{clusterId},
})
if err != nil {
return nil, fmt.Errorf("Error describing cluster '%s': %w", clusterId, err)
}
if len(clusters.Clusters) == 0 {
return nil, fmt.Errorf("No cluster '%s' found", clusterId)
}
cluster := clusters.Clusters[0]
return &cluster, nil
}

0 comments on commit d414bff

Please sign in to comment.