feat(init): Continue on first go-swagger gen
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@ -2,3 +2,7 @@ package go_swagger
|
||||
|
||||
// templateDirectory contain the path to the templates for that apiType.
|
||||
const templateDirectory = "templates/go-swagger"
|
||||
|
||||
// mergeYamlFileName is the name of the file that will contain the final yaml spec of the API.
|
||||
// that file will always be deleted after generation.
|
||||
const mergeYamlFileName = "api-temp.yaml"
|
||||
|
@ -47,11 +47,25 @@ func (a APIType) CreateProjectSkeleton(path string, config *models.Config) error
|
||||
}
|
||||
|
||||
// Launch Go Swagger
|
||||
err = a.executeGoSwagger(path, config)
|
||||
err = a.executeGoSwagger(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to Execute Go Swagger")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Delete api yaml temporary file
|
||||
err = a.deleteTempApiYaml(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to delete api yaml temporary file")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Launch Go Swagger
|
||||
err = a.downloadGoDeps(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to download go dependencies")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
38
helpers/api_types/go_swagger/delete_temp_api_yaml.go
Normal file
38
helpers/api_types/go_swagger/delete_temp_api_yaml.go
Normal file
@ -0,0 +1,38 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// deleteTempApiYaml will delete the temporary yaml api file generated by go-swagger.
|
||||
func (a APIType) deleteTempApiYaml(path string) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
tempFilePath := filepath.Join(path, mergeYamlFileName)
|
||||
|
||||
// delete
|
||||
exist, err := helpers.FileExists(tempFilePath)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if !exist {
|
||||
log.Debug("File doesn't exist already")
|
||||
return nil
|
||||
}
|
||||
|
||||
err = os.Remove(tempFilePath)
|
||||
if err != nil {
|
||||
log.Errorf("fail to delete file in %s", tempFilePath)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -2,19 +2,21 @@ package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-swagger/go-swagger/cmd/swagger/commands"
|
||||
"github.com/go-swagger/go-swagger/cmd/swagger/commands/generate"
|
||||
"github.com/jessevdk/go-flags"
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/models"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// executeGoSwagger will merge api yaml files and launch go-swagger generator.
|
||||
func (a APIType) executeGoSwagger(path string, config *models.Config) error {
|
||||
func (a APIType) executeGoSwagger(path string) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
// making sure that we are in the project directory
|
||||
@ -23,16 +25,27 @@ func (a APIType) executeGoSwagger(path string, config *models.Config) error {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// merge api files.
|
||||
// TODO: use filepath.Glob and avoid an ugly bin sh.
|
||||
cmd := exec.Command("/bin/sh", "-c", "swagger mixin --format=yaml --output=api-temp.yaml api.yaml api/*")
|
||||
output, err := cmd.CombinedOutput()
|
||||
// list files in api/*
|
||||
log.Debug("Merging api files")
|
||||
|
||||
subApiFiles, err := filepath.Glob(filepath.Join(path, "api", "*"))
|
||||
if err != nil {
|
||||
log.Error("fail to execute 'swagger mixin --format=yaml --output=api-temp.yaml api.yaml api/*'")
|
||||
log.Error("Fail to list api files in api directory")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("'swagger mixin --format=yaml --output=api-temp.yaml api.yaml api/*' returned the following output: %s", string(output))
|
||||
// merge api files.
|
||||
collisions, err := a.goSwaggerMixin(path, "api.yaml", subApiFiles)
|
||||
if err != nil {
|
||||
log.Error("Fail to merge api files")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if len(collisions) > 0 {
|
||||
return errors.NotValidf("The following collisions has been detected: %s", strings.Join(collisions, "\n"))
|
||||
}
|
||||
|
||||
log.Debug("Api files successfully merged")
|
||||
|
||||
// cleanup some directory to avoid issues.
|
||||
for _, directory := range []string{"models", "restapi/operations"} {
|
||||
@ -44,16 +57,41 @@ func (a APIType) executeGoSwagger(path string, config *models.Config) error {
|
||||
}
|
||||
|
||||
// generate server files
|
||||
cmd = exec.Command("swagger", "generate", "server", "--spec=api-temp.yaml")
|
||||
output, err = cmd.CombinedOutput()
|
||||
log.Debug("Calling Go Swagger code to generate API files")
|
||||
|
||||
err = a.goSwaggerGenerate(path)
|
||||
if err != nil {
|
||||
log.Error("fail to execute 'swagger generate server --spec=api-temp.yaml'")
|
||||
log.Error("Fail to generate server files")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("'swagger generate server --spec=api-temp.yaml' returned the following output: %s", string(output))
|
||||
|
||||
// TODO: execute 'go get ./...'
|
||||
log.Debug("API files successfully generated using Go Swagger")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// goSwaggerMixin will call go-swagger code to merge api yaml files.
|
||||
func (a APIType) goSwaggerMixin(path string, primaryFile string, mixinFiles []string) ([]string, error) {
|
||||
|
||||
mixinSpec := commands.MixinSpec{
|
||||
Compact: false,
|
||||
Output: flags.Filename(filepath.Join(path, mergeYamlFileName)),
|
||||
KeepSpecOrder: true,
|
||||
Format: "yaml",
|
||||
IgnoreConflicts: false,
|
||||
}
|
||||
|
||||
return mixinSpec.MixinFiles(primaryFile, mixinFiles, nil)
|
||||
}
|
||||
|
||||
// goSwaggerGenerate will generate all app files as go-swagger binary will.
|
||||
func (a APIType) goSwaggerGenerate(path string) error {
|
||||
generateCmd := commands.Generate{
|
||||
Server: &generate.Server{},
|
||||
}
|
||||
generateCmd.Server.Shared.Spec = flags.Filename(filepath.Join(path, mergeYamlFileName))
|
||||
generateCmd.Server.Shared.Target = flags.Filename(path)
|
||||
generateCmd.Server.Shared.StrictResponders = true
|
||||
|
||||
return generateCmd.Server.Execute(nil)
|
||||
}
|
||||
|
35
helpers/api_types/go_swagger/golang.go
Normal file
35
helpers/api_types/go_swagger/golang.go
Normal file
@ -0,0 +1,35 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// downloadGoDeps will execute go get ./... in the give repository.
|
||||
func (a APIType) downloadGoDeps(path string) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
// making sure that we are in the project directory
|
||||
if err := os.Chdir(path); err != nil {
|
||||
log.Error("Fail to move to the path")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// execute go get to download missing package
|
||||
cmd := exec.Command("go", "get", "./...")
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Error("fail to execute 'go get ./...'")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("'go get ./...' returned the following output: %s", string(output))
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user