From d85392472b48845eed9a71697f5a3476d88214df Mon Sep 17 00:00:00 2001 From: jixiufeng Date: Tue, 17 May 2022 19:05:53 +0800 Subject: [PATCH] add --quiet=true for swag init, make the debug logger quiet. --- cmd/swag/main.go | 12 ++++++++++++ gen/gen.go | 3 +++ gen/gen_test.go | 29 +++++++++++++++++++++++++++++ parser.go | 5 ++++- 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cmd/swag/main.go b/cmd/swag/main.go index dde1e0b8c..0e2b1b20d 100644 --- a/cmd/swag/main.go +++ b/cmd/swag/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io/ioutil" "log" "os" "strings" @@ -30,9 +31,15 @@ const ( instanceNameFlag = "instanceName" overridesFileFlag = "overridesFile" parseGoListFlag = "parseGoList" + quietFlag = "quiet" ) var initFlags = []cli.Flag{ + &cli.BoolFlag{ + Name: quietFlag, + Aliases: []string{q}, + Usage: "Make the logger quiet.", + }, &cli.StringFlag{ Name: generalInfoFlag, Aliases: []string{"g"}, @@ -131,6 +138,10 @@ func initAction(ctx *cli.Context) error { if len(outputTypes) == 0 { return fmt.Errorf("no output types specified") } + var logger swag.Debugger + if ctx.Bool(quietFlag) { + logger = log.New(ioutil.Discard, "", log.LstdFlags) + } return gen.New().Build(&gen.Config{ SearchDir: ctx.String(searchDirFlag), @@ -149,6 +160,7 @@ func initAction(ctx *cli.Context) error { InstanceName: ctx.String(instanceNameFlag), OverridesFile: ctx.String(overridesFileFlag), ParseGoList: ctx.Bool(parseGoListFlag), + Debugger: logger, }) } diff --git a/gen/gen.go b/gen/gen.go index 7198433f6..ecdb9373d 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -57,6 +57,8 @@ func New() *Gen { // Config presents Gen configurations. type Config struct { + Debugger swag.Debugger + // SearchDir the swag would parse,comma separated if multiple SearchDir string @@ -145,6 +147,7 @@ func (g *Gen) Build(config *Config) error { log.Println("Generate swagger docs....") p := swag.New(swag.SetMarkdownFileDirectory(config.MarkdownFilesDir), + swag.SetDebugger(config.Debugger), swag.SetExcludedDirsAndFiles(config.Excludes), swag.SetCodeExamplesDirectory(config.CodeExampleFilesDir), swag.SetStrict(config.Strict), diff --git a/gen/gen_test.go b/gen/gen_test.go index 475d8f73e..62a26c2d3 100644 --- a/gen/gen_test.go +++ b/gen/gen_test.go @@ -1,10 +1,12 @@ package gen import ( + "bytes" "encoding/json" "errors" "fmt" "io/ioutil" + "log" "os" "os/exec" "path" @@ -750,3 +752,30 @@ func TestGen_TypeOverridesFile(t *testing.T) { assert.NoError(t, err) }) } +func TestGen_Debugger(t *testing.T) { + var buf bytes.Buffer + config := &Config{ + SearchDir: searchDir, + MainAPIFile: "./main.go", + OutputDir: "../testdata/simple/docs", + OutputTypes: outputTypes, + PropNamingStrategy: "", + Debugger: log.New(&buf, "", log.LstdFlags), + } + assert.True(t, buf.Len() == 0) + assert.NoError(t, New().Build(config)) + assert.True(t, buf.Len() > 0) + + expectedFiles := []string{ + filepath.Join(config.OutputDir, "docs.go"), + filepath.Join(config.OutputDir, "swagger.json"), + filepath.Join(config.OutputDir, "swagger.yaml"), + } + for _, expectedFile := range expectedFiles { + if _, err := os.Stat(expectedFile); os.IsNotExist(err) { + require.NoError(t, err) + } + + _ = os.Remove(expectedFile) + } +} diff --git a/parser.go b/parser.go index c8a18a6db..f489ea37d 100644 --- a/parser.go +++ b/parser.go @@ -245,7 +245,10 @@ func SetStrict(strict bool) func(*Parser) { // SetDebugger allows the use of user-defined implementations. func SetDebugger(logger Debugger) func(parser *Parser) { return func(p *Parser) { - p.debug = logger + if logger != nil { + p.debug = logger + } + } }