Skip to content

Commit

Permalink
add --quiet=true for swag init, make the debug logger quiet.
Browse files Browse the repository at this point in the history
  • Loading branch information
jixiufeng committed May 18, 2022
1 parent 5f6b402 commit 614a68d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cmd/swag/main.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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),
Expand All @@ -149,6 +160,7 @@ func initAction(ctx *cli.Context) error {
InstanceName: ctx.String(instanceNameFlag),
OverridesFile: ctx.String(overridesFileFlag),
ParseGoList: ctx.Bool(parseGoListFlag),
Debugger: logger,
})
}

Expand Down
3 changes: 3 additions & 0 deletions gen/gen.go
Expand Up @@ -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

Expand Down Expand Up @@ -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),
Expand Down
29 changes: 29 additions & 0 deletions gen/gen_test.go
@@ -1,10 +1,12 @@
package gen

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
Expand Down Expand Up @@ -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)
}
}
5 changes: 4 additions & 1 deletion parser.go
Expand Up @@ -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
}

}
}

Expand Down

0 comments on commit 614a68d

Please sign in to comment.