1
0

Updated libraries

This commit is contained in:
konrad
2019-05-07 21:42:24 +02:00
parent 2b160b73c3
commit 3d7fd9ca20
313 changed files with 37947 additions and 6783 deletions

View File

@ -2,6 +2,7 @@ package gen
import (
"encoding/json"
"fmt"
"log"
"os"
"path"
@ -22,12 +23,38 @@ func New() *Gen {
return &Gen{}
}
// Config presents Gen configurations.
type Config struct {
// SearchDir the swag would be parse
SearchDir string
//OutputDir represents the output directory for al the generated files
OutputDir string
//MainAPIFile the Go file path in which 'swagger general API Info' is written
MainAPIFile string
//PropNamingStrategy represents property naming strategy like snakecase,camelcase,pascalcase
PropNamingStrategy string
//ParseVendor whether swag should be parse vendor folder
ParseVendor bool
}
// Build builds swagger json file for gived searchDir and mainAPIFile. Returns json
func (g *Gen) Build(searchDir, mainAPIFile, swaggerConfDir, propNamingStrategy string) error {
func (g *Gen) Build(config *Config) error {
if _, err := os.Stat(config.SearchDir); os.IsNotExist(err) {
return fmt.Errorf("dir: %s is not exist", config.SearchDir)
}
log.Println("Generate swagger docs....")
p := swag.New()
p.PropNamingStrategy = propNamingStrategy
p.ParseAPI(searchDir, mainAPIFile)
p.PropNamingStrategy = config.PropNamingStrategy
p.ParseVendor = config.ParseVendor
if err := p.ParseAPI(config.SearchDir, config.MainAPIFile); err != nil {
return err
}
swagger := p.GetSwagger()
b, err := json.MarshalIndent(swagger, "", " ")
@ -35,15 +62,14 @@ func (g *Gen) Build(searchDir, mainAPIFile, swaggerConfDir, propNamingStrategy s
return err
}
os.MkdirAll(path.Join(searchDir, "docs"), os.ModePerm)
docs, err := os.Create(path.Join(searchDir, "docs", "docs.go"))
os.MkdirAll(config.OutputDir, os.ModePerm)
docs, err := os.Create(path.Join(config.OutputDir, "docs.go"))
if err != nil {
return err
}
defer docs.Close()
os.Mkdir(swaggerConfDir, os.ModePerm)
swaggerJSON, err := os.Create(path.Join(swaggerConfDir, "swagger.json"))
swaggerJSON, err := os.Create(path.Join(config.OutputDir, "swagger.json"))
if err != nil {
return err
}
@ -51,7 +77,7 @@ func (g *Gen) Build(searchDir, mainAPIFile, swaggerConfDir, propNamingStrategy s
defer swaggerJSON.Close()
swaggerJSON.Write(b)
swaggerYAML, err := os.Create(path.Join(swaggerConfDir, "swagger.yaml"))
swaggerYAML, err := os.Create(path.Join(config.OutputDir, "swagger.yaml"))
if err != nil {
return err
}
@ -75,6 +101,9 @@ func (g *Gen) Build(searchDir, mainAPIFile, swaggerConfDir, propNamingStrategy s
}
log.Printf("create docs.go at %+v", docs.Name())
log.Printf("create swagger.json at %+v", swaggerJSON.Name())
log.Printf("create swagger.yaml at %+v", swaggerYAML.Name())
return nil
}