From ec6485aa452bc3503116e80fb90dd0176258fea9 Mon Sep 17 00:00:00 2001 From: Matthieu 'JP' DERASSE Date: Mon, 25 Jul 2022 20:20:10 +0000 Subject: [PATCH] feat(apiType): Start to implement apiType system --- cmd/init.go | 14 ++++- helpers/api_type/api_type.go | 20 +++++++ helpers/api_type/base/api_type.go | 5 ++ helpers/api_type/enum.go | 69 ++++++++++++++++++++++++ helpers/api_type/gin_gonic/api_type.go | 5 ++ helpers/api_type/go_swagger/api_type.go | 8 +++ helpers/api_type/interface.go | 5 ++ helpers/api_type/mojolicious/api_type.go | 8 +++ 8 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 helpers/api_type/api_type.go create mode 100644 helpers/api_type/base/api_type.go create mode 100644 helpers/api_type/enum.go create mode 100644 helpers/api_type/gin_gonic/api_type.go create mode 100644 helpers/api_type/go_swagger/api_type.go create mode 100644 helpers/api_type/interface.go create mode 100644 helpers/api_type/mojolicious/api_type.go diff --git a/cmd/init.go b/cmd/init.go index cb67206..f8783e1 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -6,8 +6,10 @@ package cmd import ( "os" + "strings" "git.home.m-and-m.ovh/mderasse/boot/helpers" + "git.home.m-and-m.ovh/mderasse/boot/helpers/api_type" "github.com/spf13/cobra" log "github.com/sirupsen/logrus" @@ -40,7 +42,7 @@ func runInitAction(cmd *cobra.Command, args []string) { log.Debugf("Starting command Init") - log.Debugf("Checking dependecies") + log.Debugf("Checking dependencies") if !checkDependencies() { return @@ -83,6 +85,16 @@ func runInitAction(cmd *cobra.Command, args []string) { 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: // Check we are in gopath or ask for the gomod name // Create directory diff --git a/helpers/api_type/api_type.go b/helpers/api_type/api_type.go new file mode 100644 index 0000000..82c2eb6 --- /dev/null +++ b/helpers/api_type/api_type.go @@ -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") +} diff --git a/helpers/api_type/base/api_type.go b/helpers/api_type/base/api_type.go new file mode 100644 index 0000000..50bc24f --- /dev/null +++ b/helpers/api_type/base/api_type.go @@ -0,0 +1,5 @@ +package base + +// ApiType +type ApiType struct { +} diff --git a/helpers/api_type/enum.go b/helpers/api_type/enum.go new file mode 100644 index 0000000..58d37d3 --- /dev/null +++ b/helpers/api_type/enum.go @@ -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 +} diff --git a/helpers/api_type/gin_gonic/api_type.go b/helpers/api_type/gin_gonic/api_type.go new file mode 100644 index 0000000..50bc24f --- /dev/null +++ b/helpers/api_type/gin_gonic/api_type.go @@ -0,0 +1,5 @@ +package base + +// ApiType +type ApiType struct { +} diff --git a/helpers/api_type/go_swagger/api_type.go b/helpers/api_type/go_swagger/api_type.go new file mode 100644 index 0000000..bc35be2 --- /dev/null +++ b/helpers/api_type/go_swagger/api_type.go @@ -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 +} diff --git a/helpers/api_type/interface.go b/helpers/api_type/interface.go new file mode 100644 index 0000000..f0cd944 --- /dev/null +++ b/helpers/api_type/interface.go @@ -0,0 +1,5 @@ +package api_type + +// ApiTypeInterface +type ApiTypeInterface interface { +} diff --git a/helpers/api_type/mojolicious/api_type.go b/helpers/api_type/mojolicious/api_type.go new file mode 100644 index 0000000..e545a65 --- /dev/null +++ b/helpers/api_type/mojolicious/api_type.go @@ -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 +}