Skip to content

Commit

Permalink
feat: add generated time flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xizhibei committed Aug 29, 2019
1 parent 4f860d7 commit 189bdae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
7 changes: 7 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const outputFlag = "output"
const parseVendorFlag = "parseVendor"
const parseDependency = "parseDependency"
const markdownFilesDirFlag = "markdownFiles"
const generatedTimeFlag = "generatedTime"

func main() {
app := cli.NewApp()
Expand All @@ -35,6 +36,7 @@ func main() {
parseVendor := c.Bool(parseVendorFlag)
parseDependency := c.Bool(parseDependency)
markdownFilesDir := c.String(markdownFilesDirFlag)
generatedTime := c.Bool(generatedTimeFlag)

switch strategy {
case swag.CamelCase, swag.SnakeCase, swag.PascalCase:
Expand All @@ -50,6 +52,7 @@ func main() {
ParseVendor: parseVendor,
ParseDependency: parseDependency,
MarkdownFilesDir: markdownFilesDir,
GeneratedTime: generatedTime,
})
},
Flags: []cli.Flag{
Expand Down Expand Up @@ -86,6 +89,10 @@ func main() {
Value: "",
Usage: "Parse folder containing markdown files to use as description, disabled by default",
},
cli.BoolTFlag{
Name: "generatedTime",
Usage: "Generate timestamp at the top of docs.go, true by default",
},
},
},
}
Expand Down
45 changes: 25 additions & 20 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type Config struct {

// MarkdownFilesDir used to find markdownfiles, which can be used for tag descriptions
MarkdownFilesDir string

// GeneratedTime whether swag should generate the timestamp at the top of docs.go
GeneratedTime bool
}

// Build builds swagger json file for given searchDir and mainAPIFile. Returns json
Expand Down Expand Up @@ -109,7 +112,7 @@ func (g *Gen) Build(config *Config) error {
}

// Write doc
err = g.writeGoDoc(docs, swagger)
err = g.writeGoDoc(docs, swagger, config)
if err != nil {
return err
}
Expand All @@ -133,7 +136,7 @@ func (g *Gen) formatSource(src []byte) []byte {
return code
}

func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger) error {
func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger, config *Config) error {

generator, err := template.New("swagger_info").Funcs(template.FuncMap{
"printDoc": func(v string) string {
Expand Down Expand Up @@ -186,23 +189,25 @@ func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger) error {

buffer := &bytes.Buffer{}
err = generator.Execute(buffer, struct {
Timestamp time.Time
Doc string
Host string
BasePath string
Schemes []string
Title string
Description string
Version string
Timestamp time.Time
GeneratedTime bool
Doc string
Host string
BasePath string
Schemes []string
Title string
Description string
Version string
}{
Timestamp: time.Now(),
Doc: string(buf),
Host: swagger.Host,
BasePath: swagger.BasePath,
Schemes: swagger.Schemes,
Title: swagger.Info.Title,
Description: swagger.Info.Description,
Version: swagger.Info.Version,
Timestamp: time.Now(),
GeneratedTime: config.GeneratedTime,
Doc: string(buf),
Host: swagger.Host,
BasePath: swagger.BasePath,
Schemes: swagger.Schemes,
Title: swagger.Info.Title,
Description: swagger.Info.Description,
Version: swagger.Info.Version,
})
if err != nil {
return err
Expand All @@ -217,8 +222,8 @@ func (g *Gen) writeGoDoc(output io.Writer, swagger *spec.Swagger) error {
}

var packageTemplate = `// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
// This file was generated by swaggo/swag at
// {{ .Timestamp }}
// This file was generated by swaggo/swag{{ if .GeneratedTime }} at
// {{ .Timestamp }}{{ end }}
package docs
Expand Down
4 changes: 2 additions & 2 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestGen_writeGoDoc(t *testing.T) {
swapTemplate := packageTemplate

packageTemplate = `{{{`
err := gen.writeGoDoc(nil, nil)
err := gen.writeGoDoc(nil, nil, nil)
assert.Error(t, err)

packageTemplate = `{{.Data}}`
Expand All @@ -240,7 +240,7 @@ func TestGen_writeGoDoc(t *testing.T) {
Info: &spec.Info{},
},
}
err = gen.writeGoDoc(&mocWriter{}, swagger)
err = gen.writeGoDoc(&mocWriter{}, swagger, &Config{})
assert.Error(t, err)

packageTemplate = swapTemplate
Expand Down

0 comments on commit 189bdae

Please sign in to comment.