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

Feature: Generated file name from instanceName #1112

Merged
merged 4 commits into from
Jan 28, 2022
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
18 changes: 15 additions & 3 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ func (g *Gen) Build(config *Config) error {
}

func (g *Gen) writeDocSwagger(config *Config, swagger *spec.Swagger) error {
docFileName := path.Join(config.OutputDir, "docs.go")
var filename = "docs.go"
if config.InstanceName != swag.Name {
filename = config.InstanceName + "_" + filename
}
docFileName := path.Join(config.OutputDir, filename)

absOutputDir, err := filepath.Abs(config.OutputDir)
if err != nil {
Expand All @@ -201,7 +205,11 @@ func (g *Gen) writeDocSwagger(config *Config, swagger *spec.Swagger) error {
}

func (g *Gen) writeJSONSwagger(config *Config, swagger *spec.Swagger) error {
jsonFileName := path.Join(config.OutputDir, "swagger.json")
var filename = "swagger.json"
if config.InstanceName != swag.Name {
filename = config.InstanceName + "_" + filename
}
jsonFileName := path.Join(config.OutputDir, filename)

b, err := g.jsonIndent(swagger)
if err != nil {
Expand All @@ -218,7 +226,11 @@ func (g *Gen) writeJSONSwagger(config *Config, swagger *spec.Swagger) error {
}

func (g *Gen) writeYAMLSwagger(config *Config, swagger *spec.Swagger) error {
yamlFileName := path.Join(config.OutputDir, "swagger.yaml")
var filename = "swagger.yaml"
if config.InstanceName != swag.Name {
filename = config.InstanceName + "_" + filename
}
yamlFileName := path.Join(config.OutputDir, filename)

b, err := g.json(swagger)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestGen_BuildInstanceName(t *testing.T) {

// Custom name
config.InstanceName = "custom"
goSourceFile = filepath.Join(config.OutputDir, config.InstanceName+"_"+"docs.go")
assert.NoError(t, New().Build(config))
expectedCode, err = ioutil.ReadFile(goSourceFile)
if err != nil {
Expand All @@ -112,9 +113,9 @@ func TestGen_BuildInstanceName(t *testing.T) {

// cleanup
expectedFiles := []string{
filepath.Join(config.OutputDir, "docs.go"),
filepath.Join(config.OutputDir, "swagger.json"),
filepath.Join(config.OutputDir, "swagger.yaml"),
filepath.Join(config.OutputDir, config.InstanceName+"_"+"docs.go"),
filepath.Join(config.OutputDir, config.InstanceName+"_"+"swagger.json"),
filepath.Join(config.OutputDir, config.InstanceName+"_"+"swagger.yaml"),
}
for _, expectedFile := range expectedFiles {
if _, err := os.Stat(expectedFile); os.IsNotExist(err) {
Expand Down