From 7604b0f678071f52430a80ab4fc824d6a4da1832 Mon Sep 17 00:00:00 2001 From: Nikita Podshivalov Date: Tue, 8 Nov 2022 13:10:52 +0300 Subject: [PATCH 1/2] added explain for `ize exec` --- internal/commands/exec.go | 42 ++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/internal/commands/exec.go b/internal/commands/exec.go index e7616b83..0dc01135 100644 --- a/internal/commands/exec.go +++ b/internal/commands/exec.go @@ -2,6 +2,9 @@ package commands import ( "fmt" + "strings" + "text/template" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ecs" @@ -12,7 +15,6 @@ import ( "github.com/pterm/pterm" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "strings" ) type ExecOptions struct { @@ -22,8 +24,21 @@ type ExecOptions struct { Command []string Task string ContainerName string + Explain bool } +var explainExecTmpl = ` +TASK_ID=$(aws ecs list-tasks --cluster {{.Env}}-{{.Namespace}} --service-name {{.Env}}-{{svc}} --desired-status "RUNNING" | jq -r '.taskArns[]' | cut -d'/' -f3 | head -n 1) + +aws ecs execute-command \ + --region {{.AwsRegion}} \ + --cluster {{.Env}}-{{.Namespace}} \ + --task $TASK_ID \ + --container {{svc}} \ + --command {{command}} \ + --interactive +` + var execExample = templates.Examples(` # Connect to a container in the ECS via AWS SSM and run command. ize exec goblin ps aux @@ -70,6 +85,7 @@ func NewCmdExec(project *config.Project) *cobra.Command { cmd.Flags().StringVar(&o.EcsCluster, "ecs-cluster", "", "set ECS cluster name") cmd.Flags().StringVar(&o.Task, "task", "", "set task id") cmd.Flags().StringVar(&o.ContainerName, "container-name", "", "set container name") + cmd.Flags().BoolVar(&o.Explain, "explain", false, "bash alternative shown") return cmd } @@ -97,14 +113,6 @@ func (o *ExecOptions) Complete(cmd *cobra.Command, args []string, argsLenAtDash } func (o *ExecOptions) Validate() error { - if len(o.Config.Env) == 0 { - return fmt.Errorf("can't validate: env must be specified") - } - - if len(o.Config.Namespace) == 0 { - return fmt.Errorf("can't validate: namespace must be specified") - } - if len(o.AppName) == 0 { return fmt.Errorf("can't validate: app name must be specified") } @@ -119,6 +127,22 @@ func (o *ExecOptions) Validate() error { func (o *ExecOptions) Run() error { appName := fmt.Sprintf("%s-%s", o.Config.Env, o.AppName) + if o.Explain { + err := o.Config.Generate(explainExecTmpl, template.FuncMap{ + "svc": func() string { + return o.AppName + }, + "command": func() string { + return strings.Join(o.Command, " ") + }, + }) + if err != nil { + return err + } + + return nil + } + logrus.Infof("app name: %s, cluster name: %s", appName, o.EcsCluster) logrus.Infof("region: %s, profile: %s", o.Config.AwsProfile, o.Config.AwsRegion) From 52937239ff448de9d589b22ebbc9cc3a6ef14766 Mon Sep 17 00:00:00 2001 From: Nikita Podshivalov <47272597+psihachina@users.noreply.github.com> Date: Fri, 11 Nov 2022 11:09:55 +0300 Subject: [PATCH 2/2] updated bash command Co-authored-by: Dmitry Kireev --- internal/commands/exec.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/commands/exec.go b/internal/commands/exec.go index 0dc01135..bf8c7a1b 100644 --- a/internal/commands/exec.go +++ b/internal/commands/exec.go @@ -31,12 +31,12 @@ var explainExecTmpl = ` TASK_ID=$(aws ecs list-tasks --cluster {{.Env}}-{{.Namespace}} --service-name {{.Env}}-{{svc}} --desired-status "RUNNING" | jq -r '.taskArns[]' | cut -d'/' -f3 | head -n 1) aws ecs execute-command \ + --interactive \ --region {{.AwsRegion}} \ --cluster {{.Env}}-{{.Namespace}} \ --task $TASK_ID \ --container {{svc}} \ - --command {{command}} \ - --interactive + --command {{command}} ` var execExample = templates.Examples(`