Skip to content

Commit

Permalink
Extract container selector
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Oct 17, 2024
1 parent e863637 commit 3ab0d82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
23 changes: 1 addition & 22 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"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/iecs/internal/selector"
Expand Down Expand Up @@ -59,7 +58,7 @@ func runExec(ctx context.Context, clusterId string, taskId string, containerId s
if err != nil {
return err
}
container, err := selectContainer(*task, containerId)
container, err := selector.SelectContainer(*task, containerId)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,26 +111,6 @@ func runExec(ctx context.Context, clusterId string, taskId string, containerId s
return smp.Run()
}

func selectContainer(task types.Task, containerId string) (*types.Container, error) {
var containerNames []string
for _, container := range task.Containers {
if *container.Name == containerId {
return &container, nil
}
containerNames = append(containerNames, *container.Name)
}
containerName, err := pterm.DefaultInteractiveSelect.WithOptions(containerNames).Show("Select a container")
if err != nil {
return nil, fmt.Errorf("Error selecting a container: %w", err)
}
for _, container := range task.Containers {
if *container.Name == containerName {
return &container, nil
}
}
return nil, fmt.Errorf("No container '%s' found in task '%s'", containerName, *task.TaskArn)
}

func init() {
rootCmd.AddCommand(execCmd)

Expand Down
28 changes: 28 additions & 0 deletions internal/selector/container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package selector

import (
"fmt"

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

func SelectContainer(task types.Task, containerId string) (*types.Container, error) {
var containerNames []string
for _, container := range task.Containers {
if *container.Name == containerId {
return &container, nil
}
containerNames = append(containerNames, *container.Name)
}
containerName, err := pterm.DefaultInteractiveSelect.WithOptions(containerNames).Show("Select a container")
if err != nil {
return nil, fmt.Errorf("Error selecting a container: %w", err)
}
for _, container := range task.Containers {
if *container.Name == containerName {
return &container, nil
}
}
return nil, fmt.Errorf("No container '%s' found in task '%s'", containerName, *task.TaskArn)
}

0 comments on commit 3ab0d82

Please sign in to comment.