Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generated time flag #501

Merged
merged 3 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
parseVendorFlag = "parseVendor"
parseDependencyFlag = "parseDependency"
markdownFilesFlag = "markdownFiles"
generatedTimeFlag = "generatedTime"
)

var initFlags = []cli.Flag{
Expand Down Expand Up @@ -54,6 +55,10 @@ var initFlags = []cli.Flag{
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",
},
}

func initAction(c *cli.Context) error {
Expand All @@ -73,6 +78,7 @@ func initAction(c *cli.Context) error {
ParseVendor: c.Bool(parseVendorFlag),
ParseDependency: c.Bool(parseDependencyFlag),
MarkdownFilesDir: c.String(markdownFilesFlag),
GeneratedTime: c.BoolT(generatedTimeFlag),
})
}

Expand Down
45 changes: 25 additions & 20 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -111,7 +114,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 Down Expand Up @@ -142,7 +145,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 @@ -195,23 +198,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 @@ -225,8 +230,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 }}
easonlin404 marked this conversation as resolved.
Show resolved Hide resolved

package docs

Expand Down
25 changes: 22 additions & 3 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,14 @@ fmt.Print("Helo world")
assert.NotEqual(t, []byte(src2), res, "Should return fmt code")
}

type mocWriter struct{}
type mocWriter struct {
hook func([]byte)
}

func (w *mocWriter) Write(data []byte) (int, error) {
if w.hook != nil {
w.hook(data)
}
return len(data), nil
}

Expand All @@ -274,7 +279,7 @@ func TestGen_writeGoDoc(t *testing.T) {
swapTemplate := packageTemplate

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

packageTemplate = `{{.Data}}`
Expand All @@ -284,9 +289,23 @@ 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 = `{{ if .GeneratedTime }}Fake Time{{ end }}`
err = gen.writeGoDoc(&mocWriter{
hook: func(data []byte) {
assert.Equal(t, "Fake Time", string(data))
},
}, swagger, &Config{GeneratedTime: true})
assert.NoError(t, err)
err = gen.writeGoDoc(&mocWriter{
hook: func(data []byte) {
assert.Equal(t, "", string(data))
},
}, swagger, &Config{GeneratedTime: false})
assert.NoError(t, err)

packageTemplate = swapTemplate

}
Expand Down