This repository has been archived by the owner on May 22, 2019. It is now read-only.
forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
awssdk.go
147 lines (130 loc) · 4.11 KB
/
awssdk.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
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/aws/aws-sdk-go/internal/model/api"
"github.com/aws/aws-sdk-go/internal/util"
"golang.org/x/tools/imports"
)
func main() {
os.Exit(_main())
}
var typesOnlyTplAPI = template.Must(template.New("api").Parse(`
{{ range $_, $s := .ShapeList }}
{{ if eq $s.Type "structure"}}{{ $s.GoCode }}{{ end }}
{{ end }}
`))
const copyrightHeader = `// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
`
// return-based exit code so the 'defer' works
func _main() int {
var typesOnly bool
flag.BoolVar(&typesOnly, "typesOnly", false, "only generate types")
flag.Parse()
apiFiles, err := filepath.Glob("./api/api-2.json")
if err != nil {
fmt.Println("Error listing api json files: ", err)
return 1
}
for _, file := range apiFiles {
var err error
if typesOnly {
err = genTypesOnlyAPI(file)
} else {
err = genFull(file)
}
if err != nil {
fmt.Println(err)
return 1
}
}
return 0
}
func genTypesOnlyAPI(file string) error {
apiGen := &api.API{
NoInflections: true,
NoRemoveUnusedShapes: true,
}
apiGen.Attach(file)
apiGen.Setup()
// to reset imports so that timestamp has an entry in the map.
apiGen.APIGoCode()
var buf bytes.Buffer
err := typesOnlyTplAPI.Execute(&buf, apiGen)
if err != nil {
panic(err)
}
code := strings.TrimSpace(buf.String())
code = util.GoFmt(code)
// Ignore dir error, filepath will catch it for an invalid path.
os.Mkdir(apiGen.PackageName(), 0755)
// Fix imports.
codeWithImports, err := imports.Process("", []byte(fmt.Sprintf("package %s\n\n%s", apiGen.PackageName(), code)), nil)
if err != nil {
fmt.Println(err)
return err
}
outFile := filepath.Join(apiGen.PackageName(), "api.go")
err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\n%s", copyrightHeader, codeWithImports)), 0644)
if err != nil {
return err
}
return nil
}
func genFull(file string) error {
// Heavily inspired by https://github.com/aws/aws-sdk-go/blob/45ba2f817fbf625fe48cf9b51fae13e5dfba6171/internal/model/cli/gen-api/main.go#L36
// That code is copyright Amazon
api := &api.API{}
api.Attach(file)
paginatorsFile := strings.Replace(file, "api-2.json", "paginators-1.json", -1)
if _, err := os.Stat(paginatorsFile); err == nil {
api.AttachPaginators(paginatorsFile)
}
docsFile := strings.Replace(file, "api-2.json", "docs-2.json", -1)
if _, err := os.Stat(docsFile); err == nil {
api.AttachDocs(docsFile)
}
api.Setup()
// Ignore dir error, filepath will catch it for an invalid path.
os.Mkdir(api.PackageName(), 0755)
outFile := filepath.Join(api.PackageName(), "api.go")
err := ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\npackage %s\n\n%s", copyrightHeader, api.PackageName(), api.APIGoCode())), 0644)
if err != nil {
return err
}
outFile = filepath.Join(api.PackageName(), "service.go")
err = ioutil.WriteFile(outFile, []byte(fmt.Sprintf("%s\npackage %s\n\n%s", copyrightHeader, api.PackageName(), api.ServiceGoCode())), 0644)
if err != nil {
return err
}
return nil
}