diff --git a/cmd/exec.go b/cmd/exec.go index 8206681..81e58bb 100644 --- a/cmd/exec.go +++ b/cmd/exec.go @@ -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" @@ -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 } @@ -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) diff --git a/internal/selector/container.go b/internal/selector/container.go new file mode 100644 index 0000000..d943e7b --- /dev/null +++ b/internal/selector/container.go @@ -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) +}