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

Commit

Permalink
#13 support HOME alias "~"
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bogdanov committed Sep 19, 2015
1 parent 3c4048e commit b0cfdf8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/rocker/template/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ func ParseKvPairs(pairs []string) (vars Vars) {
}

func loadFileContent(f string) (string, error) {
if f == "~" || strings.HasPrefix(f, "~/") {
f = strings.Replace(f, "~", os.Getenv("HOME"), 1)
}
if !filepath.IsAbs(f) {
wd, err := os.Getwd()
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions src/rocker/template/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -215,4 +216,14 @@ func TestVarsFileContent(t *testing.T) {
}

assert.Equal(t, "@testdata/content.txt", result3["FOO"])

// Test HOME
os.Setenv("HOME", path.Join(wd, "testdata"))

result4, err := VarsFromStrings([]string{"FOO=@~/content.txt"})
if err != nil {
t.Fatal(err)
}

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

0 comments on commit b0cfdf8

Please sign in to comment.