main.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // +build codegen
  2. // Command aws-gen-gocli parses a JSON description of an AWS API and generates a
  3. // Go file containing a client for the API.
  4. //
  5. // aws-gen-gocli apis/s3/2006-03-03/api-2.json
  6. package main
  7. import (
  8. "flag"
  9. "fmt"
  10. "io/ioutil"
  11. "os"
  12. "path/filepath"
  13. "runtime/debug"
  14. "sort"
  15. "strings"
  16. "sync"
  17. "github.com/aws/aws-sdk-go/private/model/api"
  18. "github.com/aws/aws-sdk-go/private/util"
  19. )
  20. type generateInfo struct {
  21. *api.API
  22. PackageDir string
  23. }
  24. var excludeServices = map[string]struct{}{
  25. "importexport": {},
  26. }
  27. // newGenerateInfo initializes the service API's folder structure for a specific service.
  28. // If the SERVICES environment variable is set, and this service is not apart of the list
  29. // this service will be skipped.
  30. func newGenerateInfo(modelFile, svcPath, svcImportPath string) *generateInfo {
  31. g := &generateInfo{API: &api.API{SvcClientImportPath: svcImportPath, BaseCrosslinkURL: "https://docs.aws.amazon.com"}}
  32. g.API.Attach(modelFile)
  33. if _, ok := excludeServices[g.API.PackageName()]; ok {
  34. return nil
  35. }
  36. paginatorsFile := strings.Replace(modelFile, "api-2.json", "paginators-1.json", -1)
  37. if _, err := os.Stat(paginatorsFile); err == nil {
  38. g.API.AttachPaginators(paginatorsFile)
  39. } else if !os.IsNotExist(err) {
  40. fmt.Println("api-2.json error:", err)
  41. }
  42. docsFile := strings.Replace(modelFile, "api-2.json", "docs-2.json", -1)
  43. if _, err := os.Stat(docsFile); err == nil {
  44. g.API.AttachDocs(docsFile)
  45. } else {
  46. fmt.Println("docs-2.json error:", err)
  47. }
  48. waitersFile := strings.Replace(modelFile, "api-2.json", "waiters-2.json", -1)
  49. if _, err := os.Stat(waitersFile); err == nil {
  50. g.API.AttachWaiters(waitersFile)
  51. } else if !os.IsNotExist(err) {
  52. fmt.Println("waiters-2.json error:", err)
  53. }
  54. examplesFile := strings.Replace(modelFile, "api-2.json", "examples-1.json", -1)
  55. if _, err := os.Stat(examplesFile); err == nil {
  56. g.API.AttachExamples(examplesFile)
  57. } else if !os.IsNotExist(err) {
  58. fmt.Println("examples-1.json error:", err)
  59. }
  60. // pkgDocAddonsFile := strings.Replace(modelFile, "api-2.json", "go-pkg-doc.gotmpl", -1)
  61. // if _, err := os.Stat(pkgDocAddonsFile); err == nil {
  62. // g.API.AttachPackageDocAddons(pkgDocAddonsFile)
  63. // } else if !os.IsNotExist(err) {
  64. // fmt.Println("go-pkg-doc.gotmpl error:", err)
  65. // }
  66. g.API.Setup()
  67. if svc := os.Getenv("SERVICES"); svc != "" {
  68. svcs := strings.Split(svc, ",")
  69. included := false
  70. for _, s := range svcs {
  71. if s == g.API.PackageName() {
  72. included = true
  73. break
  74. }
  75. }
  76. if !included {
  77. // skip this non-included service
  78. return nil
  79. }
  80. }
  81. // ensure the directory exists
  82. pkgDir := filepath.Join(svcPath, g.API.PackageName())
  83. os.MkdirAll(pkgDir, 0775)
  84. os.MkdirAll(filepath.Join(pkgDir, g.API.InterfacePackageName()), 0775)
  85. g.PackageDir = pkgDir
  86. return g
  87. }
  88. // Generates service api, examples, and interface from api json definition files.
  89. //
  90. // Flags:
  91. // -path alternative service path to write generated files to for each service.
  92. //
  93. // Env:
  94. // SERVICES comma separated list of services to generate.
  95. func main() {
  96. var svcPath, sessionPath, svcImportPath string
  97. flag.StringVar(&svcPath, "path", "service", "directory to generate service clients in")
  98. flag.StringVar(&sessionPath, "sessionPath", filepath.Join("aws", "session"), "generate session service client factories")
  99. flag.StringVar(&svcImportPath, "svc-import-path", "github.com/aws/aws-sdk-go/service", "namespace to generate service client Go code import path under")
  100. flag.Parse()
  101. api.Bootstrap()
  102. files := []string{}
  103. for i := 0; i < flag.NArg(); i++ {
  104. file := flag.Arg(i)
  105. if strings.Contains(file, "*") {
  106. paths, _ := filepath.Glob(file)
  107. files = append(files, paths...)
  108. } else {
  109. files = append(files, file)
  110. }
  111. }
  112. for svcName := range excludeServices {
  113. if strings.Contains(os.Getenv("SERVICES"), svcName) {
  114. fmt.Printf("Service %s is not supported\n", svcName)
  115. os.Exit(1)
  116. }
  117. }
  118. sort.Strings(files)
  119. // Remove old API versions from list
  120. m := map[string]bool{}
  121. for i := range files {
  122. idx := len(files) - 1 - i
  123. parts := strings.Split(files[idx], string(filepath.Separator))
  124. svc := parts[len(parts)-3] // service name is 2nd-to-last component
  125. if m[svc] {
  126. files[idx] = "" // wipe this one out if we already saw the service
  127. }
  128. m[svc] = true
  129. }
  130. wg := sync.WaitGroup{}
  131. for i := range files {
  132. filename := files[i]
  133. if filename == "" { // empty file
  134. continue
  135. }
  136. genInfo := newGenerateInfo(filename, svcPath, svcImportPath)
  137. if genInfo == nil {
  138. continue
  139. }
  140. if _, ok := excludeServices[genInfo.API.PackageName()]; ok {
  141. // Skip services not yet supported.
  142. continue
  143. }
  144. wg.Add(1)
  145. go func(g *generateInfo, filename string) {
  146. defer wg.Done()
  147. writeServiceFiles(g, filename)
  148. }(genInfo, filename)
  149. }
  150. wg.Wait()
  151. }
  152. func writeServiceFiles(g *generateInfo, filename string) {
  153. defer func() {
  154. if r := recover(); r != nil {
  155. fmt.Fprintf(os.Stderr, "Error generating %s\n%s\n%s\n",
  156. filename, r, debug.Stack())
  157. }
  158. }()
  159. fmt.Printf("Generating %s (%s)...\n",
  160. g.API.PackageName(), g.API.Metadata.APIVersion)
  161. // write files for service client and API
  162. Must(writeServiceDocFile(g))
  163. Must(writeAPIFile(g))
  164. Must(writeServiceFile(g))
  165. Must(writeInterfaceFile(g))
  166. Must(writeWaitersFile(g))
  167. Must(writeAPIErrorsFile(g))
  168. Must(writeExamplesFile(g))
  169. }
  170. // Must will panic if the error passed in is not nil.
  171. func Must(err error) {
  172. if err != nil {
  173. panic(err)
  174. }
  175. }
  176. const codeLayout = `// Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.
  177. %s
  178. package %s
  179. %s
  180. `
  181. func writeGoFile(file string, layout string, args ...interface{}) error {
  182. return ioutil.WriteFile(file, []byte(util.GoFmt(fmt.Sprintf(layout, args...))), 0664)
  183. }
  184. // writeServiceDocFile generates the documentation for service package.
  185. func writeServiceDocFile(g *generateInfo) error {
  186. return writeGoFile(filepath.Join(g.PackageDir, "doc.go"),
  187. codeLayout,
  188. strings.TrimSpace(g.API.ServicePackageDoc()),
  189. g.API.PackageName(),
  190. "",
  191. )
  192. }
  193. // writeExamplesFile writes out the service example file.
  194. func writeExamplesFile(g *generateInfo) error {
  195. code := g.API.ExamplesGoCode()
  196. if len(code) > 0 {
  197. return writeGoFile(filepath.Join(g.PackageDir, "examples_test.go"),
  198. codeLayout,
  199. "",
  200. g.API.PackageName()+"_test",
  201. code,
  202. )
  203. }
  204. return nil
  205. }
  206. // writeServiceFile writes out the service initialization file.
  207. func writeServiceFile(g *generateInfo) error {
  208. return writeGoFile(filepath.Join(g.PackageDir, "service.go"),
  209. codeLayout,
  210. "",
  211. g.API.PackageName(),
  212. g.API.ServiceGoCode(),
  213. )
  214. }
  215. // writeInterfaceFile writes out the service interface file.
  216. func writeInterfaceFile(g *generateInfo) error {
  217. const pkgDoc = `
  218. // Package %s provides an interface to enable mocking the %s service client
  219. // for testing your code.
  220. //
  221. // It is important to note that this interface will have breaking changes
  222. // when the service model is updated and adds new API operations, paginators,
  223. // and waiters.`
  224. return writeGoFile(filepath.Join(g.PackageDir, g.API.InterfacePackageName(), "interface.go"),
  225. codeLayout,
  226. fmt.Sprintf(pkgDoc, g.API.InterfacePackageName(), g.API.Metadata.ServiceFullName),
  227. g.API.InterfacePackageName(),
  228. g.API.InterfaceGoCode(),
  229. )
  230. }
  231. func writeWaitersFile(g *generateInfo) error {
  232. if len(g.API.Waiters) == 0 {
  233. return nil
  234. }
  235. return writeGoFile(filepath.Join(g.PackageDir, "waiters.go"),
  236. codeLayout,
  237. "",
  238. g.API.PackageName(),
  239. g.API.WaitersGoCode(),
  240. )
  241. }
  242. // writeAPIFile writes out the service API file.
  243. func writeAPIFile(g *generateInfo) error {
  244. return writeGoFile(filepath.Join(g.PackageDir, "api.go"),
  245. codeLayout,
  246. "",
  247. g.API.PackageName(),
  248. g.API.APIGoCode(),
  249. )
  250. }
  251. // writeAPIErrorsFile writes out the service API errors file.
  252. func writeAPIErrorsFile(g *generateInfo) error {
  253. return writeGoFile(filepath.Join(g.PackageDir, "errors.go"),
  254. codeLayout,
  255. "",
  256. g.API.PackageName(),
  257. g.API.APIErrorsGoCode(),
  258. )
  259. }