From be695f69bca8d80e3f79514a5b13a5a11b032858 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Wed, 6 Mar 2019 16:50:09 +0100 Subject: [PATCH] Make it possible to generate BOM with supported version. Simplify code. --- pkg/scaffold/types.go | 16 ++++----- pkg/server/server.go | 79 +++++++++++++++++++++---------------------- 2 files changed, 45 insertions(+), 50 deletions(-) diff --git a/pkg/scaffold/types.go b/pkg/scaffold/types.go index 4a56dd9..3b7d5c2 100644 --- a/pkg/scaffold/types.go +++ b/pkg/scaffold/types.go @@ -18,6 +18,7 @@ type Project struct { SnowdropBomVersion string `yaml:"snowdropbom" json:"snowdropbom"` SpringBootVersion string `yaml:"springbootversion" json:"springbootversion"` + UseSupported bool Modules []Module `yaml:"modules" json:"modules"` Dependencies []Dependency @@ -33,30 +34,27 @@ type Config struct { ExtraProperties ExtraProperties `yaml:"extraProperties" json:"extraProperties"` } -func (c *Config) GetCorrespondingSnowDropBom(version string) string { - logrus.Debugf("Version to search for %s", version) +func (c *Config) GetCorrespondingSnowDropBom(version string) Bom { for _, bom := range c.Boms { - logrus.Debugf("Bom is %v", bom) if bom.Community == version { - logrus.Debugf("Matching for %s and %s", bom.Community, bom.Snowdrop) - return bom.Snowdrop + return bom } } - return "" + return Bom{} } -func (c *Config) IsBOMVersionSupported(version string) bool { +func (c *Config) doesBOMExistFor(version string) bool { // Add .RELEASE if it's not present since it's expected in the configuration i := strings.Index(version, releaseSuffix) if i < 0 { version = version + releaseSuffix } - return len(c.GetCorrespondingSnowDropBom(version)) != 0 + return c.GetCorrespondingSnowDropBom(version).Community == version } func (c *Config) GetModulesCompatibleWith(version string) []Module { - if c.IsBOMVersionSupported(version) { + if c.doesBOMExistFor(version) { return keepModulesCompatibleWith(c.Modules, version) } return []Module{} diff --git a/pkg/server/server.go b/pkg/server/server.go index 9d1447c..6847e4f 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -8,6 +8,7 @@ import ( "net/http" "os" "path/filepath" + "strconv" "strings" "time" @@ -75,15 +76,7 @@ func Run(version string, gitcommit string) { log.Fatal(http.ListenAndServe(":"+port, router)) } -func getUrlVal(r *http.Request, k string) string { - return r.URL.Query().Get(k) -} - -func getArrayVal(r *http.Request, k string, params map[string][]string) []string { - return params[k] -} - -func convertArrayToStruct(modules []string) []scaffold.Module { +func asModuleArray(modules []string) []scaffold.Module { mod := make([]scaffold.Module, 0) for _, e := range modules { mod = append(mod, scaffold.Module{Name: e}) @@ -91,10 +84,6 @@ func convertArrayToStruct(modules []string) []scaffold.Module { return mod } -func getArrayModuleVal(r *http.Request, k string, params map[string][]string) []scaffold.Module { - return convertArrayToStruct(params[k]) -} - func modulesFor(w http.ResponseWriter, r *http.Request) { setCORSHeaders(r, w) vars := mux.Vars(r) @@ -134,44 +123,52 @@ func CreateZipFile(w http.ResponseWriter, r *http.Request) { params, _ := url.ParseQuery(r.URL.RawQuery) p := scaffold.GetDefaultProject() - if getUrlVal(r, "template") != "" { - p.Template = getUrlVal(r, "template") - } - if getUrlVal(r, "groupid") != "" { - p.GroupId = getUrlVal(r, "groupid") - } - if getUrlVal(r, "artifactid") != "" { - p.ArtifactId = getUrlVal(r, "artifactid") - } - if getUrlVal(r, "version") != "" { - p.Version = getUrlVal(r, "version") - } - if getUrlVal(r, "packagename") != "" { - p.PackageName = getUrlVal(r, "packagename") - } - if len(getArrayModuleVal(r, "module", params)) > 0 { - p.Modules = getArrayModuleVal(r, "module", params) + p.SpringBootVersion = params.Get("springbootversion") + p.SnowdropBomVersion = params.Get("snowdropbom") + + if len(p.SpringBootVersion) == 0 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("Must provide at least Spring Boot version")) + return } - if getUrlVal(r, "snowdropbom") != "" { - p.SnowdropBomVersion = getUrlVal(r, "snowdropbom") + + // retrieve bom information associated with the Spring Boot version + bom := scaffold.GetConfig().GetCorrespondingSnowDropBom(p.SpringBootVersion) + + // If the snowdrop bom version is not defined BUT only the Spring Boot Version, then get the corresponding + // BOM version using the version of the Spring Boot selected from the Config Bom's Array + if len(p.SnowdropBomVersion) == 0 { + p.SnowdropBomVersion = bom.Snowdrop } - if getUrlVal(r, "springbootversion") != "" { - p.SpringBootVersion = getUrlVal(r, "springbootversion") + + b, err := strconv.ParseBool(params.Get("supported")) + if err == nil { + p.UseSupported = b } - if getUrlVal(r, "outdir") != "" { - p.OutDir = getUrlVal(r, "outdir") + if p.UseSupported { + if len(bom.Supported) == 0 { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(fmt.Sprintf("%s is not a supported Spring Boot version", p.SpringBootVersion))) + return + } + p.SnowdropBomVersion = bom.Supported } - // If the snowdropbom version is not defined BUT only the Spring Boot Version, then get the corresponding - // BOM version using the version of the Spring Boot selected from the Config Bom's Array - if getUrlVal(r, "snowdropbom") == "" && getUrlVal(r, "springbootversion") != "" { - p.SnowdropBomVersion = scaffold.GetConfig().GetCorrespondingSnowDropBom(p.SpringBootVersion) + p.Template = params.Get("template") + p.GroupId = params.Get("groupid") + p.ArtifactId = params.Get("artifactid") + p.Version = params.Get("version") + p.PackageName = params.Get("packagename") + p.OutDir = params.Get("outdir") + + if len(params["module"]) > 0 { + p.Modules = asModuleArray(params["module"]) } // As dependencies and template selection can't be used together, we force the template to be equal to "custom" // when a user selects a different template. This is because we would like to avoid to populate a project with starters // which are incompatible or not fully tested with the template proposed - if len(getArrayVal(r, "module", params)) > 0 && p.Template != "custom" { + if len(params["module"]) > 0 && p.Template != "custom" { p.Template = "custom" }