-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
cli.go
81 lines (74 loc) · 2.47 KB
/
cli.go
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
package ecsta
import (
"context"
"fmt"
"log/slog"
"strings"
"github.com/alecthomas/kong"
)
type CLI struct {
Cluster string `help:"ECS cluster name" short:"c" env:"ECS_CLUSTER"`
Region string `help:"AWS region" short:"r" env:"AWS_REGION"`
Output string `help:"output format (table, tsv, json)" short:"o" default:"table" enum:"table,tsv,json" env:"ECSTA_OUTPUT"`
TaskFormatQuery string `help:"A jq query to format task in selector" short:"q" env:"ECSTA_TASK_FORMAT_QUERY"`
Debug bool `help:"enable debug output" env:"ECSTA_DEBUG"`
Configure *ConfigureOption `cmd:"" help:"Create a configuration file of ecsta"`
Describe *DescribeOption `cmd:"" help:"Describe tasks"`
Exec *ExecOption `cmd:"" help:"Execute a command on a task"`
List *ListOption `cmd:"" help:"List tasks"`
Logs *LogsOption `cmd:"" help:"Show log messages of a task"`
Portforward *PortforwardOption `cmd:"" help:"Forward a port of a task"`
Stop *StopOption `cmd:"" help:"Stop a task"`
Trace *TraceOption `cmd:"" help:"Trace a task"`
CP *CpOption `cmd:"" help:"Copy files from/to a task"`
Version struct{} `cmd:"" help:"Show version"`
}
func RunCLI(ctx context.Context, args []string) error {
var cli CLI
parser, err := kong.New(&cli, kong.Vars{"version": Version})
if err != nil {
return err
}
kctx, err := parser.Parse(args)
if err != nil {
return err
}
if cli.Debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
} else {
slog.SetLogLoggerLevel(slog.LevelInfo)
}
app, err := New(ctx, cli.Region, cli.Cluster)
if err != nil {
return err
}
app.Config.OverrideCLI(&cli)
cmd := strings.Fields(kctx.Command())[0]
return app.Dispatch(ctx, cmd, &cli)
}
func (app *Ecsta) Dispatch(ctx context.Context, command string, cli *CLI) error {
switch command {
case "configure":
return app.RunConfigure(ctx, cli.Configure)
case "describe":
return app.RunDescribe(ctx, cli.Describe)
case "exec":
return app.RunExec(ctx, cli.Exec)
case "list":
return app.RunList(ctx, cli.List)
case "logs":
return app.RunLogs(ctx, cli.Logs)
case "portforward":
return app.RunPortforward(ctx, cli.Portforward)
case "stop":
return app.RunStop(ctx, cli.Stop)
case "trace":
return app.RunTrace(ctx, cli.Trace)
case "cp":
return app.RunCp(ctx, cli.CP)
case "version":
fmt.Printf("ecsta %s\n", Version)
return nil
}
return fmt.Errorf("unknown command: %s", command)
}