Skip to content

Commit

Permalink
add append feature to stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
speier committed Dec 11, 2024
1 parent bee0fd8 commit dea3d93
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=v0.2.0
VERSION=v0.2.1

tag:
@git tag -a ${VERSION} -m "version ${VERSION}" && git push origin ${VERSION}
33 changes: 14 additions & 19 deletions internal/exec/tf/tfexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func (e *executor) Output(component *schema.Component) (map[string]*schema.Outpu
return output, nil
}

// utils

func prepareProvision(component *schema.Component, generateBackend bool) (string, error) {
varsfile := fmt.Sprintf(varsFileFmt, component.Stack.Name, component.Name)
err := writeJSON(component.Inputs, component.Path, varsfile)
Expand All @@ -147,21 +149,21 @@ func prepareProvision(component *schema.Component, generateBackend bool) (string
}

if generateBackend {
err := writeBackend(component)
err := writeBackendJSON(component)
if err != nil {
return "", err
}
}

err = writeProviders(component)
err = writeProvidersTF(component)
if err != nil {
return "", err
}

return varsfile, nil
}

func writeBackend(component *schema.Component) error {
func writeBackendJSON(component *schema.Component) error {
backend := map[string]interface{}{
"terraform": map[string]interface{}{
"backend": map[string]interface{}{
Expand All @@ -178,22 +180,18 @@ func writeBackend(component *schema.Component) error {
return nil
}

func writeProviderConfig(i int, pc map[string]interface{}) (string, []string) {
func writeProviderConfig(i int, pc map[string]interface{}) string {
sb := strings.Builder{}
data := []string{}

for k, v := range pc {
if strings.HasPrefix(k, "data_") {
data = append(data, fmt.Sprintf("%s", v))
continue
}
if k == "alias" && i > 2 {
continue
}
if m, ok := v.(map[string]interface{}); ok {
sb.WriteString(strings.Repeat(" ", i))
sb.WriteString(fmt.Sprintf("%s {", k))
sb.WriteString("\n")
pc, _ := writeProviderConfig(i+2, m)
pc := writeProviderConfig(i+2, m)
sb.WriteString(pc)
sb.WriteString(strings.Repeat(" ", i))
sb.WriteString("}\n")
Expand All @@ -211,33 +209,30 @@ func writeProviderConfig(i int, pc map[string]interface{}) (string, []string) {
}
sb.WriteString("\n")
}
return sb.String(), data

return sb.String()
}

func writeProviders(component *schema.Component) error {
func writeProvidersTF(component *schema.Component) error {
if len(component.Providers) == 0 {
return nil
}

var data []string
sb := strings.Builder{}
for k, v := range component.Providers {
sb.WriteString(fmt.Sprintf(`provider "%s" {`, k))
if m, ok := v.(map[string]interface{}); ok {
pc, pd := writeProviderConfig(2, m)
pc := writeProviderConfig(2, m)
if len(pc) > 0 {
sb.WriteString("\n")
sb.WriteString(pc)
}
if len(pd) > 0 {
data = append(data, pd...)
}
}
sb.WriteString("}\n\n")
}

for _, d := range data {
sb.WriteString(d)
for _, line := range component.Stack.Appends["providers"] {
sb.WriteString(line)
sb.WriteString("\n")
}

Expand Down
8 changes: 8 additions & 0 deletions internal/parser/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (vm *jsinterpreter) Parse(path string) (*schema.Stack, error) {
vm.rt.Set("stack", vm.registerStack(stack))
vm.rt.Set("backend", vm.registerBackend(stack))
vm.rt.Set("component", vm.registerComponent(stack))
vm.rt.Set("append", vm.registerAppend(stack))

src := result.OutputFiles[0].Contents
_, err := vm.rt.RunString(string(src))
Expand Down Expand Up @@ -138,6 +139,13 @@ func (vm *jsinterpreter) registerComponent(stack *schema.Stack) func(string, str
}
}

func (vm *jsinterpreter) registerAppend(stack *schema.Stack) func(string, []string) {
return func(t string, lines []string) {
log.Debug("register append", "type", t, "lines", lines, "stack", stack.Name)
stack.Appends[t] = lines
}
}

func (vm *jsinterpreter) getProxy(get func(property string) any) goja.Proxy {
obj := vm.rt.NewObject()
return vm.rt.NewProxy(obj, &goja.ProxyTrapConfig{
Expand Down
12 changes: 7 additions & 5 deletions internal/schema/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ const (

type (
Stack struct {
Path string `json:"path"`
Type string `json:"type"`
Name string `json:"name"`
Backend Backend `json:"backend"`
Components []*Component `json:"components"`
Path string `json:"path"`
Type string `json:"type"`
Name string `json:"name"`
Backend Backend `json:"backend"`
Components []*Component `json:"components"`
Appends map[string][]string `json:"appends"`
}

Stacks struct {
Expand All @@ -32,6 +33,7 @@ func NewStack(path string, t string) *Stack {
Path: path,
Type: t,
Components: make([]*Component, 0),
Appends: make(map[string][]string, 0),
}
}

Expand Down
6 changes: 2 additions & 4 deletions stacks/dev.stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ const gke = component('gke', 'test/modules/gke', {
}
})

const google = {
data_client_config: `data "google_client_config" "default" {}`
}
append('providers', [`data "google_client_config" "default" {}`])

const k8s = {
// alias: 'main',
Expand All @@ -46,7 +44,7 @@ const helm = {

const metsrv = component('metsrv', 'test/modules/kubernetes', {
providers: {
google: google,
// google: {},
kubernetes: k8s,
helm: helm
}
Expand Down

0 comments on commit dea3d93

Please sign in to comment.