-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
270 lines (254 loc) · 6.77 KB
/
cli.go
File metadata and controls
270 lines (254 loc) · 6.77 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package batcha
import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/spf13/cobra"
)
// CLI builds and returns the root cobra command.
func CLI() *cobra.Command {
root := &cobra.Command{
Use: "batcha",
Short: "Declarative AWS Batch Job Definition deployment tool",
}
root.AddCommand(
initCmd(),
registerCmd(),
renderCmd(),
diffCmd(),
statusCmd(),
runCmd(),
logsCmd(),
verifyCmd(),
versionCmd(),
)
return root
}
func initCmd() *cobra.Command {
var (
jobDefName string
region string
outputDir string
)
cmd := &cobra.Command{
Use: "init",
Short: "Generate config and job definition from an existing AWS Batch definition",
RunE: func(cmd *cobra.Command, args []string) error {
return Init(cmd.Context(), InitOption{
JobDefinitionName: jobDefName,
Region: region,
OutputDir: outputDir,
})
},
}
cmd.Flags().StringVar(&jobDefName, "job-definition-name", "", "Name of the AWS Batch job definition to fetch")
cmd.Flags().StringVar(®ion, "region", "", "AWS region (falls back to AWS_REGION)")
cmd.Flags().StringVar(&outputDir, "output", ".", "Output directory for generated files")
_ = cmd.MarkFlagRequired("job-definition-name")
return cmd
}
func registerCmd() *cobra.Command {
var (
configPath string
dryRun bool
)
cmd := &cobra.Command{
Use: "register",
Short: "Register an AWS Batch Job Definition",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
return app.Register(ctx, RegisterOption{DryRun: dryRun})
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Render template and print JSON without registering")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func renderCmd() *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "render",
Short: "Render and print the job definition template",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
return app.Render(ctx)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func diffCmd() *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "diff",
Short: "Show differences between local and remote job definition",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
return app.Diff(ctx)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func statusCmd() *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "status",
Short: "Show the current status of the job definition on AWS",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
return app.Status(ctx)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func runCmd() *cobra.Command {
var (
configPath string
jobQueue string
jobName string
params []string
wait bool
)
cmd := &cobra.Command{
Use: "run",
Short: "Submit a job using the latest active job definition",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
paramMap := make(map[string]string)
for _, p := range params {
k, v, ok := strings.Cut(p, "=")
if !ok {
return fmt.Errorf("invalid parameter format %q, expected key=value", p)
}
paramMap[k] = v
}
return app.Run(ctx, RunOption{
JobQueue: jobQueue,
JobName: jobName,
Parameters: paramMap,
Wait: wait,
})
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
cmd.Flags().StringVar(&jobQueue, "job-queue", "", "AWS Batch job queue name (overrides config)")
cmd.Flags().StringVar(&jobName, "job-name", "", "Job name (defaults to job definition name)")
cmd.Flags().StringArrayVar(¶ms, "parameter", nil, "Parameter overrides (key=value, repeatable)")
cmd.Flags().BoolVar(&wait, "wait", false, "Wait for the job to complete")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func logsCmd() *cobra.Command {
var (
configPath string
jobID string
jobQueue string
follow bool
since string
)
cmd := &cobra.Command{
Use: "logs",
Short: "Fetch CloudWatch logs for a Batch job",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
var sinceDur time.Duration
if since != "" {
sinceDur, err = time.ParseDuration(since)
if err != nil {
return fmt.Errorf("invalid --since duration: %w", err)
}
}
return app.Logs(ctx, LogsOption{
JobID: jobID,
JobQueue: jobQueue,
Follow: follow,
Since: sinceDur,
})
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
cmd.Flags().StringVar(&jobID, "job-id", "", "AWS Batch job ID (if omitted, finds the latest job)")
cmd.Flags().StringVar(&jobQueue, "job-queue", "", "AWS Batch job queue name (overrides config)")
cmd.Flags().BoolVarP(&follow, "follow", "f", false, "Follow logs in real time")
cmd.Flags().StringVar(&since, "since", "", "Show logs since duration (e.g. 1h, 30m)")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func verifyCmd() *cobra.Command {
var configPath string
cmd := &cobra.Command{
Use: "verify",
Short: "Validate the job definition template locally",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
app, err := New(ctx, configPath)
if err != nil {
return err
}
return app.Verify(ctx)
},
}
cmd.Flags().StringVar(&configPath, "config", "", "Path to config YAML file")
_ = cmd.MarkFlagRequired("config")
return cmd
}
func versionCmd() *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "Print version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("batcha %s\n", Version)
},
}
}
// Run executes the CLI with signal handling.
func Run() int {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
cmd := CLI()
cmd.SetContext(ctx)
cmd.SilenceUsage = true
cmd.SilenceErrors = true
if err := cmd.ExecuteContext(ctx); err != nil {
if _, ok := err.(*DiffError); ok {
return 1
}
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return 1
}
return 0
}