feat(go-swagger): Refactoring to use go-swagger templating system for most of the work
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
@ -6,3 +6,10 @@ 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"
|
||||
|
||||
// regeneratedFiles is a list of files / directory that are automatically regenerated by go-swagger.
|
||||
// They will be automatically deleted by the generate api process.
|
||||
var regeneratedFiles = []string{
|
||||
"restapi",
|
||||
"models",
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.dev.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
@ -13,13 +16,34 @@ import (
|
||||
func (a APIType) GenerateAPI(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
// delete file that need to be regenerated
|
||||
err := a.deleteRegeneratedFiles(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to delete Files that will be regenerated by Go Swagger")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
err = a.createDirectories(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to create project directories")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Launch Go Swagger
|
||||
err := a.executeGoSwagger(path)
|
||||
err = a.executeGoSwagger(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to Execute Go Swagger")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// No use atm
|
||||
// Generate files from spec
|
||||
// err = a.generateFromSpec(path)
|
||||
// if err != nil {
|
||||
// log.Error("Fail to generate files from spec")
|
||||
// return errors.Trace(err)
|
||||
// }
|
||||
|
||||
// Delete api yaml temporary file
|
||||
err = a.deleteTempApiYaml(path)
|
||||
if err != nil {
|
||||
@ -29,3 +53,44 @@ func (a APIType) GenerateAPI(path string, config *models.Config) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteRegeneratedFiles will delete files that will be regenerated by go swagger.
|
||||
func (a APIType) deleteRegeneratedFiles(path string) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
for _, file := range regeneratedFiles {
|
||||
tempFilePath := filepath.Join(path, file)
|
||||
|
||||
exist, err := helpers.FileExists(tempFilePath, true)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
if !exist {
|
||||
log.Debug("File/Directory doesn't exist already")
|
||||
return nil
|
||||
}
|
||||
|
||||
err = os.RemoveAll(tempFilePath)
|
||||
if err != nil {
|
||||
log.Errorf("fail to delete file/dir in %s", tempFilePath)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// // generateFromSpec will read the swagger spec and create cmd server, handlers, test ...
|
||||
// func (a APIType) generateFromSpec(path string) error {
|
||||
// log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
// tempFilePath := filepath.Join(path, mergeYamlFileName)
|
||||
// specData, err := spec.NewSpec(tempFilePath)
|
||||
// if err != nil {
|
||||
// return errors.Trace(err)
|
||||
// }
|
||||
// fmt.Printf("%#v", specData.GetOperationGroups())
|
||||
|
||||
// return nil
|
||||
// }
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
func (a APIType) GenerateDockerfile(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
templatePath := filepath.Join(templateDirectory, "Dockerfile.tmpl")
|
||||
templatePath := filepath.Join(templateDirectory, "custom/Dockerfile.tmpl")
|
||||
savePath := filepath.Join(path, "Dockerfile")
|
||||
|
||||
data, err := a.getStdTemplate(path, config)
|
||||
|
@ -15,7 +15,7 @@ import (
|
||||
func (a APIType) GenerateLauncher(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
templatePath := filepath.Join(templateDirectory, "launcher.sh.tmpl")
|
||||
templatePath := filepath.Join(templateDirectory, "custom/launcher.sh.tmpl")
|
||||
savePath := filepath.Join(path, "launcher.sh")
|
||||
|
||||
data, err := a.getStdTemplate(path, config)
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
func (a APIType) GenerateMakefile(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
templatePath := filepath.Join(templateDirectory, "Makefile.tmpl")
|
||||
templatePath := filepath.Join(templateDirectory, "custom/Makefile.tmpl")
|
||||
savePath := filepath.Join(path, "Makefile")
|
||||
|
||||
data, err := a.getStdTemplate(path, config)
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
func (a APIType) GenerateReadme(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
templatePath := filepath.Join(templateDirectory, "Readme.md.tmpl")
|
||||
templatePath := filepath.Join(templateDirectory, "custom/Readme.md.tmpl")
|
||||
savePath := filepath.Join(path, "Readme.md")
|
||||
|
||||
data, err := a.getStdTemplate(path, config)
|
||||
|
@ -72,6 +72,7 @@ func (a APIType) executeGoSwagger(path string) error {
|
||||
|
||||
// goSwaggerMixin will call go-swagger code to merge api yaml files.
|
||||
func (a APIType) goSwaggerMixin(path string, primaryFile string, mixinFiles []string) ([]string, error) {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
mixinSpec := commands.MixinSpec{
|
||||
Compact: false,
|
||||
@ -86,16 +87,24 @@ func (a APIType) goSwaggerMixin(path string, primaryFile string, mixinFiles []st
|
||||
|
||||
// goSwaggerGenerate will generate all app files as go-swagger binary will.
|
||||
func (a APIType) goSwaggerGenerate(path string) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
execDirectory, err := helpers.GetExecutableDirectory()
|
||||
if err != nil {
|
||||
log.Error("Fail to get Gouick directory")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
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.ServerPackage = "restapi"
|
||||
generateCmd.Server.Models.ModelPackage = "models"
|
||||
generateCmd.Server.Operations.APIPackage = "operations"
|
||||
generateCmd.Server.Shared.StrictResponders = true
|
||||
generateCmd.Server.Shared.Template = "stratoscale"
|
||||
generateCmd.Server.Shared.StrictResponders = false
|
||||
generateCmd.Server.Shared.TemplateDir = flags.Filename(filepath.Join(execDirectory, "templates/go-swagger/app/templates/"))
|
||||
generateCmd.Server.Shared.ConfigFile = flags.Filename(filepath.Join(execDirectory, "templates/go-swagger/app/config.yaml"))
|
||||
|
||||
return generateCmd.Server.Execute(nil)
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.dev.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
"git.dev.m-and-m.ovh/mderasse/gouick/models"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -14,6 +15,7 @@ import (
|
||||
|
||||
// getProcesses will read cmd directory and guess the app processes.
|
||||
func (a APIType) getProcesses(path string) ([]*models.Process, error) {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
cmdPath := filepath.Join(path, "cmd")
|
||||
cmdDirectories, err := filepath.Glob(filepath.Join(cmdPath, "*"))
|
||||
|
@ -25,15 +25,15 @@ func (a APIType) createDefaultAPIYamls(path string, config *models.Config) error
|
||||
templateFileList := []templateFileStruct{
|
||||
{
|
||||
savePath: filepath.Join(path, "api.yaml"),
|
||||
templatePath: filepath.Join(templateDirectory, "api.yaml.tmpl"),
|
||||
templatePath: filepath.Join(templateDirectory, "custom/api.yaml.tmpl"),
|
||||
},
|
||||
{
|
||||
savePath: filepath.Join(path, "api/001-general.yaml"),
|
||||
templatePath: filepath.Join(templateDirectory, "api/001-general.yaml.tmpl"),
|
||||
templatePath: filepath.Join(templateDirectory, "custom/api/001-general.yaml.tmpl"),
|
||||
},
|
||||
{
|
||||
savePath: filepath.Join(path, "api/001-monitoring.yaml"),
|
||||
templatePath: filepath.Join(templateDirectory, "api/002-monitoring.yaml.tmpl"),
|
||||
templatePath: filepath.Join(templateDirectory, "custom/api/002-monitoring.yaml.tmpl"),
|
||||
},
|
||||
}
|
||||
|
||||
@ -60,8 +60,7 @@ func (a APIType) deleteTempApiYaml(path string) error {
|
||||
|
||||
tempFilePath := filepath.Join(path, mergeYamlFileName)
|
||||
|
||||
// delete
|
||||
exist, err := helpers.FileExists(tempFilePath)
|
||||
exist, err := helpers.FileExists(tempFilePath, false)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ func IsGouickDirectory(path string) (bool, error) {
|
||||
|
||||
// IsGouickProject will check if a gouick config file exist and readeable.
|
||||
func IsGouickProject(path string) (bool, error) {
|
||||
exist, err := FileExists(filepath.Join(path, configFile))
|
||||
exist, err := FileExists(filepath.Join(path, configFile), false)
|
||||
if err != nil {
|
||||
return false, errors.Trace(err)
|
||||
}
|
||||
@ -42,7 +42,7 @@ func IsGouickProject(path string) (bool, error) {
|
||||
}
|
||||
|
||||
// FileExists will check if a file exist and is readeable.
|
||||
func FileExists(path string) (bool, error) {
|
||||
func FileExists(path string, allowDirectory bool) (bool, error) {
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrPermission) {
|
||||
@ -53,7 +53,7 @@ func FileExists(path string) (bool, error) {
|
||||
return false, errors.Trace(err)
|
||||
}
|
||||
}
|
||||
if fileInfo.IsDir() {
|
||||
if fileInfo.IsDir() && !allowDirectory {
|
||||
return false, errors.NotValidf("%s is actually a directory", path)
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
|
||||
// IsGoProject check if we are in a golang project (have go.mod).
|
||||
func IsGoProject(path string) (bool, error) {
|
||||
exist, err := FileExists(filepath.Join(path, "go.mod"))
|
||||
exist, err := FileExists(filepath.Join(path, "go.mod"), false)
|
||||
if err != nil {
|
||||
return false, errors.Trace(err)
|
||||
}
|
||||
@ -19,7 +19,7 @@ func IsGoProject(path string) (bool, error) {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
exist, err = FileExists(filepath.Join(path, "go.sum"))
|
||||
exist, err = FileExists(filepath.Join(path, "go.sum"), false)
|
||||
if err != nil {
|
||||
return false, errors.Trace(err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user