github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/goagen/gen_swagger/generator.go (about) 1 package genswagger 2 3 import ( 4 "encoding/json" 5 "flag" 6 "fmt" 7 "io/ioutil" 8 "os" 9 "path/filepath" 10 11 "gopkg.in/yaml.v2" 12 13 "github.com/goadesign/goa/design" 14 "github.com/goadesign/goa/goagen/codegen" 15 "github.com/goadesign/goa/goagen/utils" 16 ) 17 18 //NewGenerator returns an initialized instance of a JavaScript Client Generator 19 func NewGenerator(options ...Option) *Generator { 20 g := &Generator{} 21 22 for _, option := range options { 23 option(g) 24 } 25 26 return g 27 } 28 29 // Generator is the swagger code generator. 30 type Generator struct { 31 API *design.APIDefinition // The API definition 32 OutDir string // Path to output directory 33 genfiles []string // Generated files 34 } 35 36 // Generate is the generator entry point called by the meta generator. 37 func Generate() (files []string, err error) { 38 var outDir, ver string 39 set := flag.NewFlagSet("swagger", flag.PanicOnError) 40 set.StringVar(&outDir, "out", "", "") 41 set.StringVar(&ver, "version", "", "") 42 set.String("design", "", "") 43 set.Bool("force", false, "") 44 set.Bool("notest", false, "") 45 set.Parse(os.Args[1:]) 46 47 if err := codegen.CheckVersion(ver); err != nil { 48 return nil, err 49 } 50 51 g := &Generator{OutDir: outDir, API: design.Design} 52 53 return g.Generate() 54 } 55 56 // Generate produces the skeleton main. 57 func (g *Generator) Generate() (_ []string, err error) { 58 if g.API == nil { 59 return nil, fmt.Errorf("missing API definition, make sure design is properly initialized") 60 } 61 62 go utils.Catch(nil, func() { g.Cleanup() }) 63 64 defer func() { 65 if err != nil { 66 g.Cleanup() 67 } 68 }() 69 70 s, err := New(g.API) 71 if err != nil { 72 return nil, err 73 } 74 75 swaggerDir := filepath.Join(g.OutDir, "swagger") 76 os.RemoveAll(swaggerDir) 77 if err = os.MkdirAll(swaggerDir, 0755); err != nil { 78 return nil, err 79 } 80 g.genfiles = append(g.genfiles, swaggerDir) 81 82 // JSON 83 rawJSON, err := json.Marshal(s) 84 if err != nil { 85 return nil, err 86 } 87 swaggerFile := filepath.Join(swaggerDir, "swagger.json") 88 if err := ioutil.WriteFile(swaggerFile, rawJSON, 0644); err != nil { 89 return nil, err 90 } 91 g.genfiles = append(g.genfiles, swaggerFile) 92 93 // YAML 94 var yamlSource interface{} 95 if err = json.Unmarshal(rawJSON, &yamlSource); err != nil { 96 return nil, err 97 } 98 99 rawYAML, err := yaml.Marshal(yamlSource) 100 if err != nil { 101 return nil, err 102 } 103 swaggerFile = filepath.Join(swaggerDir, "swagger.yaml") 104 if err := ioutil.WriteFile(swaggerFile, rawYAML, 0644); err != nil { 105 return nil, err 106 } 107 g.genfiles = append(g.genfiles, swaggerFile) 108 109 return g.genfiles, nil 110 } 111 112 // Cleanup removes all the files generated by this generator during the last invokation of Generate. 113 func (g *Generator) Cleanup() { 114 for _, f := range g.genfiles { 115 os.Remove(f) 116 } 117 g.genfiles = nil 118 }