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

feat: add packageName option for docs.go #1442

Merged
merged 4 commits into from
Mar 21, 2023
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
7 changes: 7 additions & 0 deletions cmd/swag/main.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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