diff --git a/awscmd/ecs.go b/awscmd/ecs.go index db781e5..2856158 100644 --- a/awscmd/ecs.go +++ b/awscmd/ecs.go @@ -221,6 +221,50 @@ type OuputEcsRunTask struct { ID string } +func EcsTaskWait(ctx context.Context, input *InputEcsTaskWait, w io.Writer) (*OuputEcsTaskWait, error) { + sess, err := NewSession(input.Region) + if err != nil { + return nil, err + } + svc := ecs.New(sess) + + attempt := 1 + for { + result, err := svc.DescribeTasksWithContext(ctx, &ecs.DescribeTasksInput{ + Cluster: aws.String(input.Cluster), + Tasks: []*string{aws.String(input.ARN)}, + }) + if err != nil { + return nil, err + } + + task := result.Tasks[0] + status := *task.LastStatus + + if status == "STOPPED" { + break + } else { + fmt.Fprintf(w, "Waiting to finish (`%s%s%s`).. (Check %d, retrying in 3s)\n", + ctc.ForegroundYellow, status, ctc.Reset, + attempt, + ) + } + + time.Sleep(3 * time.Second) + } + + return &OuputEcsTaskWait{}, nil +} + +type InputEcsTaskWait struct { + Region string + Cluster string + ARN string +} + +type OuputEcsTaskWait struct { +} + func ecsRegisterTaskDefinition(svc *ecs.ECS, ctx context.Context, input *inputRegisterTaskDefinition, w io.Writer) (*outputRegisterTaskDefinition, error) { fmt.Fprintf(w, " Fetching task definition\n") taskDefinitionOut, err := svc.DescribeTaskDefinitionWithContext(ctx, &ecs.DescribeTaskDefinitionInput{ diff --git a/main.go b/main.go index 6149651..5f4b6fc 100644 --- a/main.go +++ b/main.go @@ -220,7 +220,24 @@ func main() { } fmt.Fprintf( c.App.Writer, - "%sNew task started ID: `%s`%s\n", + "%sNew task started: `%s`%s\n", + ctc.ForegroundGreen, + out.ID, + ctc.Reset, + ) + waitInput := &awscmd.InputEcsTaskWait{ + Region: c.String("region"), + Cluster: c.String("cluster"), + ARN: out.ARN, + } + _, err = awscmd.EcsTaskWait(context.TODO(), waitInput, c.App.Writer) + if err != nil { + return err + } + + fmt.Fprintf( + c.App.Writer, + "%sTask `%s` has stopped%s\n", ctc.ForegroundGreen, out.ID, ctc.Reset,