Skip to content

Commit

Permalink
feat: predefined variables working (that was easy, lol)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Aug 14, 2022
1 parent 9900a05 commit 08bf5ce
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 51 deletions.
2 changes: 1 addition & 1 deletion examples/terraform/compiled/dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
REPLACED
rg-dev-terraform-example

something: https://gitlab.example.com/1234
something: https://gitlab.example.com/REPLACED

foo-bar-baz

Expand Down
4 changes: 3 additions & 1 deletion examples/terraform/inventory/classes/gitlab.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ gitlab:
something: ${gitlab:base_url}/${gitlab:project_id}
another: ${gitlab:base_url}/something/else
evenMore: ${gitlab:project_id}
#test: ${target_name}
foo:
- ${gitlab:pizza:3}
- ${gitlab:pizza:0}
- baz
pizza:
- ${target_name}
- this
- is
- an
Expand Down
3 changes: 2 additions & 1 deletion examples/terraform/inventory/targets/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target:
name: "dev"

gitlab:
foo: bar
baz: bar
project_id: REPLACED

azure:
Expand All @@ -22,3 +22,4 @@ target:

terraform:
state_name: "dev-state"
gitlab: ${gitlab:base_url}
4 changes: 4 additions & 0 deletions examples/terraform/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path"

"github.com/spf13/afero"
"gopkg.in/yaml.v3"

"github.com/lukasjarosch/skipper"
)
Expand Down Expand Up @@ -76,6 +77,9 @@ func main() {
panic(err)
}

out, _ := yaml.Marshal(data)
log.Println(string(out))

// pretend that we've got some other data source
additional := map[string]any{
"something": "else",
Expand Down
75 changes: 30 additions & 45 deletions inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,6 @@ func (inv *Inventory) Load(classPath, targetPath string) error {
}
}

// TODO replace variables in targets
// TODO replace pre-defined variables (e.g. ${target_name})
// TODO enable custom variable definition inside classes

// replace all variables with the required value
/*
for _, class := range inv.classFiles {
// Determine which variables exist in the Data map
variables := FindVariables(class.Data())
if len(variables) == 0 {
continue
}
for _, variable := range variables {
// sourceValue is the value on which the variable points to.
// This is the value we need to replace the variable with
targetValue, err := class.Data().GetPath(variable.NameAsIdentifier()...)
if err != nil {
return err
}
// targetValue is the value where the variable is. It needs to be replaced with an actual value
sourceValue, err := class.Data().GetPath(variable.Identifier...)
if err != nil {
return err
}
// Replace the full variable name (${variable}) with the targetValue
sourceValue = strings.ReplaceAll(fmt.Sprint(sourceValue), variable.FullName(), fmt.Sprint(targetValue))
class.Data().SetPath(sourceValue, variable.Identifier...)
}
}
*/

return nil
}

Expand Down Expand Up @@ -149,7 +112,11 @@ func (inv *Inventory) Data(targetName string) (data Data, err error) {
// TODO: what if a class defines the 'target' key?
data[targetKey] = targetData

err = inv.replaceVariables(data)
predefinedVariables := map[string]interface{}{
"target_name": targetName,
}

err = inv.replaceVariables(data, predefinedVariables)
if err != nil {
return nil, err
}
Expand All @@ -158,24 +125,42 @@ func (inv *Inventory) Data(targetName string) (data Data, err error) {
}

// replaceVariables iterates over the given Data map and replaces all variables with the required value.
func (inv *Inventory) replaceVariables(data Data) error {
// TODO replace pre-defined variables (e.g. ${target_name})
// TODO enable custom variable definition inside classes
func (inv *Inventory) replaceVariables(data Data, predefinedVariables map[string]interface{}) (err error) {

// Determine which variables exist in the Data map
variables := FindVariables(data)

if len(variables) == 0 {
return nil
}

isPredefinedVariable := func(variable Variable) bool {
for name := range predefinedVariables {
if strings.EqualFold(variable.Name, name) {
return true
}
}
return false
}

for _, variable := range variables {

// sourceValue is the value on which the variable points to.
// This is the value we need to replace the variable with
targetValue, err := data.GetPath(variable.NameAsIdentifier()...)
if err != nil {
return err
var targetValue interface{}

if isPredefinedVariable(variable) {
targetValue = predefinedVariables[variable.Name]
} else {
// targetValue is the value on which the variable points to.
// This is the value we need to replace the variable with
targetValue, err = data.GetPath(variable.NameAsIdentifier()...)
if err != nil {
return err
}
}

// targetValue is the value where the variable is. It needs to be replaced with an actual value
// sourceValue is the value where the variable is. It needs to be replaced with an actual value
sourceValue, err := data.GetPath(variable.Identifier...)
if err != nil {
return err
Expand Down
8 changes: 5 additions & 3 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ var variableRegex = regexp.MustCompile(`\$\{((\w*)(\:\w+)*)\}`)
// Variable is a keyword which self-references the Data map it is defined in.
// A Variable has the form ${key:key}.
type Variable struct {
// Name is the ':' separated identifier which points to the value, we consider this the name of the variable.
// The reason we use ':' is to improve readability between curly braces.
// Name of the variable is whatever string is between ${}.
// + For dynamic variables, this can be a ':' separated string which points somewhere into the Data map.
// The reason we use ':' is to improve readability between curly braces.
// + For predefined variables, this can be any string and must not be a path into the Data map.
Name string
// Identifier is the list of keys which point to the variable itself within the data set it is defined.
// Identifier is the list of keys which point to the variable itself within the data set in which it is used.
Identifier []interface{}
}

Expand Down

0 comments on commit 08bf5ce

Please sign in to comment.