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-698 added explain for ize exec #530

Merged
merged 2 commits into from
Nov 11, 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
42 changes: 33 additions & 9 deletions internal/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -12,7 +15,6 @@ import (
"github.com/pterm/pterm"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
)

type ExecOptions struct {
Expand All @@ -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 \
--interactive \
--region {{.AwsRegion}} \
--cluster {{.Env}}-{{.Namespace}} \
--task $TASK_ID \
--container {{svc}} \
--command {{command}}
`

var execExample = templates.Examples(`
# Connect to a container in the ECS via AWS SSM and run command.
ize exec goblin ps aux
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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)

Expand Down