-
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.
Merge pull request #5 from sevenval/feature/strict-mode
Feature/strict mode
- Loading branch information
Showing
41 changed files
with
8,152 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
|
||
|
||
[[constraint]] | ||
name = "github.com/google/go-cmp" | ||
version = "0.1.0" |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.