Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
vendor update rocker/template
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bogdanov committed Sep 30, 2015
1 parent 42a2128 commit 84ae7d6
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 8 deletions.
2 changes: 1 addition & 1 deletion vendor/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
{
"importpath": "github.com/grammarly/rocker/src/rocker/template",
"repository": "https://github.com/grammarly/rocker",
"revision": "b4e13649def6e2852db73477f6cf272b76d47e53",
"revision": "15024281d69f1e7a1814204e60b3ac8c771691ce",
"branch": "master",
"path": "/src/rocker/template"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ func Process(name string, reader io.Reader, vars Vars, funcs map[string]interfac
return nil, fmt.Errorf("Error reading template %s, error: %s", name, err)
}

// Copy the vars struct because we don't want to modify the original struct
vars = Vars{}.Merge(vars)

// merge OS environment variables with the given Vars map
// todo: maybe, we need to make it configurable
vars["Env"] = ParseKvPairs(os.Environ())
Expand All @@ -64,7 +67,7 @@ func Process(name string, reader io.Reader, vars Vars, funcs map[string]interfac
"equalFold": strings.EqualFold,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix,
"index": strings.Index,
"indexOf": strings.Index,
"indexAny": strings.IndexAny,
"join": strings.Join,
"lastIndex": strings.LastIndex,
Expand Down
60 changes: 54 additions & 6 deletions vendor/src/github.com/grammarly/rocker/src/rocker/template/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"regexp"
"sort"
"strings"

"github.com/go-yaml/yaml"
)

// Vars describes the data structure of the build variables
Expand Down Expand Up @@ -113,6 +115,45 @@ func VarsFromStrings(pairs []string) (vars Vars, err error) {
return vars, nil
}

// VarsFromFile reads variables from either JSON or YAML file
func VarsFromFile(filename string) (vars Vars, err error) {

if filename, err = resolveFileName(filename); err != nil {
return nil, err
}

data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}

vars = Vars{}

switch filepath.Ext(filename) {
case ".yaml", ".yml", ".":
if err := yaml.Unmarshal(data, &vars); err != nil {
return nil, err
}
case ".json":
if err := json.Unmarshal(data, &vars); err != nil {
return nil, err
}
}

return vars, nil
}

// VarsFromFileMulti reads multiple files and merge vars
func VarsFromFileMulti(files []string) (vars Vars, err error) {
varsList := make([]Vars, len(files))
for i, f := range files {
if varsList[i], err = VarsFromFile(f); err != nil {
return nil, err
}
}
return Vars{}.Merge(varsList...), nil
}

// ParseKvPairs parses Vars from a slice of strings e.g. []string{"KEY=VALUE"}
func ParseKvPairs(pairs []string) (vars Vars) {
vars = make(Vars)
Expand All @@ -123,7 +164,18 @@ func ParseKvPairs(pairs []string) (vars Vars) {
return vars
}

func loadFileContent(f string) (string, error) {
func loadFileContent(f string) (content string, err error) {
if f, err = resolveFileName(f); err != nil {
return "", err
}
data, err := ioutil.ReadFile(f)
if err != nil {
return "", err
}
return string(data), nil
}

func resolveFileName(f string) (string, error) {
if f == "~" || strings.HasPrefix(f, "~/") {
f = strings.Replace(f, "~", os.Getenv("HOME"), 1)
}
Expand All @@ -134,11 +186,7 @@ func loadFileContent(f string) (string, error) {
}
f = path.Join(wd, f)
}
data, err := ioutil.ReadFile(f)
if err != nil {
return "", err
}
return string(data), nil
return f, nil
}

// Code borrowed from https://github.com/docker/docker/blob/df0e0c76831bed08cf5e08ac9a1abebf6739da23/builder/support.go
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package template
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"rocker/test"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -98,6 +100,41 @@ func TestVarsFromStrings(t *testing.T) {
}
}

func TestVarsFromFile_Yaml(t *testing.T) {
tempDir, rm := tplMkFiles(t, map[string]string{
"vars.yml": `
Foo: x
Bar: yes
`,
})
defer rm()

vars, err := VarsFromFile(tempDir + "/vars.yml")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, "x", vars["Foo"])
assert.Equal(t, true, vars["Bar"])
}

func TestVarsFromFile_Json(t *testing.T) {
tempDir, rm := tplMkFiles(t, map[string]string{
"vars.json": `
{"Foo": "x", "Bar": true}
`,
})
defer rm()

vars, err := VarsFromFile(tempDir + "/vars.json")
if err != nil {
t.Fatal(err)
}

assert.Equal(t, "x", vars["Foo"])
assert.Equal(t, true, vars["Bar"])
}

func TestVarsReplaceString(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -227,3 +264,19 @@ func TestVarsFileContent(t *testing.T) {

assert.Equal(t, "hello\n", result4["FOO"])
}

func tplMkFiles(t *testing.T, files map[string]string) (string, func()) {
tempDir, err := ioutil.TempDir("", "rocker-vars-test")
if err != nil {
t.Fatal(err)
}

if err = test.MakeFiles(tempDir, files); err != nil {
os.RemoveAll(tempDir)
t.Fatal(err)
}

return tempDir, func() {
os.RemoveAll(tempDir)
}
}

0 comments on commit 84ae7d6

Please sign in to comment.