Skip to content

Commit

Permalink
Extract task selector
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Oct 17, 2024
1 parent 98eec5e commit e863637
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
37 changes: 1 addition & 36 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
Expand Down Expand Up @@ -56,7 +55,7 @@ func runExec(ctx context.Context, clusterId string, taskId string, containerId s
if err != nil {
return err
}
task, err := selectTask(ctx, client, *cluster.ClusterName, taskId)
task, err := selector.SelectTask(ctx, client, *cluster.ClusterName, taskId)
if err != nil {
return err
}
Expand Down Expand Up @@ -113,40 +112,6 @@ func runExec(ctx context.Context, clusterId string, taskId string, containerId s
return smp.Run()
}

func selectTask(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*types.Task, error) {
if taskId != "" {
return describeTask(ctx, client, clusterId, taskId)
}
output, err := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterId,
})
if err != nil {
return nil, fmt.Errorf("Error listing tasks: %w", err)
}
if len(output.TaskArns) == 0 {
return nil, errors.New("No tasks found")
}
taskArn, err := pterm.DefaultInteractiveSelect.WithOptions(output.TaskArns).Show("Select a task")
if err != nil {
return nil, fmt.Errorf("Error selecting task: %w", err)
}
return describeTask(ctx, client, clusterId, taskArn)
}

func describeTask(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*types.Task, error) {
output, err := client.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &clusterId,
Tasks: []string{taskId},
})
if err != nil {
return nil, fmt.Errorf("Error describing task '%s': %w", taskId, err)
}
if len(output.Tasks) == 0 {
return nil, fmt.Errorf("No task '%s' found", taskId)
}
return &output.Tasks[0], nil
}

func selectContainer(task types.Task, containerId string) (*types.Container, error) {
var containerNames []string
for _, container := range task.Containers {
Expand Down
Binary file removed iecs
Binary file not shown.
45 changes: 45 additions & 0 deletions internal/selector/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 SelectTask(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*types.Task, error) {
if taskId != "" {
return describeTask(ctx, client, clusterId, taskId)
}
output, err := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterId,
})
if err != nil {
return nil, fmt.Errorf("Error listing tasks: %w", err)
}
if len(output.TaskArns) == 0 {
return nil, errors.New("No tasks found")
}
taskArn, err := pterm.DefaultInteractiveSelect.WithOptions(output.TaskArns).Show("Select a task")
if err != nil {
return nil, fmt.Errorf("Error selecting task: %w", err)
}
return describeTask(ctx, client, clusterId, taskArn)
}

func describeTask(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*types.Task, error) {
output, err := client.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &clusterId,
Tasks: []string{taskId},
})
if err != nil {
return nil, fmt.Errorf("Error describing task '%s': %w", taskId, err)
}
if len(output.Tasks) == 0 {
return nil, fmt.Errorf("No task '%s' found", taskId)
}
return &output.Tasks[0], nil
}

0 comments on commit e863637

Please sign in to comment.