Skip to content

Commit

Permalink
Use regular expressions instead of os.Expand.
Browse files Browse the repository at this point in the history
  • Loading branch information
yawn committed Nov 23, 2014
1 parent 96a17ea commit 89a7740
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

Trivial templating for configuration files using environment keys:

1. Keys are defined as `${key}` or `$key` in various configuration files (glob patterns work)
* Definitions without a value result in an error
* envplate can optionally
1. Keys are defined as `${key}` in arbitrary configuration files (glob patterns work)
* Key definitions without a value in the environment results in an error
* The failure to resolve the supplied glob pattern(s) to at least one file results in an error
* `ep` can optionally
* create backups using the `-b` flag, appending a `.bak` extension to backup copies
* output to stdout instead of replacing values in files using the `-d` flag
* be verbose about it's operations by using the `-v` flag
Expand Down Expand Up @@ -40,8 +41,4 @@ RUN curl -sLo /usr/local/bin/ep https://github.com/yawn/envplate/releases/downlo
...
CMD [ "/usr/local/bin/ep", "-v", "/etc/nginx/nginx.conf", "--", "/usr/sbin/nginx", "-c", "/etc/nginx/conf" ]
```

## TODO

* (Maybe) drop the `$key` variable syntax (in favor of `${key}`-only)
```
6 changes: 5 additions & 1 deletion envplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
)

const (
NoKeyDefined = ""
)

var exp = regexp.MustCompile(`(\$\{\w+\})`)

func Apply(globs []string) {

matches := false
Expand Down Expand Up @@ -82,8 +85,9 @@ func parse(file string) error {

Log(DEBUG, "Parsing environment references in '%s'", file)

parsed := os.Expand(string(content), func(key string) string {
parsed := exp.ReplaceAllStringFunc(string(content), func(match string) string {

key := match[2 : len(match)-1]
value := os.Getenv(key)

Log(DEBUG, "Expanding reference to '%s' to value '%s'", key, value)
Expand Down
16 changes: 7 additions & 9 deletions envplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ func _read(t *testing.T, name string) string {

func _template(t *testing.T) (string, string) {

tpl := `
Database1=${DATABASE}
tpl := `Database1=${DATABASE}
Mode=${MODE}
Database2=${DATABASE}
`
Database3=$FOOBAR`

return _write(t, "parse.txt", tpl, 0644), tpl

Expand Down Expand Up @@ -124,20 +123,19 @@ func TestFullParse(t *testing.T) {

assert := assert.New(t)

file, tpl := _template(t)
file, _ := _template(t)
defer _delete(t, file)

backup := fmt.Sprintf("%s.bak", file)
fmt.Println(backup)

// time.Sleep(5 * time.Second)

err := parse(file)

assert.NoError(err)

assert.True(_exists(backup))
assert.NotEqual(tpl, _read(t, file), "content unchanged")
assert.Equal(`Database1=db.example.com
Mode=debug
Database2=db.example.com
Database3=$FOOBAR`, _read(t, file))

}

Expand Down

0 comments on commit 89a7740

Please sign in to comment.