config: add concat function [GH-191]

This commit is contained in:
Mitchell Hashimoto 2014-08-19 13:14:45 -07:00
parent 7ebd3c0a48
commit 2fb001dcf9
3 changed files with 59 additions and 0 deletions

View File

@ -9,6 +9,11 @@ BACKWARDS INCOMPATIBILITIES:
* Keys cannot be double-quoted strings: `"foo" = "bar"` is no longer
valid.
FEATURES:
* **New Function: `concat`**: Concatenate multiple strings together.
Example: `concat(var.region, "-", var.channel)`.
IMPROVEMENTS:
* core: "~/.terraformrc" (Unix) or "%APPDATA%/terraform.rc" (Windows)

View File

@ -1,6 +1,7 @@
package config
import (
"bytes"
"fmt"
"io/ioutil"
"strings"
@ -11,11 +12,27 @@ var Funcs map[string]InterpolationFunc
func init() {
Funcs = map[string]InterpolationFunc{
"concat": interpolationFuncConcat,
"file": interpolationFuncFile,
"lookup": interpolationFuncLookup,
}
}
// interpolationFuncConcat implements the "concat" function that allows
// strings to be joined together.
func interpolationFuncConcat(
vs map[string]string, args ...string) (string, error) {
var buf bytes.Buffer
for _, a := range args {
if _, err := buf.WriteString(a); err != nil {
return "", err
}
}
return buf.String(), nil
}
// interpolationFuncFile implements the "file" function that allows
// loading contents from a file.
func interpolationFuncFile(

View File

@ -6,6 +6,43 @@ import (
"testing"
)
func TestInterpolateFuncConcat(t *testing.T) {
cases := []struct {
Args []string
Result string
Error bool
}{
{
[]string{"foo", "bar", "baz"},
"foobarbaz",
false,
},
{
[]string{"foo", "bar"},
"foobar",
false,
},
{
[]string{"foo"},
"foo",
false,
},
}
for i, tc := range cases {
actual, err := interpolationFuncConcat(nil, tc.Args...)
if (err != nil) != tc.Error {
t.Fatalf("%d: err: %s", i, err)
}
if actual != tc.Result {
t.Fatalf("%d: bad: %#v", i, actual)
}
}
}
func TestInterpolateFuncFile(t *testing.T) {
tf, err := ioutil.TempFile("", "tf")
if err != nil {