diff --git a/Makefile b/Makefile index 8267474..e3dcdc6 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION = 1.1.0 +VERSION = 1.1.1 APP := subst PACKAGES := $(shell go list -f {{.Dir}} ./...) @@ -7,14 +7,13 @@ GOFILES := $(wildcard $(GOFILES)) .PHONY: clean release release-ci release-manual docker docker-latest README.md -clean: +clean: rm -rf binaries/ rm -rf release/ release-ci: README.md zip release: README.md - git reset git add README.md git add Makefile git commit -am "Release $(VERSION)" || true @@ -45,48 +44,48 @@ binaries: binaries/osx_x86_64/$(APP) binaries/windows_x86_64/$(APP).exe binaries release/$(APP)_$(VERSION)_osx_x86_64.zip: binaries/osx_x86_64/$(APP) mkdir -p release cd ./binaries/osx_x86_64 && zip -r -D ../../release/$(APP)_$(VERSION)_osx_x86_64.zip $(APP) - + binaries/osx_x86_64/$(APP): $(GOFILES) GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/osx_x86_64/$(APP) release/$(APP)_$(VERSION)_windows_x86_64.zip: binaries/windows_x86_64/$(APP).exe mkdir -p release cd ./binaries/windows_x86_64 && zip -r -D ../../release/$(APP)_$(VERSION)_windows_x86_64.zip $(APP).exe - + binaries/windows_x86_64/$(APP).exe: $(GOFILES) GOOS=windows GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/windows_x86_64/$(APP).exe release/$(APP)_$(VERSION)_linux_x86_64.zip: binaries/linux_x86_64/$(APP) mkdir -p release cd ./binaries/linux_x86_64 && zip -r -D ../../release/$(APP)_$(VERSION)_linux_x86_64.zip $(APP) - + binaries/linux_x86_64/$(APP): $(GOFILES) GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/linux_x86_64/$(APP) release/$(APP)_$(VERSION)_osx_x86_32.zip: binaries/osx_x86_32/$(APP) mkdir -p release cd ./binaries/osx_x86_32 && zip -r -D ../../release/$(APP)_$(VERSION)_osx_x86_32.zip $(APP) - + binaries/osx_x86_32/$(APP): $(GOFILES) GOOS=darwin GOARCH=386 go build -ldflags "-X main.version=$(VERSION)" -o binaries/osx_x86_32/$(APP) release/$(APP)_$(VERSION)_windows_x86_32.zip: binaries/windows_x86_32/$(APP).exe mkdir -p release cd ./binaries/windows_x86_32 && zip -r -D ../../release/$(APP)_$(VERSION)_windows_x86_32.zip $(APP).exe - + binaries/windows_x86_32/$(APP).exe: $(GOFILES) GOOS=windows GOARCH=386 go build -ldflags "-X main.version=$(VERSION)" -o binaries/windows_x86_32/$(APP).exe release/$(APP)_$(VERSION)_linux_x86_32.zip: binaries/linux_x86_32/$(APP) mkdir -p release cd ./binaries/linux_x86_32 && zip -r -D ../../release/$(APP)_$(VERSION)_linux_x86_32.zip $(APP) - + binaries/linux_x86_32/$(APP): $(GOFILES) GOOS=linux GOARCH=386 go build -ldflags "-X main.version=$(VERSION)" -o binaries/linux_x86_32/$(APP) release/$(APP)_$(VERSION)_linux_arm64.zip: binaries/linux_arm64/$(APP) mkdir -p release cd ./binaries/linux_arm64 && zip -r -D ../../release/$(APP)_$(VERSION)_linux_arm64.zip $(APP) - + binaries/linux_arm64/$(APP): $(GOFILES) GOOS=linux GOARCH=arm64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/linux_arm64/$(APP) diff --git a/README.md b/README.md index 06b5e15..92d1b53 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ - [Get it](#get-it) - [Use it](#use-it) - - [Examples](#examples) + - [Examples](#examples) - [Comments](#comments) @@ -21,20 +21,20 @@ Using go get: go get -u github.com/sgreben/subst ``` -Or [download the binary](https://github.com/sgreben/subst/releases/latest) from the releases page. +Or [download the binary](https://github.com/sgreben/subst/releases/latest) from the releases page. ```bash # Linux -curl -LO https://github.com/sgreben/subst/releases/download/1.1.0/subst_1.1.0_linux_x86_64.zip -unzip subst_1.1.0_linux_x86_64.zip +curl -LO https://github.com/sgreben/subst/releases/download/1.1.1/subst_1.1.1_linux_x86_64.zip +unzip subst_1.1.1_linux_x86_64.zip # OS X -curl -LO https://github.com/sgreben/subst/releases/download/1.1.0/subst_1.1.0_osx_x86_64.zip -unzip subst_1.1.0_osx_x86_64.zip +curl -LO https://github.com/sgreben/subst/releases/download/1.1.1/subst_1.1.1_osx_x86_64.zip +unzip subst_1.1.1_osx_x86_64.zip # Windows -curl -LO https://github.com/sgreben/subst/releases/download/1.1.0/subst_1.1.0_windows_x86_64.zip -unzip subst_1.1.0_windows_x86_64.zip +curl -LO https://github.com/sgreben/subst/releases/download/1.1.1/subst_1.1.1_windows_x86_64.zip +unzip subst_1.1.1_windows_x86_64.zip ``` ## Use it @@ -64,17 +64,17 @@ comment: $COMMENT $ cat template.yaml | subst -q NAME=jp URL=github.com/sgreben/jp COMMENT= name: jp url: github.com/sgreben/jp -comment: +comment: -$ cat template.yaml | subst -q NAME=jp +$ cat template.yaml | subst -q NAME=jp name: jp url: $URL comment: $COMMENT -$ cat template.yaml | subst -q -unknown=empty NAME=jp +$ cat template.yaml | subst -q -unknown=empty NAME=jp name: jp -url: -comment: +url: +comment: ``` Without the `-q` flag, `subst` also prints to stderr: @@ -98,7 +98,7 @@ To ignore occurrences of defined variables, you can define an escape string usin ```shell $ echo 'unescaped: $X, ${X}. escaped: \$X, \${X}.' | subst -escape '\' X=value -unescaped: value, value. escaped: \$X, \${X}. +unescaped: value, value. escaped: $X, ${X}. ``` ## Comments diff --git a/README.template.md b/README.template.md index a03017a..e016916 100644 --- a/README.template.md +++ b/README.template.md @@ -8,7 +8,7 @@ - [Get it](#get-it) - [Use it](#use-it) - - [Examples](#examples) + - [Examples](#examples) - [Comments](#comments) @@ -21,7 +21,7 @@ Using go get: go get -u github.com/sgreben/subst ``` -Or [download the binary](https://github.com/sgreben/subst/releases/latest) from the releases page. +Or [download the binary](https://github.com/sgreben/subst/releases/latest) from the releases page. ```bash # Linux @@ -64,17 +64,17 @@ comment: $COMMENT $ cat template.yaml | subst -q NAME=jp URL=github.com/sgreben/jp COMMENT= name: jp url: github.com/sgreben/jp -comment: +comment: -$ cat template.yaml | subst -q NAME=jp +$ cat template.yaml | subst -q NAME=jp name: jp url: $URL comment: $COMMENT -$ cat template.yaml | subst -q -unknown=empty NAME=jp +$ cat template.yaml | subst -q -unknown=empty NAME=jp name: jp -url: -comment: +url: +comment: ``` Without the `-q` flag, `subst` also prints to stderr: @@ -98,9 +98,9 @@ To ignore occurrences of defined variables, you can define an escape string usin ```shell $ echo 'unescaped: $X, ${X}. escaped: \$X, \${X}.' | subst -escape '\' X=value -unescaped: value, value. escaped: \$X, \${X}. +unescaped: value, value. escaped: $X, ${X}. ``` ## Comments -Feel free to [leave a comment](https://github.com/sgreben/subst/issues/1) or create an issue. \ No newline at end of file +Feel free to [leave a comment](https://github.com/sgreben/subst/issues/1) or create an issue. diff --git a/expand.go b/expand.go index b763f63..055c28d 100644 --- a/expand.go +++ b/expand.go @@ -42,14 +42,18 @@ func expand(s, escape string, mapping func(string) string) string { for j := 0; j < len(s); j++ { if shouldEscape { if j+len(escaped) < len(s) && s[j:j+len(escaped)] == escaped { - j += len(escaped) + buf = append(buf, s[i:j]...) + buf = append(buf, s[j+len(escape):j+len(escaped)]...) + i = j + len(escaped) + j = i continue } } if s[j] == '$' && j+1 < len(s) { buf = append(buf, s[i:j]...) name, w := variableName(s[j+1:]) - buf = append(buf, mapping(name)...) + value := mapping(name) + buf = append(buf, value...) j += w i = j + 1 }