Skip to content

Commit

Permalink
feat: Generated file name from instanceName (#1112)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferrany1 committed Jan 28, 2022
1 parent 32f02b9 commit cae7467
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
18 changes: 15 additions & 3 deletions gen/gen.go
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
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

0 comments on commit cae7467

Please sign in to comment.