Skip to content

Commit

Permalink
Remove select functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sestrella committed Nov 12, 2024
1 parent cff4b0b commit 3cc95e3
Showing 1 changed file with 34 additions and 51 deletions.
85 changes: 34 additions & 51 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,54 @@ func Execute() {
}

func describeCluster(ctx context.Context, client *ecs.Client, clusterId string) (*types.Cluster, error) {
selectedClusterId, err := selectClusterId(ctx, client, clusterId)
if err != nil {
return nil, err
if clusterId == "" {
listedClusters, err := client.ListClusters(ctx, &ecs.ListClustersInput{})
if err != nil {
return nil, err
}
clusterArn, err := pterm.DefaultInteractiveSelect.WithOptions(listedClusters.ClusterArns).Show("Cluster")
if err != nil {
return nil, err
}
clusterId = clusterArn
}
describedClusters, err := client.DescribeClusters(ctx, &ecs.DescribeClustersInput{
Clusters: []string{*selectedClusterId},
Clusters: []string{clusterId},
})
if err != nil {
return nil, err
}
if len(describedClusters.Clusters) == 1 {
return &describedClusters.Clusters[0], nil
}
return nil, fmt.Errorf("no cluster '%v' found", *selectedClusterId)
return nil, fmt.Errorf("no cluster '%v' found", clusterId)
}

func selectClusterId(ctx context.Context, client *ecs.Client, clusterId string) (*string, error) {
if clusterId == "" {
listedClusters, err := client.ListClusters(ctx, &ecs.ListClustersInput{})
func describeTask(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*types.Task, error) {
if taskId == "" {
listTasks, err := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterId,
})
if err != nil {
return nil, err
}
clusterArn, err := pterm.DefaultInteractiveSelect.WithOptions(listedClusters.ClusterArns).Show("Cluster")
taskArn, err := pterm.DefaultInteractiveSelect.WithOptions(listTasks.TaskArns).Show("Task")
if err != nil {
return nil, err
}
return &clusterArn, nil
}
return &clusterId, nil
}

func describeTask(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*types.Task, error) {
selectedTaskId, err := selectTaskId(ctx, client, clusterId, taskId)
if err != nil {
return nil, err
taskId = taskArn
}
describedTasks, err := client.DescribeTasks(ctx, &ecs.DescribeTasksInput{
Cluster: &clusterId,
Tasks: []string{*selectedTaskId},
Tasks: []string{taskId},
})
if err != nil {
return nil, err
}
if len(describedTasks.Tasks) == 1 {
return &describedTasks.Tasks[0], nil
}
return nil, fmt.Errorf("no task '%v' found", selectedTaskId)
}

func selectTaskId(ctx context.Context, client *ecs.Client, clusterId string, taskId string) (*string, error) {
if taskId == "" {
listTasks, _ := client.ListTasks(ctx, &ecs.ListTasksInput{
Cluster: &clusterId,
})
taskArn, err := pterm.DefaultInteractiveSelect.WithOptions(listTasks.TaskArns).Show("Task")
if err != nil {
return nil, err
}
return &taskArn, nil
}
return &taskId, nil
return nil, fmt.Errorf("no task '%v' found", taskId)
}

func describeContainer(containers []types.Container, containerId string) (*types.Container, error) {
Expand All @@ -95,11 +82,11 @@ func describeContainer(containers []types.Container, containerId string) (*types
for _, container := range containers {
containerNames = append(containerNames, *container.Name)
}
selectedContainerName, err := pterm.DefaultInteractiveSelect.WithOptions(containerNames).Show("Container")
containerName, err := pterm.DefaultInteractiveSelect.WithOptions(containerNames).Show("Container")
if err != nil {
return nil, err
}
containerId = selectedContainerName
containerId = containerName
}
for _, container := range containers {
if *container.Name == containerId {
Expand All @@ -117,25 +104,21 @@ func describeContainerDefinition(ctx context.Context, client *ecs.Client, taskDe
return nil, err
}
containerDefinitions := describedTaskDefinition.TaskDefinition.ContainerDefinitions
selectedContainerName, err := selectContainerName(containerDefinitions, containerId)
if err != nil {
return nil, err
}
for _, containerDefinition := range containerDefinitions {
if *containerDefinition.Name == selectedContainerName {
return &containerDefinition, nil
}
}
return nil, fmt.Errorf("no container '%v' found", selectedContainerName)
}

func selectContainerName(containerDefinitions []types.ContainerDefinition, containerId string) (string, error) {
if containerId == "" {
var containerNames []string
for _, containerDefinition := range containerDefinitions {
containerNames = append(containerNames, *containerDefinition.Name)
}
return pterm.DefaultInteractiveSelect.WithOptions(containerNames).Show("Container")
containerName, err := pterm.DefaultInteractiveSelect.WithOptions(containerNames).Show("Container")
if err != nil {
return nil, err
}
containerId = containerName
}
for _, containerDefinition := range containerDefinitions {
if *containerDefinition.Name == containerId {
return &containerDefinition, nil
}
}
return containerId, nil
return nil, fmt.Errorf("no container '%v' found", containerId)
}

0 comments on commit 3cc95e3

Please sign in to comment.