Skip to content
This repository has been archived by the owner on Jun 6, 2023. It is now read-only.

Commit

Permalink
Make it possible to generate BOM with supported version. Simplify code.
Browse files Browse the repository at this point in the history
  • Loading branch information
metacosm committed Mar 6, 2019
1 parent 83037f1 commit be695f6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 50 deletions.
16 changes: 7 additions & 9 deletions pkg/scaffold/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down
79 changes: 38 additions & 41 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -75,26 +76,14 @@ 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})
}
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)
Expand Down Expand Up @@ -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"
}

Expand Down

0 comments on commit be695f6

Please sign in to comment.