Skip to content

Commit

Permalink
Merge pull request #9 from chnacib/feat/listtask
Browse files Browse the repository at this point in the history
Feature: list task for cluster
  • Loading branch information
chnacib authored Apr 26, 2024
2 parents 9b43a06 + cd6eb61 commit 14a2429
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions pkg/ecs/listtasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,63 @@ import (

func ListTasks() *cobra.Command {
var cluster string
var taskArns []*string

cmd := &cobra.Command{
Use: "tasks",
Aliases: []string{"tsk", "task"},
Short: "List ECS tasks from services",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("No service specified")
return
}

service := args[0]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
client := ecs.New(sess)
if len(args) == 0 {
listTaskInput := &ecs.ListTasksInput{
Cluster: aws.String(cluster),
}

listTaskOutput, err := client.ListTasks(listTaskInput)
if err != nil {
fmt.Printf("Tasks not found for cluster %s", cluster)
os.Exit(0)
}

taskArns = append(taskArns, listTaskOutput.TaskArns...)

describeTasksInput := &ecs.DescribeTasksInput{
Tasks: taskArns,
Cluster: aws.String(cluster),
}

describeTasksOutput, err := client.DescribeTasks(describeTasksInput)
if err != nil {
fmt.Printf("Tasks not found for cluster %s\n", cluster)
os.Exit(0)
}

w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
//defer w.Flush()

fmt.Fprintln(w, "NAME\tREVISION\tSTATUS\tCPU\tMEMORY\tNETWORK\tSTARTED")

for _, task := range describeTasksOutput.Tasks {
taskName := NameArn(aws.StringValue(task.TaskArn))
revision := NameArn(aws.StringValue(task.TaskDefinitionArn))
cpu := aws.StringValue(task.Cpu)
memory := aws.StringValue(task.Memory)
status := aws.StringValue(task.LastStatus)
network := aws.StringValue(task.Containers[0].NetworkInterfaces[0].PrivateIpv4Address)
timeStart := aws.TimeValue(task.StartedAt)
formatTime := timeStart.Format("2006-01-02 15:04:05")

fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", taskName, revision, status, cpu, memory, network, formatTime)
}
w.Flush()
os.Exit(0)
}

service := args[0]

input := &ecs.ListTasksInput{
ServiceName: aws.String(service),
Expand All @@ -43,10 +84,7 @@ func ListTasks() *cobra.Command {
os.Exit(0)
}

var taskArns []*string
for _, taskArn := range response.TaskArns {
taskArns = append(taskArns, taskArn)
}
taskArns = append(taskArns, response.TaskArns...)

describeTasksInput := &ecs.DescribeTasksInput{
Tasks: taskArns,
Expand Down

0 comments on commit 14a2429

Please sign in to comment.