Skip to content

Commit

Permalink
added task flag
Browse files Browse the repository at this point in the history
  • Loading branch information
psihachina committed Nov 8, 2022
1 parent 2583ab4 commit 25ee69d
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions internal/commands/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package commands

import (
"fmt"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/ecs"
Expand All @@ -19,6 +20,7 @@ type LogsOptions struct {
Config *config.Project
AppName string
EcsCluster string
Task string
}

func NewLogsFlags(project *config.Project) *LogsOptions {
Expand Down Expand Up @@ -57,6 +59,7 @@ func NewCmdLogs(project *config.Project) *cobra.Command {
}

cmd.Flags().StringVar(&o.EcsCluster, "ecs-cluster", "", "set ECS cluster name")
cmd.Flags().StringVar(&o.Task, "task", "", "set ECS task id")

return cmd
}
Expand All @@ -72,14 +75,6 @@ func (o *LogsOptions) Complete(cmd *cobra.Command) error {
}

func (o *LogsOptions) Validate() error {
if len(o.Config.Env) == 0 {
return fmt.Errorf("can't validate: env must be specified\n")
}

if len(o.Config.Namespace) == 0 {
return fmt.Errorf("can't validate: namespace must be specified\n")
}

if len(o.AppName) == 0 {
return fmt.Errorf("can't validate: app name must be specified\n")
}
Expand All @@ -89,21 +84,24 @@ func (o *LogsOptions) Validate() error {
func (o *LogsOptions) Run() error {
logGroup := fmt.Sprintf("%s-%s", o.Config.Env, o.AppName)

lto, err := o.Config.AWSClient.ECSClient.ListTasks(&ecs.ListTasksInput{
Cluster: &o.EcsCluster,
DesiredStatus: aws.String("RUNNING"),
ServiceName: &logGroup,
MaxResults: aws.Int64(1),
})
taskID := o.Task
if len(taskID) == 0 {
lto, err := o.Config.AWSClient.ECSClient.ListTasks(&ecs.ListTasksInput{
Cluster: &o.EcsCluster,
DesiredStatus: aws.String("RUNNING"),
ServiceName: &logGroup,
MaxResults: aws.Int64(1),
})

logrus.Infof("log group: %s, cluster name: %s", logGroup, o.EcsCluster)
logrus.Infof("log group: %s, cluster name: %s", logGroup, o.EcsCluster)

if err != nil {
return fmt.Errorf("can't run logs: %w", err)
}
if err != nil {
return fmt.Errorf("can't run logs: %w", err)
}

taskID := *lto.TaskArns[0]
taskID = taskID[strings.LastIndex(taskID, "/")+1:]
taskID = *lto.TaskArns[0]
taskID = taskID[strings.LastIndex(taskID, "/")+1:]
}

var token *string
logStreamName := fmt.Sprintf("main/%s/%s", o.AppName, taskID)
Expand Down

0 comments on commit 25ee69d

Please sign in to comment.