-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathmain.go
405 lines (340 loc) · 12.3 KB
/
main.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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
//go:build generate
// +build generate
package main
import (
"bytes"
_ "embed"
"flag"
"fmt"
"go/ast"
"go/format"
"html/template"
"log"
"os"
"sort"
"strings"
"github.com/hashicorp/terraform-provider-aws/names"
"golang.org/x/tools/go/packages"
)
const (
defaultFilename = "list_pages_gen.go"
sdkV1 = 1
sdkV2 = 2
)
var (
inputPaginator = flag.String("InputPaginator", "", "name of the input pagination token field")
listOps = flag.String("ListOps", "", "ListOps")
outputPaginator = flag.String("OutputPaginator", "", "name of the output pagination token field")
paginator = flag.String("Paginator", "NextToken", "name of the pagination token field")
export = flag.Bool("Export", false, "whether to export the list functions")
sdkVersion = flag.Int("AWSSDKVersion", sdkV1, "Version of the AWS Go SDK to use i.e. 1 or 2")
v2Suffix = flag.Bool("V2Suffix", false, "whether to append a V2 suffix to the list functions")
)
func usage() {
fmt.Fprintf(os.Stderr, "Usage:\n")
fmt.Fprintf(os.Stderr, "\tmain.go [flags] [<generated-lister-file>]\n\n")
fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
log.SetPrefix("generate/listpage: ")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
if (*inputPaginator != "" && *outputPaginator == "") || (*inputPaginator == "" && *outputPaginator != "") {
log.Fatal("both InputPaginator and OutputPaginator must be specified if one is")
}
if *inputPaginator == "" {
*inputPaginator = *paginator
}
if *outputPaginator == "" {
*outputPaginator = *paginator
}
filename := defaultFilename
if args := flag.Args(); len(args) > 0 {
filename = args[0]
}
servicePackage := os.Getenv("GOPACKAGE")
log.SetPrefix(fmt.Sprintf("generate/listpage: %s: ", servicePackage))
awsService, err := names.AWSGoV1Package(servicePackage)
if err != nil {
log.Fatalf("encountered: %s", err)
}
functions := strings.Split(*listOps, ",")
sort.Strings(functions)
tmpl := template.Must(template.New("function").Parse(functionTemplateV1))
if *sdkVersion == sdkV2 {
tmpl = template.Must(template.New("function").Parse(functionTemplateV2))
}
g := Generator{
tmpl: tmpl,
inputPaginator: *inputPaginator,
outputPaginator: *outputPaginator,
}
sourcePackage := fmt.Sprintf("github.com/aws/aws-sdk-go/service/%[1]s", awsService)
if *sdkVersion == sdkV2 {
sourcePackage = fmt.Sprintf("github.com/aws/aws-sdk-go-v2/service/%[1]s", awsService)
}
g.parsePackage(sourcePackage)
g.printHeader(HeaderInfo{
Parameters: strings.Join(os.Args[1:], " "),
DestinationPackage: servicePackage,
SourcePackage: sourcePackage,
SourceIntfPackage: fmt.Sprintf("github.com/aws/aws-sdk-go/service/%[1]s/%[1]siface", awsService),
}, *sdkVersion)
awsUpper, err := names.AWSGoClientTypeName(servicePackage, *sdkVersion)
if err != nil {
log.Fatalf("encountered: %s", err)
}
for _, functionName := range functions {
g.generateFunction(functionName, awsService, awsUpper, *export, *sdkVersion, *v2Suffix)
}
src := g.format()
err = os.WriteFile(filename, src, 0644)
if err != nil {
log.Fatalf("error writing output: %s", err)
}
}
type HeaderInfo struct {
Parameters string
DestinationPackage string
SourcePackage string
SourceIntfPackage string
}
type Generator struct {
buf bytes.Buffer
pkg *Package
tmpl *template.Template
inputPaginator string
outputPaginator string
}
func (g *Generator) Printf(format string, args ...interface{}) {
fmt.Fprintf(&g.buf, format, args...)
}
type PackageFile struct {
file *ast.File
}
type Package struct {
name string
files []*PackageFile
}
func (g *Generator) printHeader(headerInfo HeaderInfo, sdkVersion int) {
header := template.Must(template.New("header").Parse(headerTemplateV1))
if sdkVersion == sdkV2 {
header = template.Must(template.New("header").Parse(headerTemplateV2))
}
err := header.Execute(&g.buf, headerInfo)
if err != nil {
log.Fatalf("error writing header: %s", err)
}
}
func (g *Generator) parsePackage(sourcePackage string) {
cfg := &packages.Config{
Mode: packages.NeedName | packages.NeedTypes | packages.NeedSyntax,
}
pkgs, err := packages.Load(cfg, sourcePackage)
if err != nil {
log.Fatal(err)
}
if len(pkgs) != 1 {
log.Fatalf("error: %d packages found", len(pkgs))
}
g.addPackage(pkgs[0])
}
func (g *Generator) addPackage(pkg *packages.Package) {
g.pkg = &Package{
name: pkg.Name,
files: make([]*PackageFile, len(pkg.Syntax)),
}
for i, file := range pkg.Syntax {
g.pkg.files[i] = &PackageFile{
file: file,
}
}
}
type FuncSpec struct {
Name string
AWSName string
RecvType string
ParamType string
ResultType string
InputPaginator string
OutputPaginator string
V2Suffix bool
}
func (g *Generator) generateFunction(functionName, awsService, awsServiceUpper string, export bool, sdkVersion int, v2Suffix bool) {
var function *ast.FuncDecl
for _, file := range g.pkg.files {
if file.file != nil {
for _, decl := range file.file.Decls {
if funcDecl, ok := decl.(*ast.FuncDecl); ok {
if funcDecl.Name.Name == functionName {
function = funcDecl
break
}
}
}
if function != nil {
break
}
}
}
if function == nil {
log.Fatalf("function \"%s\" not found", functionName)
}
funcName := function.Name.Name
if !export {
funcName = fmt.Sprintf("%s%s", strings.ToLower(funcName[0:1]), funcName[1:])
}
recvType := fmt.Sprintf("%[1]siface.%[2]sAPI", awsService, awsServiceUpper)
if sdkVersion == sdkV2 {
recvType = fmt.Sprintf("*%[1]s.%[2]s", awsService, awsServiceUpper)
}
funcSpec := FuncSpec{
Name: fixUpFuncName(funcName, awsServiceUpper),
AWSName: function.Name.Name,
RecvType: recvType,
ParamType: g.expandTypeField(function.Type.Params, sdkVersion, false), // Assumes there is a single input parameter
ResultType: g.expandTypeField(function.Type.Results, sdkVersion, true), // Assumes we can take the first return parameter
InputPaginator: g.inputPaginator,
OutputPaginator: g.outputPaginator,
V2Suffix: v2Suffix,
}
err := g.tmpl.Execute(&g.buf, funcSpec)
if err != nil {
log.Fatalf("error writing function \"%s\": %s", functionName, err)
}
}
func (g *Generator) expandTypeField(field *ast.FieldList, sdkVersion int, result bool) string {
typeValue := field.List[0].Type
if sdkVersion == sdkV2 && !result {
typeValue = field.List[1].Type
}
if star, ok := typeValue.(*ast.StarExpr); ok {
return fmt.Sprintf("*%s", g.expandTypeExpr(star.X))
}
log.Fatalf("Unexpected type expression: (%[1]T) %[1]v", typeValue)
return ""
}
func (g *Generator) expandTypeExpr(expr ast.Expr) string {
if ident, ok := expr.(*ast.Ident); ok {
return fmt.Sprintf("%s.%s", g.pkg.name, ident.Name)
}
log.Fatalf("Unexpected expression: (%[1]T) %[1]v", expr)
return ""
}
func fixUpFuncName(funcName, service string) string {
return strings.ReplaceAll(fixSomeInitialisms(funcName), service, "")
}
//go:embed v1/header.tmpl
var headerTemplateV1 string
//go:embed v1/function.tmpl
var functionTemplateV1 string
//go:embed v2/header.tmpl
var headerTemplateV2 string
//go:embed v2/function.tmpl
var functionTemplateV2 string
func (g *Generator) format() []byte {
src, err := format.Source(g.buf.Bytes())
if err != nil {
log.Printf("warning: internal error: invalid Go generated: %s", err)
log.Printf("warning: compile the package to analyze the error")
return g.buf.Bytes()
}
return src
}
func fixSomeInitialisms(s string) string {
replace := s
replace = strings.Replace(replace, "ResourceSes", "ResourceSES", 1)
replace = strings.Replace(replace, "ApiGateway", "APIGateway", 1)
replace = strings.Replace(replace, "Cloudwatch", "CloudWatch", 1)
replace = strings.Replace(replace, "CurReport", "CURReport", 1)
replace = strings.Replace(replace, "CloudHsm", "CloudHSM", 1)
replace = strings.Replace(replace, "DynamoDb", "DynamoDB", 1)
replace = strings.Replace(replace, "Opsworks", "OpsWorks", 1)
replace = strings.Replace(replace, "Precheck", "PreCheck", 1)
replace = strings.Replace(replace, "Graphql", "GraphQL", 1)
replace = strings.Replace(replace, "Haproxy", "HAProxy", 1)
replace = strings.Replace(replace, "Acmpca", "ACMPCA", 1)
replace = strings.Replace(replace, "AcmPca", "ACMPCA", 1)
replace = strings.Replace(replace, "Dnssec", "DNSSEC", 1)
replace = strings.Replace(replace, "DocDb", "DocDB", 1)
replace = strings.Replace(replace, "Docdb", "DocDB", 1)
replace = strings.Replace(replace, "Https", "HTTPS", 1)
replace = strings.Replace(replace, "Ipset", "IPSet", 1)
replace = strings.Replace(replace, "Iscsi", "iSCSI", 1)
replace = strings.Replace(replace, "Mysql", "MySQL", 1)
replace = strings.Replace(replace, "Wafv2", "WAFV2", 1)
replace = strings.Replace(replace, "Cidr", "CIDR", 1)
replace = strings.Replace(replace, "Coip", "CoIP", 1)
replace = strings.Replace(replace, "Dhcp", "DHCP", 1)
replace = strings.Replace(replace, "Dkim", "DKIM", 1)
replace = strings.Replace(replace, "Grpc", "GRPC", 1)
replace = strings.Replace(replace, "Http", "HTTP", 1)
replace = strings.Replace(replace, "Mwaa", "MWAA", 1)
replace = strings.Replace(replace, "Oidc", "OIDC", 1)
replace = strings.Replace(replace, "Qldb", "QLDB", 1)
replace = strings.Replace(replace, "Smtp", "SMTP", 1)
replace = strings.Replace(replace, "Xray", "XRay", 1)
replace = strings.Replace(replace, "Acl", "ACL", 1)
replace = strings.Replace(replace, "Acm", "ACM", 1)
replace = strings.Replace(replace, "Ami", "AMI", 1)
replace = strings.Replace(replace, "Api", "API", 1)
replace = strings.Replace(replace, "Arn", "ARN", 1)
replace = strings.Replace(replace, "Bgp", "BGP", 1)
replace = strings.Replace(replace, "Csv", "CSV", 1)
replace = strings.Replace(replace, "Dax", "DAX", 1)
replace = strings.Replace(replace, "Dlm", "DLM", 1)
replace = strings.Replace(replace, "Dms", "DMS", 1)
replace = strings.Replace(replace, "Dns", "DNS", 1)
replace = strings.Replace(replace, "Ebs", "EBS", 1)
replace = strings.Replace(replace, "Ec2", "EC2", 1)
replace = strings.Replace(replace, "Ecr", "ECR", 1)
replace = strings.Replace(replace, "Ecs", "ECS", 1)
replace = strings.Replace(replace, "Efs", "EFS", 1)
replace = strings.Replace(replace, "Eip", "EIP", 1)
replace = strings.Replace(replace, "Eks", "EKS", 1)
replace = strings.Replace(replace, "Elb", "ELB", 1)
replace = strings.Replace(replace, "Emr", "EMR", 1)
replace = strings.Replace(replace, "Fms", "FMS", 1)
replace = strings.Replace(replace, "Fsx", "FSx", 1)
replace = strings.Replace(replace, "Hsm", "HSM", 1)
replace = strings.Replace(replace, "Iam", "IAM", 1)
replace = strings.Replace(replace, "Iot", "IoT", 1)
replace = strings.Replace(replace, "Kms", "KMS", 1)
replace = strings.Replace(replace, "Msk", "MSK", 1)
replace = strings.Replace(replace, "Nat", "NAT", 1)
replace = strings.Replace(replace, "Nfs", "NFS", 1)
replace = strings.Replace(replace, "Php", "PHP", 1)
replace = strings.Replace(replace, "Ram", "RAM", 1)
replace = strings.Replace(replace, "Rds", "RDS", 1)
replace = strings.Replace(replace, "Rfc", "RFC", 1)
replace = strings.Replace(replace, "Sfn", "SFN", 1)
replace = strings.Replace(replace, "Smb", "SMB", 1)
replace = strings.Replace(replace, "Sms", "SMS", 1)
replace = strings.Replace(replace, "Sns", "SNS", 1)
replace = strings.Replace(replace, "Sql", "SQL", 1)
replace = strings.Replace(replace, "Sqs", "SQS", 1)
replace = strings.Replace(replace, "Ssh", "SSH", 1)
replace = strings.Replace(replace, "Ssm", "SSM", 1)
replace = strings.Replace(replace, "Sso", "SSO", 1)
replace = strings.Replace(replace, "Sts", "STS", 1)
replace = strings.Replace(replace, "Swf", "SWF", 1)
replace = strings.Replace(replace, "Tcp", "TCP", 1)
replace = strings.Replace(replace, "Vpc", "VPC", 1)
replace = strings.Replace(replace, "Vpn", "VPN", 1)
replace = strings.Replace(replace, "Waf", "WAF", 1)
replace = strings.Replace(replace, "Xss", "XSS", 1)
replace = strings.Replace(replace, "Db", "DB", 1)
replace = strings.Replace(replace, "Ip", "IP", 1)
replace = strings.Replace(replace, "Mq", "MQ", 1)
if replace != strings.TrimSuffix(replace, "Ids") {
replace = fmt.Sprintf("%s%s", strings.TrimSuffix(replace, "Ids"), "IDs")
}
if replace != strings.TrimSuffix(replace, "Id") {
replace = fmt.Sprintf("%s%s", strings.TrimSuffix(replace, "Id"), "ID")
}
return replace
}