main.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Command aws-gen-gocli parses a JSON description of an AWS API and generates a
  2. // Go file containing a client for the API.
  3. //
  4. // aws-gen-gocli apis/s3/2006-03-03/api-2.json
  5. package main
  6. import (
  7. "flag"
  8. "fmt"
  9. "io/ioutil"
  10. "os"
  11. "path/filepath"
  12. "runtime/debug"
  13. "sort"
  14. "strings"
  15. "sync"
  16. "github.com/aws/aws-sdk-go/internal/model/api"
  17. "github.com/aws/aws-sdk-go/internal/util"
  18. )
  19. type generateInfo struct {
  20. *api.API
  21. PackageDir string
  22. }
  23. var excludeServices = map[string]struct{}{
  24. "simpledb": {},
  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 string) *generateInfo {
  31. g := &generateInfo{API: &api.API{}}
  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. }
  40. docsFile := strings.Replace(modelFile, "api-2.json", "docs-2.json", -1)
  41. if _, err := os.Stat(docsFile); err == nil {
  42. g.API.AttachDocs(docsFile)
  43. }
  44. g.API.Setup()
  45. if svc := os.Getenv("SERVICES"); svc != "" {
  46. svcs := strings.Split(svc, ",")
  47. included := false
  48. for _, s := range svcs {
  49. if s == g.API.PackageName() {
  50. included = true
  51. break
  52. }
  53. }
  54. if !included {
  55. // skip this non-included service
  56. return nil
  57. }
  58. }
  59. // ensure the directory exists
  60. pkgDir := filepath.Join(svcPath, g.API.PackageName())
  61. os.MkdirAll(pkgDir, 0775)
  62. os.MkdirAll(filepath.Join(pkgDir, g.API.InterfacePackageName()), 0775)
  63. g.PackageDir = pkgDir
  64. return g
  65. }
  66. // Generates service api, examples, and interface from api json definition files.
  67. //
  68. // Flags:
  69. // -path alternative service path to write generated files to for each service.
  70. //
  71. // Env:
  72. // SERVICES comma separated list of services to generate.
  73. func main() {
  74. var svcPath string
  75. flag.StringVar(&svcPath, "path", "service", "generate in a specific directory (default: 'service')")
  76. flag.Parse()
  77. files := []string{}
  78. for i := 0; i < flag.NArg(); i++ {
  79. file := flag.Arg(i)
  80. if strings.Contains(file, "*") {
  81. paths, _ := filepath.Glob(file)
  82. files = append(files, paths...)
  83. } else {
  84. files = append(files, file)
  85. }
  86. }
  87. for svcName := range excludeServices {
  88. if strings.Contains(os.Getenv("SERVICES"), svcName) {
  89. fmt.Printf("Service %s is not supported\n", svcName)
  90. os.Exit(1)
  91. }
  92. }
  93. sort.Strings(files)
  94. // Remove old API versions from list
  95. m := map[string]bool{}
  96. for i := range files {
  97. idx := len(files) - 1 - i
  98. parts := strings.Split(files[idx], string(filepath.Separator))
  99. svc := parts[len(parts)-3] // service name is 2nd-to-last component
  100. if m[svc] {
  101. files[idx] = "" // wipe this one out if we already saw the service
  102. }
  103. m[svc] = true
  104. }
  105. w := sync.WaitGroup{}
  106. for i := range files {
  107. file := files[i]
  108. if file == "" { // empty file
  109. continue
  110. }
  111. w.Add(1)
  112. go func() {
  113. defer func() {
  114. w.Done()
  115. if r := recover(); r != nil {
  116. fmtStr := "Error generating %s\n%s\n%s\n"
  117. fmt.Fprintf(os.Stderr, fmtStr, file, r, debug.Stack())
  118. }
  119. }()
  120. if g := newGenerateInfo(file, svcPath); g != nil {
  121. if _, ok := excludeServices[g.API.PackageName()]; !ok {
  122. // Skip services not yet supported.
  123. fmt.Printf("Generating %s (%s)...\n",
  124. g.API.PackageName(), g.API.Metadata.APIVersion)
  125. // write api.go and service.go files
  126. g.writeAPIFile()
  127. g.writeExamplesFile()
  128. g.writeServiceFile()
  129. g.writeInterfaceFile()
  130. }
  131. }
  132. }()
  133. }
  134. w.Wait()
  135. }
  136. const codeLayout = `// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
  137. %s
  138. package %s
  139. %s
  140. `
  141. func writeGoFile(file string, layout string, args ...interface{}) error {
  142. return ioutil.WriteFile(file, []byte(util.GoFmt(fmt.Sprintf(layout, args...))), 0664)
  143. }
  144. // writeExamplesFile writes out the service example file.
  145. func (g *generateInfo) writeExamplesFile() {
  146. writeGoFile(filepath.Join(g.PackageDir, "examples_test.go"),
  147. codeLayout,
  148. "",
  149. g.API.PackageName()+"_test",
  150. g.API.ExampleGoCode(),
  151. )
  152. }
  153. // writeServiceFile writes out the service initialization file.
  154. func (g *generateInfo) writeServiceFile() {
  155. writeGoFile(filepath.Join(g.PackageDir, "service.go"),
  156. codeLayout,
  157. "",
  158. g.API.PackageName(),
  159. g.API.ServiceGoCode(),
  160. )
  161. }
  162. // writeInterfaceFile writes out the service interface file.
  163. func (g *generateInfo) writeInterfaceFile() {
  164. writeGoFile(filepath.Join(g.PackageDir, g.API.InterfacePackageName(), "interface.go"),
  165. codeLayout,
  166. fmt.Sprintf("\n// Package %s provides an interface for the %s.",
  167. g.API.InterfacePackageName(), g.API.Metadata.ServiceFullName),
  168. g.API.InterfacePackageName(),
  169. g.API.InterfaceGoCode(),
  170. )
  171. writeGoFile(filepath.Join(g.PackageDir, g.API.InterfacePackageName(), "interface_test.go"),
  172. codeLayout,
  173. "",
  174. g.API.InterfacePackageName()+"_test",
  175. g.API.InterfaceTestGoCode(),
  176. )
  177. }
  178. // writeAPIFile writes out the service api file.
  179. func (g *generateInfo) writeAPIFile() {
  180. writeGoFile(filepath.Join(g.PackageDir, "api.go"),
  181. codeLayout,
  182. fmt.Sprintf("\n// Package %s provides a client for %s.",
  183. g.API.PackageName(), g.API.Metadata.ServiceFullName),
  184. g.API.PackageName(),
  185. g.API.APIGoCode(),
  186. )
  187. }