-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathexec.go
More file actions
121 lines (109 loc) · 3.44 KB
/
exec.go
File metadata and controls
121 lines (109 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package ecspresso
import (
"context"
"fmt"
"github.com/fujiwara/ecsta"
)
type ExecOption struct {
ID string `help:"task ID" default:""`
Container string `help:"container name" default:""`
Run *ExecRunOption `cmd:"" default:"withargs" help:"execute command on task"`
Portforward *ExecPortforwardOption `cmd:"" help:"port forwarding to a task"`
Cp *ExecCpOption `cmd:"" help:"copy files between local and task"`
}
// ExecRunOption is the default subcommand for exec.
// Deprecated flags are kept as hidden for backward compatibility.
type ExecRunOption struct {
Command string `help:"command to execute" default:"sh"`
PortForward bool `name:"port-forward" hidden:"" default:"false"`
LocalPort int `name:"local-port" hidden:"" default:"0"`
Port int `name:"port" hidden:"" default:"0"`
Host string `name:"host" hidden:"" default:""`
L string `name:"L" short:"L" hidden:"" default:""`
}
type ExecPortforwardOption struct {
LocalPort int `help:"local port number" default:"0"`
Port int `help:"remote port number" default:"0"`
Host string `help:"remote host" default:""`
L string `name:"L" short:"L" help:"short expression of local-port:host:port" default:""`
}
type ExecCpOption struct {
Src string `arg:"" help:"source"`
Dest string `arg:"" help:"destination"`
Port int `help:"port number for file transfer" default:"12345"`
Progress bool `help:"show progress bar" default:"true" negatable:""`
}
func (d *App) NewEcsta(ctx context.Context) (*ecsta.Ecsta, error) {
app, err := ecsta.New(ctx, d.config.Region, d.config.Cluster)
if err != nil {
return nil, fmt.Errorf("failed to create ecsta application: %w", err)
}
if fc := d.FilterCommand(); fc != "" {
app.Config.Set("filter_command", fc)
}
return app, nil
}
func (d *App) Exec(ctx context.Context, opt ExecOption) error {
// Do not call d.Start() because timeout disabled for exec.
ecstaApp, err := d.NewEcsta(ctx)
if err != nil {
return err
}
family, err := d.taskDefinitionFamily(ctx)
if err != nil {
return err
}
var service *string
if d.config.Service != "" {
service = &d.config.Service
}
switch {
case opt.Cp != nil:
return ecstaApp.RunCp(ctx, &ecsta.CpOption{
Src: opt.Cp.Src,
Dest: opt.Cp.Dest,
Port: opt.Cp.Port,
Progress: opt.Cp.Progress,
ID: opt.ID,
Container: opt.Container,
Family: &family,
Service: service,
})
case opt.Portforward != nil:
return ecstaApp.RunPortforward(ctx, &ecsta.PortforwardOption{
ID: opt.ID,
Container: opt.Container,
LocalPort: opt.Portforward.LocalPort,
RemotePort: opt.Portforward.Port,
RemoteHost: opt.Portforward.Host,
L: opt.Portforward.L,
Family: &family,
Service: service,
})
case opt.Run != nil:
run := opt.Run
switch {
case run.PortForward:
LogWarn("--port-forward flag is deprecated, use 'exec portforward' subcommand instead")
return ecstaApp.RunPortforward(ctx, &ecsta.PortforwardOption{
ID: opt.ID,
Container: opt.Container,
LocalPort: run.LocalPort,
RemotePort: run.Port,
RemoteHost: run.Host,
L: run.L,
Family: &family,
Service: service,
})
default:
return ecstaApp.RunExec(ctx, &ecsta.ExecOption{
ID: opt.ID,
Command: run.Command,
Container: opt.Container,
Family: &family,
Service: service,
})
}
}
return nil
}