feat(init): Continue on template
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:
13
helpers/api_types/base/create_project_skeleton.go
Normal file
13
helpers/api_types/base/create_project_skeleton.go
Normal file
@@ -0,0 +1,13 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// CreateProjectSkeleton will generate all the files needed to start a new project.
|
||||
func (a APIType) CreateProjectSkeleton(path string, config *models.Config) error {
|
||||
return errors.NotImplementedf("%s not implemented for %s", helpers.GetCurrentFuncName(), a.GetName())
|
||||
}
|
13
helpers/api_types/base/generate_makefile.go
Normal file
13
helpers/api_types/base/generate_makefile.go
Normal file
@@ -0,0 +1,13 @@
|
||||
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())
|
||||
}
|
4
helpers/api_types/go_swagger/constants.go
Normal file
4
helpers/api_types/go_swagger/constants.go
Normal file
@@ -0,0 +1,4 @@
|
||||
package go_swagger
|
||||
|
||||
// templateDirectory contain the path to the templates for that apiType.
|
||||
const templateDirectory = "templates/go-swagger"
|
44
helpers/api_types/go_swagger/create_directories.go
Normal file
44
helpers/api_types/go_swagger/create_directories.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
"git.home.m-and-m.ovh/mderasse/gouick/helpers"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var standardDirectories = []string{
|
||||
"api",
|
||||
"chart",
|
||||
"cmd",
|
||||
"models",
|
||||
"pkg",
|
||||
"restapi",
|
||||
"testdata",
|
||||
}
|
||||
|
||||
// createDirectories will create all skeleton directories for the project.
|
||||
func (a APIType) createDirectories(path string) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
for _, directory := range standardDirectories {
|
||||
|
||||
log.Debugf("Will create directory %s", directory)
|
||||
|
||||
fullPath := filepath.Join(path, directory)
|
||||
|
||||
created, err := helpers.CheckAndCreateDir(fullPath)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to create directory %s", directory)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
if !created {
|
||||
log.Debugf("Skipping directory %s", directory)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -1,13 +1,32 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"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"
|
||||
)
|
||||
|
||||
// CreateProjectSkeleton will generate all the files needed to start a new project.
|
||||
// it will return a list of created files or an error
|
||||
func (a APIType) CreateProjectSkeleton(path string) ([]string, error) {
|
||||
log.Debugf("Starting %s create project skeleton", string(a.GetName()))
|
||||
func (a APIType) CreateProjectSkeleton(path string, config *models.Config) error {
|
||||
log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
|
||||
|
||||
return nil, nil
|
||||
err := a.createDirectories(path)
|
||||
if err != nil {
|
||||
log.Error("Fail to create project directories")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
// Generate Makefile
|
||||
err = a.GenerateMakefile(path, config)
|
||||
if err != nil {
|
||||
log.Error("Fail to generate Makefile")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
// Generate Readme
|
||||
// Generate api.yaml
|
||||
|
||||
return nil
|
||||
}
|
||||
|
36
helpers/api_types/go_swagger/generate_makefile.go
Normal file
36
helpers/api_types/go_swagger/generate_makefile.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package go_swagger
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
"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 makefile struct {
|
||||
AppNameKebabCase string
|
||||
}
|
||||
|
||||
// GenerateMakefile will generate makefile based on the given config.
|
||||
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{
|
||||
AppNameKebabCase: strcase.ToKebab(config.ProjectName),
|
||||
}
|
||||
|
||||
err := helpers.WriteTemplate(templatePath, savePath, data)
|
||||
if err != nil {
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@@ -5,6 +5,8 @@ import "git.home.m-and-m.ovh/mderasse/gouick/models"
|
||||
// APITypeInterface is the interface that need to be respected by an APIType.
|
||||
type APITypeInterface interface {
|
||||
CheckInitialize() error
|
||||
GetName() models.APITypeName
|
||||
CreateProjectSkeleton(path string, config *models.Config) error
|
||||
GenerateMakefile(path string, config *models.Config) error
|
||||
GetInitializeUserInput(params *models.Config) (*models.Config, error)
|
||||
GetName() models.APITypeName
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ package helpers
|
||||
import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/juju/errors"
|
||||
@@ -136,3 +137,12 @@ func RemoveDirectoryContent(path string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetExecutableDirectory will return the directory of Gouick.
|
||||
func GetExecutableDirectory() (string, error) {
|
||||
e, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", errors.Trace(err)
|
||||
}
|
||||
return path.Dir(e), nil
|
||||
}
|
||||
|
50
helpers/template.go
Normal file
50
helpers/template.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
|
||||
"github.com/juju/errors"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// WriteTemplate will parse the given template filepath, and write the result to the savePath.
|
||||
func WriteTemplate(templatePath string, savePath string, data interface{}) error {
|
||||
|
||||
execDirectory, err := GetExecutableDirectory()
|
||||
if err != nil {
|
||||
log.Error("Fail to get Gouick directory")
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
templatePath = filepath.Join(execDirectory, templatePath)
|
||||
|
||||
template, err := template.ParseFiles(templatePath)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to parse the template %s", templatePath)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
//nolint: gosec // We compute the savePath
|
||||
fh, err := os.Create(savePath)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to create saving path %s", savePath)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := fh.Close(); err != nil {
|
||||
log.Errorf("Error closing file: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
err = template.Execute(fh, data)
|
||||
if err != nil {
|
||||
log.Errorf("Fail to write compute the template content for %s", savePath)
|
||||
return errors.Trace(err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user