Skip to content

Commit

Permalink
feat: add packageName option for docs.go (#1442)
Browse files Browse the repository at this point in the history
* feat: replace hyphen from docs package name

* feat: add packageName option

* Update main.go

---------

Co-authored-by: sdghchj <sdghchj@qq.com>
  • Loading branch information
hwwwi and sdghchj committed Mar 21, 2023
1 parent aaa681e commit f475da2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 7 additions & 0 deletions cmd/swag/main.go
Expand Up @@ -35,6 +35,7 @@ const (
quietFlag = "quiet"
tagsFlag = "tags"
parseExtensionFlag = "parseExtension"
packageName = "packageName"
collectionFormatFlag = "collectionFormat"
)

Expand Down Expand Up @@ -142,6 +143,11 @@ var initFlags = []cli.Flag{
Value: "",
Usage: "A comma-separated list of tags to filter the APIs for which the documentation is generated.Special case if the tag is prefixed with the '!' character then the APIs with that tag will be excluded",
},
&cli.StringFlag{
Name: packageName,
Value: "",
Usage: "A package name of docs.go, using output directory name by default (check `--output` option)",
},
&cli.StringFlag{
Name: collectionFormatFlag,
Aliases: []string{"cf"},
Expand Down Expand Up @@ -193,6 +199,7 @@ func initAction(ctx *cli.Context) error {
OverridesFile: ctx.String(overridesFileFlag),
ParseGoList: ctx.Bool(parseGoListFlag),
Tags: ctx.String(tagsFlag),
PackageName: ctx.String(packageName),
Debugger: logger,
CollectionFormat: collectionFormat,
})
Expand Down
11 changes: 10 additions & 1 deletion gen/gen.go
Expand Up @@ -127,6 +127,9 @@ type Config struct {
// include only tags mentioned when searching, comma separated
Tags string

// PackageName defines package name of generated `docs.go`
PackageName string

// CollectionFormat set default collection format
CollectionFormat string
}
Expand Down Expand Up @@ -225,7 +228,13 @@ func (g *Gen) writeDocSwagger(config *Config, swagger *spec.Swagger) error {
return err
}

packageName := filepath.Base(absOutputDir)
var packageName string
if len(config.PackageName) > 0 {
packageName = config.PackageName
} else {
packageName = filepath.Base(absOutputDir)
packageName = strings.ReplaceAll(packageName, "-", "_")
}

docs, err := os.Create(docFileName)
if err != nil {
Expand Down

0 comments on commit f475da2

Please sign in to comment.