feat(apiType): Start to implement apiType system

This commit is contained in:
Matthieu 'JP' DERASSE 2022-07-25 20:20:10 +00:00
parent 66ff28f6cd
commit ec6485aa45
Signed by: mderasse
GPG Key ID: 55141C777B16A705
8 changed files with 133 additions and 1 deletions

View File

@ -6,8 +6,10 @@ package cmd
import ( import (
"os" "os"
"strings"
"git.home.m-and-m.ovh/mderasse/boot/helpers" "git.home.m-and-m.ovh/mderasse/boot/helpers"
"git.home.m-and-m.ovh/mderasse/boot/helpers/api_type"
"github.com/spf13/cobra" "github.com/spf13/cobra"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -40,7 +42,7 @@ func runInitAction(cmd *cobra.Command, args []string) {
log.Debugf("Starting command Init") log.Debugf("Starting command Init")
log.Debugf("Checking dependecies") log.Debugf("Checking dependencies")
if !checkDependencies() { if !checkDependencies() {
return return
@ -83,6 +85,16 @@ func runInitAction(cmd *cobra.Command, args []string) {
return return
} }
// ask which API type we want to use
var possibleApiTypes []string
for _, apiType := range api_type.GetListOfApiTypeName() {
possibleApiTypes = append(possibleApiTypes, string(apiType))
}
log.Infof("Which kind of API do you want to init (possible values: %s)", strings.Join(possibleApiTypes, ", "))
apiType := helpers.ApiTypeInput()
log.Debugf("Using api type : %s", apiType)
// XXX: // XXX:
// Check we are in gopath or ask for the gomod name // Check we are in gopath or ask for the gomod name
// Create directory // Create directory

View File

@ -0,0 +1,20 @@
package api_type
import "github.com/juju/errors"
func GetApiType(in ApiTypeName) (ApiTypeInterface, error) {
if in == "" {
return nil, errors.BadRequestf("missing parameter")
}
if !in.IsValid() {
return nil, errors.NotValidf("invalid parameter")
}
switch in {
case ApyTypeName_GIN_GONIC:
case ApyTypeName_GO_SWAGGER:
case ApyTypeName_MOJOLICIOUS:
}
return nil, errors.NotFoundf("Unknown Api Type")
}

View File

@ -0,0 +1,5 @@
package base
// ApiType
type ApiType struct {
}

69
helpers/api_type/enum.go Normal file
View File

@ -0,0 +1,69 @@
package api_type
import (
"strings"
"github.com/juju/errors"
)
// ApiTypeName
type ApiTypeName string
const (
ApyTypeName_NULL ApiTypeName = ""
ApyTypeName_GO_SWAGGER ApiTypeName = "Go Swagger"
ApyTypeName_GIN_GONIC ApiTypeName = "Gin Gonic"
ApyTypeName_MOJOLICIOUS ApiTypeName = "Mojolicious"
)
// GetListOfApiTypeName returns a list of ApiTypeName
func GetListOfApiTypeName() []ApiTypeName {
return []ApiTypeName{
ApyTypeName_GO_SWAGGER,
ApyTypeName_GIN_GONIC,
ApyTypeName_MOJOLICIOUS,
}
}
// IsValid validates enum values
func (e ApiTypeName) IsValid() bool {
for _, v := range GetListOfApiTypeName() {
if e == v {
return true
}
}
return false
}
func NewApiTypeName(in string) (ApiTypeName, error) {
out := ApyTypeName_NULL
if in != "" {
out = ApiTypeName(in)
if !out.IsValid() {
return ApyTypeName_NULL, errors.BadRequestf("Value %s invalid for enum ApiTypeName", in)
}
}
return out, nil
}
func NewApiTypeFromInput(in string) (ApiTypeName, error) {
in = strings.ToLower(in)
in = strings.ReplaceAll(in, " ", "-")
in = strings.ReplaceAll(in, "_", "-")
out := ApyTypeName_NULL
if in != "" {
for _, apiTypeName := range GetListOfApiTypeName() {
apiTypeStr := strings.ToLower(string(apiTypeName))
apiTypeStr = strings.ReplaceAll(apiTypeStr, " ", "-")
apiTypeStr = strings.ReplaceAll(apiTypeStr, "_", "-")
if in == apiTypeStr {
return apiTypeName, nil
}
}
return ApyTypeName_NULL, errors.BadRequestf("Value %s invalid for enum ApiTypeName", in)
}
return out, nil
}

View File

@ -0,0 +1,5 @@
package base
// ApiType
type ApiType struct {
}

View File

@ -0,0 +1,8 @@
package go_swagger
import "git.home.m-and-m.ovh/mderasse/boot/helpers/api_type/base"
// ApiType
type ApiType struct {
base.ApiType
}

View File

@ -0,0 +1,5 @@
package api_type
// ApiTypeInterface
type ApiTypeInterface interface {
}

View File

@ -0,0 +1,8 @@
package mojolicious
import "git.home.m-and-m.ovh/mderasse/boot/helpers/api_type/base"
// ApiType
type ApiType struct {
base.ApiType
}