Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IZE-707 added task flag #529

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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