Skip to content

Commit

Permalink
Default values for scope, instance_size_slug, instance_count and kind…
Browse files Browse the repository at this point in the history
… fields (#13)

* Add more default values

* Remove default value for region
  • Loading branch information
renehernandez authored Nov 27, 2020
1 parent c4eece0 commit 6cb1a7f
Showing 1 changed file with 59 additions and 14 deletions.
73 changes: 59 additions & 14 deletions internal/apps/app_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,75 @@ package apps

import (
"github.com/digitalocean/godo"
"github.com/renehernandez/appfile/internal/log"
)

type AppSpec struct {
godo.AppSpec
}

func (spec *AppSpec) SetDefaultValues() {
if len(spec.StaticSites) > 0 {
for _, siteSpec := range spec.StaticSites {
if len(siteSpec.Routes) == 0 {
siteSpec.Routes = append(siteSpec.Routes, &godo.AppRouteSpec{
Path: "/",
})
}
log.Debugf("Setting default values for %s spec", spec.Name)
for _, siteSpec := range spec.StaticSites {
spec.setEnvVarsDefaults(siteSpec.Envs)
if len(siteSpec.Routes) == 0 {
siteSpec.Routes = append(siteSpec.Routes, &godo.AppRouteSpec{
Path: "/",
})
}
}

if len(spec.Services) > 0 {
for _, svcSpec := range spec.Services {
if len(svcSpec.InternalPorts) == 0 && len(svcSpec.Routes) == 0 {
svcSpec.Routes = append(svcSpec.Routes, &godo.AppRouteSpec{
Path: "/",
})
}
for _, svcSpec := range spec.Services {
spec.setEnvVarsDefaults(svcSpec.Envs)

if svcSpec.InstanceCount == 0 {
svcSpec.InstanceCount = 1
}

if svcSpec.InstanceSizeSlug == "" {
svcSpec.InstanceSizeSlug = "basic-xxs"
}

if len(svcSpec.InternalPorts) == 0 && len(svcSpec.Routes) == 0 {
svcSpec.Routes = append(svcSpec.Routes, &godo.AppRouteSpec{
Path: "/",
})
}
}

for _, jobSpec := range spec.Jobs {
spec.setEnvVarsDefaults(jobSpec.Envs)

if jobSpec.Kind == "" {
jobSpec.Kind = "POST_DEPLOY"
}

if jobSpec.InstanceCount == 0 {
jobSpec.InstanceCount = 1
}

if jobSpec.InstanceSizeSlug == "" {
jobSpec.InstanceSizeSlug = "basic-xxs"
}
}

for _, workerSpec := range spec.Workers {
spec.setEnvVarsDefaults(workerSpec.Envs)

if workerSpec.InstanceCount == 0 {
workerSpec.InstanceCount = 1
}

if workerSpec.InstanceSizeSlug == "" {
workerSpec.InstanceSizeSlug = "basic-xxs"
}
}
}

func (spec *AppSpec) setEnvVarsDefaults(envs []*godo.AppVariableDefinition) {
for _, envVar := range envs {
if envVar.Scope == "" {
envVar.Scope = "RUN_AND_BUILD_TIME"
}
}
}

0 comments on commit 6cb1a7f

Please sign in to comment.