feat(init): continue on template
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:
@ -1,13 +0,0 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/models"
|
||||
)
|
||||
|
||||
// GenerateMakefile will generate makefile based on the given config.
|
||||
func (a APIType) GenerateMakefile(path string, config *models.Config) error {
|
||||
return errors.NotImplementedf("%s not implemented for %s", helpers.GetCurrentFuncName(), a.GetName())
|
||||
}
|
@ -16,7 +16,11 @@ var standardDirectories = []string{
|
||||
"cmd",
|
||||
"models",
|
||||
"pkg",
|
||||
"pkg/api",
|
||||
"pkg/helper",
|
||||
"pkg/transform",
|
||||
"restapi",
|
||||
"restapi/operations",
|
||||
"testdata",
|
||||
}
|
||||
|
||||
|
@ -20,13 +20,38 @@ func (a APIType) CreateProjectSkeleton(path string, config *models.Config) error
|
||||
}
|
||||
|
||||
// Generate Makefile
|
||||
err = a.GenerateMakefile(path, config)
|
||||
err = a.generateMakefile(path, config)
|
||||
if err != nil {
|
||||
log.Error("Fail to generate Makefile")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
// Generate Readme
|
||||
// Generate api.yaml
|
||||
err = a.generateReadme(path, config)
|
||||
if err != nil {
|
||||
log.Error("Fail to generate Readme.md")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Generate api yaml files
|
||||
err = a.generateAPIYamls(path, config)
|
||||
if err != nil {
|
||||
log.Error("Fail to generate API Yaml files")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// init Go Module
|
||||
err = a.initializeGoModule(path, config)
|
||||
if err != nil {
|
||||
log.Error("Fail to initialize Go Module")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Launch Go Swagger
|
||||
err = a.executeGoSwagger(path, config)
|
||||
if err != nil {
|
||||
log.Error("Fail to Execute Go Swagger")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
59
helpers/api_types/go_swagger/execute_go_swagger.go
Normal file
59
helpers/api_types/go_swagger/execute_go_swagger.go
Normal file
@ -0,0 +1,59 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"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 {
|
||||
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)
|
||||
}
|
||||
|
||||
// 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()
|
||||
if err != nil {
|
||||
log.Error("fail to execute 'swagger mixin --format=yaml --output=api-temp.yaml api.yaml api/*'")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("'swagger mixin --format=yaml --output=api-temp.yaml api.yaml api/*' returned the following output: %s", string(output))
|
||||
|
||||
// cleanup some directory to avoid issues.
|
||||
for _, directory := range []string{"models", "restapi/operations"} {
|
||||
err := helpers.RemoveDirectoryContent(filepath.Join(path, directory))
|
||||
if err != nil {
|
||||
log.Errorf("fail to delete content of directory '%s'", directory)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
// generate server files
|
||||
cmd = exec.Command("swagger", "generate", "server", "--spec=api-temp.yaml")
|
||||
output, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Error("fail to execute 'swagger generate server --spec=api-temp.yaml'")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("'swagger generate server --spec=api-temp.yaml' returned the following output: %s", string(output))
|
||||
|
||||
// TODO: execute 'go get ./...'
|
||||
|
||||
return nil
|
||||
}
|
60
helpers/api_types/go_swagger/generate_api_yamls.go
Normal file
60
helpers/api_types/go_swagger/generate_api_yamls.go
Normal file
@ -0,0 +1,60 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type apiYamlTemplate struct {
|
||||
ProjectDescription string
|
||||
ProjectName string
|
||||
ProjectOwner string
|
||||
}
|
||||
|
||||
// generateAPIYamls will generate a readme based on the given config.
|
||||
// Launched only at project init.
|
||||
func (a APIType) generateAPIYamls(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
type templateFileStruct struct {
|
||||
savePath string
|
||||
templatePath string
|
||||
}
|
||||
|
||||
templateFileList := []templateFileStruct{
|
||||
{
|
||||
savePath: filepath.Join(path, "api.yaml"),
|
||||
templatePath: filepath.Join(templateDirectory, "api.yaml.tmpl"),
|
||||
},
|
||||
{
|
||||
savePath: filepath.Join(path, "api/001-general.yaml"),
|
||||
templatePath: filepath.Join(templateDirectory, "api/001-general.yaml.tmpl"),
|
||||
},
|
||||
{
|
||||
savePath: filepath.Join(path, "api/001-monitoring.yaml"),
|
||||
templatePath: filepath.Join(templateDirectory, "api/002-monitoring.yaml.tmpl"),
|
||||
},
|
||||
}
|
||||
|
||||
data := apiYamlTemplate{
|
||||
ProjectDescription: config.ProjectDescription,
|
||||
ProjectName: config.ProjectName,
|
||||
ProjectOwner: config.ProjectOwner,
|
||||
}
|
||||
|
||||
for _, templateFile := range templateFileList {
|
||||
|
||||
err := helpers.WriteTemplate(templateFile.templatePath, templateFile.savePath, data)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -12,18 +12,19 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type makefile struct {
|
||||
type makefileTemplate struct {
|
||||
AppNameKebabCase string
|
||||
}
|
||||
|
||||
// GenerateMakefile will generate makefile based on the given config.
|
||||
func (a APIType) GenerateMakefile(path string, config *models.Config) error {
|
||||
// generateMakefile will generate makefile based on the given config.
|
||||
// Launched only at project init or on force.
|
||||
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")
|
||||
savePath := filepath.Join(path, "Makefile")
|
||||
|
||||
data := makefile{
|
||||
data := makefileTemplate{
|
||||
AppNameKebabCase: strcase.ToKebab(config.ProjectName),
|
||||
}
|
||||
|
||||
|
38
helpers/api_types/go_swagger/generate_readme.go
Normal file
38
helpers/api_types/go_swagger/generate_readme.go
Normal file
@ -0,0 +1,38 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
type readmeTemplate struct {
|
||||
ProjectDescription string
|
||||
ProjectName string
|
||||
}
|
||||
|
||||
// generateReadme will generate a readme based on the given config.
|
||||
// Launched only at project init.
|
||||
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")
|
||||
savePath := filepath.Join(path, "Readme.md")
|
||||
|
||||
data := readmeTemplate{
|
||||
ProjectDescription: config.ProjectDescription,
|
||||
ProjectName: config.ProjectName,
|
||||
}
|
||||
|
||||
err := helpers.WriteTemplate(templatePath, savePath, data)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -32,8 +32,8 @@ func (a APIType) GetInitializeUserInput(params *models.Config) (*models.Config,
|
||||
if !isInGoPath {
|
||||
log.Debug("We are not in GoPath, ask extra info")
|
||||
goModulePath := helpers.StringInput()
|
||||
log.Info("Go Module name:")
|
||||
params.ModuleName = &goModulePath
|
||||
log.Info("Go Module path:")
|
||||
params.ModulePath = &goModulePath
|
||||
}
|
||||
|
||||
log.Info("Do you want to enable database models auto generation ?")
|
||||
|
39
helpers/api_types/go_swagger/initialize_go_module.go
Normal file
39
helpers/api_types/go_swagger/initialize_go_module.go
Normal file
@ -0,0 +1,39 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// initializeGoModule will launch a go mod init.
|
||||
func (a APIType) initializeGoModule(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
if err := os.Chdir(path); err != nil {
|
||||
log.Error("Fail to move to the path")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
modulePath := ""
|
||||
if config.ModulePath != nil {
|
||||
modulePath = *config.ModulePath
|
||||
}
|
||||
|
||||
cmd := exec.Command("go", "mod", "init", modulePath)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Errorf("fail to execute go mod init %s", modulePath)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
log.Debugf("go mod init %s returned the following output: %s", modulePath, string(output))
|
||||
|
||||
return nil
|
||||
}
|
@ -6,7 +6,6 @@ import "git.home.m-and-m.ovh/mderasse/gouick/models"
|
||||
type APITypeInterface interface {
|
||||
CheckInitialize() error
|
||||
CreateProjectSkeleton(path string, config *models.Config) error
|
||||
GenerateMakefile(path string, config *models.Config) error
|
||||
GetInitializeUserInput(params *models.Config) (*models.Config, error)
|
||||
GetName() models.APITypeName
|
||||
}
|
||||
|
Reference in New Issue
Block a user