Skip to content

Commit

Permalink
[ORC-542] env template first iteration (#134)
Browse files Browse the repository at this point in the history
* env template first iteration

* Add unit tests
  • Loading branch information
chrisghill committed Jul 3, 2024
1 parent 71cb349 commit 810a10a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 26 deletions.
9 changes: 3 additions & 6 deletions cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"os"
"sort"
"strings"

"github.com/massdriver-cloud/mass/docs/helpdocs"
Expand Down Expand Up @@ -208,17 +207,15 @@ func runBundleNew(input *bundleNew) {
log.Fatal(err)
}

var artifacts []string
artifactDefinitions := map[string]map[string]interface{}{}
for _, v := range artifactDefs {
if _, ok := hiddenArtifacts[v.Name]; ok {
continue
}
artifacts = append(artifacts, v.Name)
artifactDefinitions[v.Name] = v.Schema
}

sort.StringSlice(artifacts).Sort()

bundle.SetMassdriverArtifactDefinitions(artifacts)
bundle.SetMassdriverArtifactDefinitions(artifactDefinitions)

var templateData *templatecache.TemplateData
if input.name == "" || input.templateName == "" {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/massdriver-cloud/mass

go 1.22.0

replace github.com/osteele/liquid => github.com/massdriver-cloud/liquid v1.4.0

Check failure on line 5 in go.mod

View workflow job for this annotation

GitHub Actions / lint

replacement are not allowed: github.com/osteele/liquid (gomoddirectives)

require (
github.com/AlecAivazis/survey/v2 v2.3.6
github.com/BurntSushi/toml v1.2.1
Expand All @@ -19,10 +21,10 @@ require (
github.com/itchyny/gojq v0.12.12
github.com/manifoldco/promptui v0.9.0
github.com/massdriver-cloud/airlock v0.0.2
github.com/massdriver-cloud/liquid v1.2.4
github.com/massdriver-cloud/schema2json v0.0.2
github.com/moby/moby v24.0.8+incompatible
github.com/moby/term v0.0.0-20221205130635-1aeaba878587
github.com/osteele/liquid v1.4.0
github.com/rs/zerolog v1.29.0
github.com/sethvargo/go-envconfig v0.9.0
github.com/spf13/afero v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYt
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/massdriver-cloud/airlock v0.0.2 h1:sXWMgsLuZySLg0w/OYMpgi7HSjIsgsbWWf9mcLnfLH0=
github.com/massdriver-cloud/airlock v0.0.2/go.mod h1:lOiYJFY6PkJsBFha99xxqLuy5HiCureZEwQrnU6+MbA=
github.com/massdriver-cloud/liquid v1.2.4 h1:gLGL3yq7e9oY15WZKFBs8j2Q3LOG/SfS0LmBHnSvNA4=
github.com/massdriver-cloud/liquid v1.2.4/go.mod h1:lHQ8awCExKKWnko2axSyC2/ONv5btPOjhDZyQ0ND2PE=
github.com/massdriver-cloud/liquid v1.4.0 h1:807qhdLOHFbbIG7huhxB1bZRt/qOZWuHhhhjuA/bVjc=
github.com/massdriver-cloud/liquid v1.4.0/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak=
github.com/massdriver-cloud/schema2json v0.0.2 h1:ohcx8233BJ6sKBYUKh7gdcbbJ2ASSdkjE81fUUIFfgk=
github.com/massdriver-cloud/schema2json v0.0.2/go.mod h1:EwuRNUGTQsq0KFfUoAFgdmUwcKWzNv5y1n49sOylBK0=
github.com/matryer/moq v0.2.3/go.mod h1:9RtPYjTnH1bSBIkpvtHkFN7nbWAnO7oRpdJkEIn6UtE=
Expand Down
44 changes: 37 additions & 7 deletions pkg/bundle/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package bundle
import (
"errors"
"fmt"
"maps"
"regexp"
"sort"
"strings"

"github.com/AlecAivazis/survey/v2"
Expand All @@ -23,7 +25,7 @@ var (
connNameError = fmt.Sprintf(baseNameError, "underscores", "_", "_", "_")
)

var massdriverArtifactDefinitions []string
var massdriverArtifactDefinitions map[string]map[string]interface{}

var promptsNew = []func(t *templatecache.TemplateData) error{
getName,
Expand All @@ -34,7 +36,7 @@ var promptsNew = []func(t *templatecache.TemplateData) error{
}

// SetMassdriverArtifactDefinitions sets the defs used to specify connections in a bundle
func SetMassdriverArtifactDefinitions(in []string) {
func SetMassdriverArtifactDefinitions(in map[string]map[string]interface{}) {
massdriverArtifactDefinitions = in
}

Expand Down Expand Up @@ -151,10 +153,17 @@ func connNameValidate(name string) error {
func GetConnections(t *templatecache.TemplateData) error {
none := "(None)"

artifactDefinitionsTypes := []string{}
// in 1.23 we can use maps.Keys(), but until then we'll extract the keys manually
for adt := range massdriverArtifactDefinitions {
artifactDefinitionsTypes = append(artifactDefinitionsTypes, adt)
}
sort.StringSlice(artifactDefinitionsTypes).Sort()

var selectedDeps []string
multiselect := &survey.MultiSelect{
Message: "What connections do you need?\n If you don't need any, just hit enter or select (None)\n",
Options: append([]string{none}, massdriverArtifactDefinitions...),
Options: artifactDefinitionsTypes,
}

err := survey.AskOne(multiselect, &selectedDeps)
Expand All @@ -163,16 +172,17 @@ func GetConnections(t *templatecache.TemplateData) error {
}

var depMap []templatecache.Connection
envs := map[string]string{}

for i, v := range selectedDeps {
if v == none {
for _, currentDep := range selectedDeps {
if currentDep == none {
if len(selectedDeps) > 1 {
return fmt.Errorf("if selecting %v, you cannot select other dependecies. selected %#v", none, selectedDeps)
}
return nil
}

fmt.Printf("Please enter a name for the connection: \"%v\"\nThis will be the variable name used to reference it in your app|bundle IaC\n", v)
fmt.Printf("Please enter a name for the connection: \"%v\"\nThis will be the variable name used to reference it in your app|bundle IaC\n", currentDep)

prompt := promptui.Prompt{
Label: `Name`,
Expand All @@ -184,10 +194,13 @@ func GetConnections(t *templatecache.TemplateData) error {
return errName
}

depMap = append(depMap, templatecache.Connection{Name: result, ArtifactDefinition: selectedDeps[i]})
depMap = append(depMap, templatecache.Connection{Name: result, ArtifactDefinition: currentDep})

maps.Copy(envs, GetConnectionEnvs(result, massdriverArtifactDefinitions[currentDep]))
}

t.Connections = depMap
t.Envs = envs
return nil
}

Expand Down Expand Up @@ -228,3 +241,20 @@ func getExistingParamsPath(in string) (string, error) {

return prompt.Run()
}

func GetConnectionEnvs(connectionName string, artifactDefinition map[string]interface{}) map[string]string {
envs := map[string]string{}

mdBlock, mdBlockExists := artifactDefinition["$md"]
if mdBlockExists {
envsBlock, envsBlockExists := mdBlock.(map[string]interface{})["envTemplates"]
if envsBlockExists {
for envName, value := range envsBlock.(map[string]interface{}) {
envValue := value.(string)

Check failure on line 253 in pkg/bundle/prompt.go

View workflow job for this annotation

GitHub Actions / lint

Error return value is not checked (errcheck)
envs[envName] = strings.ReplaceAll(envValue, "connection_name", connectionName)
}
}
}

return envs
}
45 changes: 45 additions & 0 deletions pkg/bundle/prompt_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bundle

import (
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -56,3 +57,47 @@ func TestConnNameValidate(t *testing.T) {
}
}
}

func TestGetConnectionEnvs(t *testing.T) {
type test struct {
name string
connectionName string
artifactDefinition map[string]interface{}
want map[string]string
}
tests := []test{
{
name: "Basic",
connectionName: "foobar",
artifactDefinition: map[string]interface{}{
"$md": map[string]interface{}{
"envTemplates": map[string]interface{}{
"SOME_ENV": ".connection_name.data.foo.bar",
"ANOTHER_ENV": "lol | split() | .connection_name | abc",
},
},
},
want: map[string]string{
"SOME_ENV": ".foobar.data.foo.bar",
"ANOTHER_ENV": "lol | split() | .foobar | abc",
},
},
{
name: "Empty",
connectionName: "foobar",
artifactDefinition: map[string]interface{}{
"$md": map[string]interface{}{},
},
want: map[string]string{},
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := GetConnectionEnvs(tc.connectionName, tc.artifactDefinition)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("got %v, want %v", got, tc.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/templatecache/file_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/massdriver-cloud/liquid"
"github.com/osteele/liquid"
"github.com/spf13/afero"
)

Expand Down
19 changes: 10 additions & 9 deletions pkg/templatecache/template_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ type TemplateCache interface {
}

type TemplateData struct {
Name string `json:"name"`
Description string `json:"description"`
Access string `json:"access"`
Location string `json:"location"`
TemplateName string `json:"templateName"`
TemplateRepo string `json:"templateRepo"`
OutputDir string `json:"outputDir"`
Type string `json:"type"`
Connections []Connection `json:"connections"`
Name string `json:"name"`
Description string `json:"description"`
Access string `json:"access"`
Location string `json:"location"`
TemplateName string `json:"templateName"`
TemplateRepo string `json:"templateRepo"`
OutputDir string `json:"outputDir"`
Type string `json:"type"`
Connections []Connection `json:"connections"`
Envs map[string]string `json:"envs"`

// ParamsSchema is a YAML formatted string
ParamsSchema string `json:"paramsSchema"`
Expand Down

0 comments on commit 810a10a

Please sign in to comment.