Skip to content

Commit

Permalink
Add escape character
Browse files Browse the repository at this point in the history
  • Loading branch information
masipcat committed Feb 22, 2019
1 parent c508a43 commit ad76fdf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ endif
MAKEDIR:=$(strip $(shell dirname "$(realpath $(lastword $(MAKEFILE_LIST)))"))

gocode:
GO15VENDOREXPERIMENT=1 go install -ldflags "-X main.BuildVersion=${VERSION}" github.com/kopeio/kexpand
GO15VENDOREXPERIMENT=1 go install -ldflags "-X main.BuildVersion=${VERSION}" github.com/vinissimus/kexpand

gocode_docker:
GO15VENDOREXPERIMENT=1 CGO_ENABLED=0 GOOS=linux go install -ldflags "-s -X main.BuildVersion=${VERSION}" -a -installsuffix cgo github.com/kopeio/kexpand
GO15VENDOREXPERIMENT=1 CGO_ENABLED=0 GOOS=linux go install -ldflags "-s -X main.BuildVersion=${VERSION}" -a -installsuffix cgo github.com/vinissimus/kexpand

crossbuild-in-docker:
docker pull golang:${GOVERSION} # Keep golang image up to date
docker run --name=kexpand-build-${UNIQUE} -e STATIC_BUILD=yes -e VERSION=${VERSION} -v ${MAKEDIR}:/go/src/github.com/kopeio/kexpand golang:${GOVERSION} make -f /go/src/github.com/kopeio/kexpand/Makefile crossbuild
docker run --name=kexpand-build-${UNIQUE} -e STATIC_BUILD=yes -e VERSION=${VERSION} -v ${MAKEDIR}:/go/src/github.com/vinissimus/kexpand golang:${GOVERSION} make -f /go/src/github.com/vinissimus/kexpand/Makefile crossbuild
docker cp kexpand-build-${UNIQUE}:/go/.build .

crossbuild:
mkdir -p .build/dist/
GOOS=darwin GOARCH=amd64 go build -a ${EXTRA_BUILDFLAGS} -o .build/dist/darwin/amd64/kexpand -ldflags "${EXTRA_LDFLAGS} -X main.BuildVersion=${VERSION} -X main.GitVersion=${GITSHA}" github.com/kopeio/kexpand
GOOS=linux GOARCH=amd64 go build -a ${EXTRA_BUILDFLAGS} -o .build/dist/linux/amd64/kexpand -ldflags "${EXTRA_LDFLAGS} -X main.BuildVersion=${VERSION} -X main.GitVersion=${GITSHA}" github.com/kopeio/kexpand
GOOS=darwin GOARCH=amd64 go build -a ${EXTRA_BUILDFLAGS} -o .build/dist/darwin/amd64/kexpand -ldflags "${EXTRA_LDFLAGS} -X main.BuildVersion=${VERSION} -X main.GitVersion=${GITSHA}" github.com/vinissimus/kexpand
GOOS=linux GOARCH=amd64 go build -a ${EXTRA_BUILDFLAGS} -o .build/dist/linux/amd64/kexpand -ldflags "${EXTRA_LDFLAGS} -X main.BuildVersion=${VERSION} -X main.GitVersion=${GITSHA}" github.com/vinissimus/kexpand


kexpand-dist: crossbuild-in-docker
Expand Down Expand Up @@ -58,8 +58,8 @@ ci: images test govet

govet:
go vet \
github.com/kopeio/kexpand/cmd/...
github.com/vinissimus/kexpand/cmd/...

test:
go test github.com/kopeio/kexpand/cmd/...
go test github.com/vinissimus/kexpand/cmd/...

9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Build: `CGO_ENABLED=0 go build -a ${EXTRA_BUILDFLAGS} -o kexpand -ldflags "-w -extldflags '-static' -X main.BuildVersion=0.3 -X main.GitVersion=${GITSHA}" github.com/kopeio/kexpand`

(https://medium.com/@diogok/on-golang-static-binaries-cross-compiling-and-plugins-1aed33499671)

## Kexpand

kexpand is a tool for expanding Kubernetes placeholder variables into their actual values.
Expand Down Expand Up @@ -112,8 +116,11 @@ not recommended for use.
## Variable names
Variables names can include any alphanumeric character along with hypens(-), period(.), and underscores(_).

## Escape variables
Variables names starting with `\` will be ignored (`\$(HOME)` => `$(HOME)`)

## Multiple files
kexpand supprts passing multiple files and wildcards for templates to specify multiple files at once. kexpand will add `---`
kexpand supprts passing multiple files and wildcards for templates to specify multiple files at once. kexpand will add `---`
between each filename as required by kubectl.

`kexpand *.tmpl.yaml -f values.yaml`
Expand Down
16 changes: 10 additions & 6 deletions cmd/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ func (c *ExpandCmd) DoExpand(src []byte, values map[string]interface{}) ([]byte,
var err error

// All
expr := `\$(\({1,2})([[:alnum:]_\.\-]+)(\|[[:alnum:]]+)?\){1,2}|(\{{2})([[:alnum:]_\.\-]+)(\|[[:alnum:]]+)?\}{2}`
expr := `(\\?)(?:\$(\({1,2})([[:alnum:]_\.\-]+)(\|[[:alnum:]]+)?\){1,2}|(\{{2})([[:alnum:]_\.\-]+)(\|[[:alnum:]]+)?\}{2})`
re := regexp.MustCompile(expr)
expandFunction := func(match []byte) []byte {
re := regexp.MustCompile(expr)
//re := regexp.MustCompile(expr)

matchStr := string(match[:])
result := re.FindStringSubmatch(matchStr)
Expand All @@ -191,21 +191,25 @@ func (c *ExpandCmd) DoExpand(src []byte, values map[string]interface{}) ([]byte,
glog.Fatalf("Unexpected match: %q", matchStr)
}

if result[2] == "" && result[5] == "" {
if result[3] == "" && result[6] == "" {
glog.Fatalf("No variable defined within: %q", matchStr)
}

key := result[2] + result[5]
key := result[3] + result[6]
replacement := values[key]

if result[1] == `\` {
return []byte(match[1:])
}

if replacement == nil {
if c.IgnoreMissingKeys == false {
err = fmt.Errorf("Key not found: %q", key)
}
return match
}

pipeFunction := result[3] + result[6]
pipeFunction := result[4] + result[7]
if pipeFunction != "" {
if pipeFunction == "|base64" {
b, ok := replacement.([]byte)
Expand All @@ -229,7 +233,7 @@ func (c *ExpandCmd) DoExpand(src []byte, values map[string]interface{}) ([]byte,
}

var s string
delim := result[1] + result[4]
delim := result[2] + result[5]
switch len(delim) {
case 1:
s = fmt.Sprintf("\"%v\"", replacement)
Expand Down
4 changes: 4 additions & 0 deletions cmd/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func Test_Find(t *testing.T) {
"who": "world",
},
},
{
input: "hello \\$((who))",
expected: "hello $((who))",
},
{
input: "hello {{who}}",
expected: "hello world",
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"github.com/kopeio/kexpand/cmd"
"github.com/vinissimus/kexpand/cmd"
)

func main() {
Expand Down

0 comments on commit ad76fdf

Please sign in to comment.