-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marcel Ludwig
committed
Sep 29, 2017
1 parent
9b9ee8c
commit 8749eee
Showing
4 changed files
with
88 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,59 @@ | ||
package temgo | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type EnvVars map[string]string | ||
|
||
// following the posix standards | ||
var templatePattern = regexp.MustCompile(`\{{2} ([a-zA-Z_]+[a-zA-Z_0-9]*) }{2}`) | ||
type Temgo struct { | ||
envVars EnvVars | ||
pattern *regexp.Regexp | ||
strict bool | ||
} | ||
|
||
func New(envs EnvVars, strict bool) *Temgo { | ||
return &Temgo{ | ||
envVars: envs, | ||
// following the posix standards | ||
pattern: regexp.MustCompile(`{{2} ([a-zA-Z_]+[a-zA-Z_0-9]*) }{2}`), | ||
strict: strict, | ||
} | ||
} | ||
|
||
func ContainsVariable(str []byte) bool { | ||
return templatePattern.Match(str) | ||
func (t *Temgo) ContainsVariable(str []byte) bool { | ||
return t.pattern.Match(str) | ||
} | ||
|
||
func replace(e *EnvVars, bytes []byte) []byte { | ||
list := templatePattern.FindAllStringSubmatch(string(bytes), -1) | ||
func (t *Temgo) replace(bytes []byte) (result []byte, errList []string) { | ||
result = bytes | ||
list := t.pattern.FindAllStringSubmatch(string(bytes), -1) | ||
for _, match := range list { | ||
str, ok := (*e)[match[1]] | ||
str, ok := t.envVars[match[1]] | ||
if !ok { | ||
continue // variable not set | ||
if t.strict { | ||
errList = append(errList, "variable "+match[1]+" is not set") | ||
} | ||
continue | ||
} | ||
bytes = []byte(strings.Replace(string(bytes), match[0], str, -1)) | ||
result = []byte(strings.Replace(string(result), match[0], str, -1)) | ||
} | ||
return bytes | ||
return result, errList | ||
} | ||
|
||
func (e *EnvVars) ReplaceVariables(str []byte) []byte { | ||
result := templatePattern.ReplaceAllFunc(str, func(b []byte) []byte { | ||
return replace(e, b) | ||
func (t *Temgo) ReplaceVariables(str []byte) ([]byte, error) { | ||
var errList []string | ||
result := t.pattern.ReplaceAllFunc(str, func(b []byte) []byte { | ||
r, err := t.replace(b) | ||
if err != nil { | ||
errList = append(errList, err...) | ||
} | ||
return r | ||
}) | ||
return result | ||
if len(errList) > 0 { | ||
return nil, errors.New(strings.Join(errList, "\n")) | ||
} | ||
return result, nil | ||
} |
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