feat(init): Continue on first go-swagger gen
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Matthieu 'JP' DERASSE 2022-09-02 18:50:04 +00:00
parent 9107f0d36e
commit a992c3cd5e
Signed by: mderasse
GPG Key ID: 55141C777B16A705
9 changed files with 1269 additions and 23 deletions

41
go.mod
View File

@ -4,7 +4,9 @@ go 1.17
require ( require (
github.com/blang/semver v3.5.1+incompatible github.com/blang/semver v3.5.1+incompatible
github.com/go-swagger/go-swagger v0.29.0
github.com/iancoleman/strcase v0.2.0 github.com/iancoleman/strcase v0.2.0
github.com/jessevdk/go-flags v1.5.0
github.com/juju/errors v1.0.0 github.com/juju/errors v1.0.0
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.5.0 github.com/spf13/cobra v1.5.0
@ -12,7 +14,46 @@ require (
) )
require ( require (
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/felixge/httpsnoop v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.2 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/runtime v0.21.1 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
github.com/go-openapi/strfmt v0.21.3 // indirect
github.com/go-openapi/swag v0.21.1 // indirect
github.com/go-openapi/validate v0.20.3 // indirect
github.com/gorilla/handlers v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/spf13/afero v1.8.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.10.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/toqueteos/webbrowser v1.2.0 // indirect
go.mongodb.org/mongo-driver v1.10.0 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/tools v0.1.8 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/ini.v1 v1.66.3 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
) )

1080
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -2,3 +2,7 @@ package go_swagger
// templateDirectory contain the path to the templates for that apiType. // templateDirectory contain the path to the templates for that apiType.
const templateDirectory = "templates/go-swagger" 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"

View File

@ -47,11 +47,25 @@ func (a APIType) CreateProjectSkeleton(path string, config *models.Config) error
} }
// Launch Go Swagger // Launch Go Swagger
err = a.executeGoSwagger(path, config) err = a.executeGoSwagger(path)
if err != nil { if err != nil {
log.Error("Fail to Execute Go Swagger") log.Error("Fail to Execute Go Swagger")
return errors.Trace(err) 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 return nil
} }

View 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
}

View File

@ -2,19 +2,21 @@ package go_swagger
import ( import (
"os" "os"
"os/exec"
"path/filepath" "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" "github.com/juju/errors"
"git.home.m-and-m.ovh/mderasse/gouick/helpers" "git.home.m-and-m.ovh/mderasse/gouick/helpers"
"git.home.m-and-m.ovh/mderasse/gouick/models"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// executeGoSwagger will merge api yaml files and launch go-swagger generator. // 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()) log.Debugf("Starting %s - %s", a.GetName(), helpers.GetCurrentFuncName())
// making sure that we are in the project directory // 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) return errors.Trace(err)
} }
// merge api files. // list files in api/*
// TODO: use filepath.Glob and avoid an ugly bin sh. log.Debug("Merging api files")
cmd := exec.Command("/bin/sh", "-c", "swagger mixin --format=yaml --output=api-temp.yaml api.yaml api/*")
output, err := cmd.CombinedOutput() subApiFiles, err := filepath.Glob(filepath.Join(path, "api", "*"))
if err != nil { 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) 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. // cleanup some directory to avoid issues.
for _, directory := range []string{"models", "restapi/operations"} { for _, directory := range []string{"models", "restapi/operations"} {
@ -44,16 +57,41 @@ func (a APIType) executeGoSwagger(path string, config *models.Config) error {
} }
// generate server files // generate server files
cmd = exec.Command("swagger", "generate", "server", "--spec=api-temp.yaml") log.Debug("Calling Go Swagger code to generate API files")
output, err = cmd.CombinedOutput()
err = a.goSwaggerGenerate(path)
if err != nil { 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) return errors.Trace(err)
} }
log.Debugf("'swagger generate server --spec=api-temp.yaml' returned the following output: %s", string(output)) log.Debug("API files successfully generated using Go Swagger")
// TODO: execute 'go get ./...'
return nil 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)
}

View 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
}

View File

@ -29,7 +29,7 @@ func IsGouickDirectory(path string) (bool, error) {
// IsGouickProject will check if a gouick config file exist and readeable. // IsGouickProject will check if a gouick config file exist and readeable.
func IsGouickProject(path string) (bool, error) { func IsGouickProject(path string) (bool, error) {
exist, err := fileExists(filepath.Join(path, configFile)) exist, err := FileExists(filepath.Join(path, configFile))
if err != nil { if err != nil {
return false, errors.Trace(err) return false, errors.Trace(err)
} }
@ -41,8 +41,8 @@ func IsGouickProject(path string) (bool, error) {
return true, nil return true, nil
} }
// fileExists will check if a file exist and is readeable. // FileExists will check if a file exist and is readeable.
func fileExists(path string) (bool, error) { func FileExists(path string) (bool, error) {
fileInfo, err := os.Stat(path) fileInfo, err := os.Stat(path)
if err != nil { if err != nil {
if errors.Is(err, fs.ErrPermission) { if errors.Is(err, fs.ErrPermission) {

View File

@ -10,7 +10,7 @@ import (
// IsGoProject check if we are in a golang project (have go.mod). // IsGoProject check if we are in a golang project (have go.mod).
func IsGoProject(path string) (bool, error) { func IsGoProject(path string) (bool, error) {
exist, err := fileExists(filepath.Join(path, "go.mod")) exist, err := FileExists(filepath.Join(path, "go.mod"))
if err != nil { if err != nil {
return false, errors.Trace(err) return false, errors.Trace(err)
} }
@ -19,7 +19,7 @@ func IsGoProject(path string) (bool, error) {
return true, nil return true, nil
} }
exist, err = fileExists(filepath.Join(path, "go.sum")) exist, err = FileExists(filepath.Join(path, "go.sum"))
if err != nil { if err != nil {
return false, errors.Trace(err) return false, errors.Trace(err)
} }