-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement variables for aviator files
- Loading branch information
Showing
9 changed files
with
206 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package evaluator | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
var variableFormatRegex = regexp.MustCompile(`\(\(\s([-\w\p{L}]+)\s\)\)`) | ||
|
||
func Evaluate(aviatorFile []byte, vars map[string]string) ([]byte, error) { | ||
var err error | ||
return variableFormatRegex.ReplaceAllFunc(aviatorFile, func(match []byte) []byte { | ||
key := string(variableFormatRegex.FindSubmatch(match)[1]) | ||
|
||
val, ok := vars[key] | ||
if !ok { | ||
err = errors.New(fmt.Sprintf("Variable (( %s )) not provided", key)) | ||
} | ||
|
||
var replace []byte | ||
if strings.Contains(val, "\n") { | ||
replace, _ = json.Marshal(val) | ||
} else { | ||
replace = []byte(val) | ||
} | ||
|
||
return []byte(replace) | ||
}), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package evaluator_test | ||
|
||
import ( | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/JulzDiverse/aviator/evaluator" | ||
) | ||
|
||
var _ = Describe("Evaluate", func() { | ||
Context("When evaluating an aviator file variable", func() { | ||
|
||
var ( | ||
aviatorYaml string | ||
expectedYaml string | ||
evaluated []byte | ||
err error | ||
vars map[string]string | ||
) | ||
|
||
JustBeforeEach(func() { | ||
evaluated, err = Evaluate([]byte(aviatorYaml), vars) | ||
}) | ||
|
||
Context("When the varialbe key is an existing key", func() { | ||
Context("When the value is a usual value", func() { | ||
BeforeEach(func() { | ||
vars = map[string]string{ | ||
"base_yaml": "base.yml", | ||
} | ||
|
||
aviatorYaml = `--- | ||
spruce: | ||
- base: (( base_yaml )).old` | ||
|
||
expectedYaml = `--- | ||
spruce: | ||
- base: base.yml.old` | ||
}) | ||
|
||
It("should replace the key with it's provided value", func() { | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(strings.TrimSpace(string(evaluated))).To(Equal(expectedYaml)) | ||
}) | ||
}) | ||
|
||
Context("When the value is a mulit-line value", func() { | ||
BeforeEach(func() { | ||
vars = map[string]string{ | ||
"multi_line": `hello | ||
world`, | ||
} | ||
|
||
aviatorYaml = `--- | ||
fly: | ||
vars: | ||
key: (( multi_line ))` | ||
|
||
expectedYaml = `--- | ||
fly: | ||
vars: | ||
key: "hello\nworld"` | ||
|
||
}) | ||
|
||
It("should replace the key with it's provided value", func() { | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(strings.TrimSpace(string(evaluated))).To(Equal(expectedYaml)) | ||
}) | ||
}) | ||
}) | ||
|
||
Context("When the variable key is not existing", func() { | ||
BeforeEach(func() { | ||
aviatorYaml = `--- | ||
key: (( not_provided ))` | ||
}) | ||
|
||
It("should fail", func() { | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
|
||
It("should return a meaningful error message", func() { | ||
Expect(err).To(MatchError(ContainSubstring("Variable (( not_provided )) not provided"))) | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.