From 262d50bb20d0cd9fbe65f35615a8b1d1f70b24de Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Sun, 17 May 2015 14:58:16 -0500 Subject: [PATCH 1/7] testing docs --- .gitignore | 1 + docs/index.md | 17 +++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 19 insertions(+) create mode 100644 docs/index.md create mode 100644 mkdocs.yml diff --git a/.gitignore b/.gitignore index 378eac2..ee102b3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ build +site diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..fc71ab8 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,17 @@ +# Welcome to Sigil + +For full documentation visit [mkdocs.org](http://mkdocs.org). + +## Commands + +* `mkdocs new [dir-name]` - Create a new project. +* `mkdocs serve` - Start the live-reloading docs server. +* `mkdocs build` - Build the documentation site. +* `mkdocs help` - Print this help message. + +## Project layout + + mkdocs.yml # The configuration file. + docs/ + index.md # The documentation homepage. + ... # Other markdown pages, images and other files. diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 0000000..d3a35a0 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1 @@ +site_name: Sigil From e340d9603cfedff9eac7949a50c942caf1c13e6d Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Sun, 17 May 2015 15:31:33 -0500 Subject: [PATCH 2/7] theme experiment --- mkdocs.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/mkdocs.yml b/mkdocs.yml index d3a35a0..c7cde55 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1 +1,2 @@ site_name: Sigil +theme: cosmo From d100dac70282db816fb620f0f00e51d09d574895 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Sun, 17 May 2015 21:08:23 -0500 Subject: [PATCH 3/7] readme --- README.md | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++- docs/index.md | 17 ---------- mkdocs.yml | 2 -- 3 files changed, 91 insertions(+), 20 deletions(-) delete mode 100644 docs/index.md delete mode 100644 mkdocs.yml diff --git a/README.md b/README.md index 4c37471..d6f4f43 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,93 @@ -# sigil +# Sigil Standalone string interpolator and template processor + +``` +$ echo '${name} is{{ range seq ${count:-3} }} cool{{ end }}!' | sigil name=Sigil +Sigil is cool cool cool! +``` + +Sigil is a command line tool for template processing and POSIX-compliant +variable expansion. It was created for configuration templating, but can be used +for any text processing. + +## Getting Sigil + +``` +$ curl https://dl.gliderlabs.com/sigil/latest/$(uname -sm|tr \ _).tgz \ + | tar -zxC /usr/local/bin +``` + +Other releases can be downloaded from [Github Releases](https://github.com/gliderlabs/sigil/releases). + +## Using Sigil + +Template text can be provided via STDIN or from a file if provided with the `-f` +flag. Any other arguments are key-values in the form `=`. They are +used as variables. + + * `echo 'Hello, $name' | sigil name=Jeff` + * `sigil -f config.tmpl var1=foo "var2=Hello world"` + +### Variables + +#### POSIX style + +There are two forms of variable syntax in Sigil. The first is POSIX style, which +among other features allows default values or enforces required values: + + * `$variable` - normal POSIX style + * `${variable:-"default"}` - expansion with default value + * `${variable:?}` - fails when not set + +Environment variables are also available as POSIX style variables. This makes +Sigil great for quick and simple string interpolation. + +#### Template style + +The other syntax to use variables is consistent with the rest of the templating +syntax. It uses `{{` and `}}` to define template expressions. Variable expansion +in this form is simply used as: + + * `{{ $variable }}` + +You can do much more with this syntax, such as modifier pipelines. All of which +is explained below. + +### Functions + +There are a number of builtin functions that can be used as modifiers, +conditional tests, expansion data sources, and more. There are two references +for functions available: + + * [Sigil builtins](http://godoc.org/github.com/gliderlabs/sigil/builtin) + * [Go template builtins](http://golang.org/pkg/text/template/#hdr-Functions) + +Here are a few examples: + + * `{{ $variable | capitalize }}` + * `{{ include "file.tmpl" "var1=foo" "var2=bar" }}` + * `{{ file "example.txt" | replace "old" "new" }}` + * `{{ json "file.json" | pointer "/Widgets/0/Name" }}` + +### Conditionals + + * `{{ if expr }} true {{ end }}` + * `{{ if expr }} true {{ else }} false {{ end }}` + * `{{ if expr }} true {{ else if expr }} also true {{ end }}` + +### Loops / Iteration + + * `{{ range expr }} element: {{.}} {{ end }}` + * `{{ range expr }} elements {{ else }} no elements {{ end }}` + +### Full Syntax + +Lots more is possible with this template syntax. Sigil is based on Go's +[text/template package](http://golang.org/pkg/text/template/). You can read full +documentation there. + + +## License + +BSD diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index fc71ab8..0000000 --- a/docs/index.md +++ /dev/null @@ -1,17 +0,0 @@ -# Welcome to Sigil - -For full documentation visit [mkdocs.org](http://mkdocs.org). - -## Commands - -* `mkdocs new [dir-name]` - Create a new project. -* `mkdocs serve` - Start the live-reloading docs server. -* `mkdocs build` - Build the documentation site. -* `mkdocs help` - Print this help message. - -## Project layout - - mkdocs.yml # The configuration file. - docs/ - index.md # The documentation homepage. - ... # Other markdown pages, images and other files. diff --git a/mkdocs.yml b/mkdocs.yml deleted file mode 100644 index c7cde55..0000000 --- a/mkdocs.yml +++ /dev/null @@ -1,2 +0,0 @@ -site_name: Sigil -theme: cosmo From 0404434a7299f7002e379f68d8d069d434698722 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Mon, 18 May 2015 13:40:01 -0500 Subject: [PATCH 4/7] changing posix expansion back to preprocess --- sigil.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sigil.go b/sigil.go index 9d75d5d..e78046f 100644 --- a/sigil.go +++ b/sigil.go @@ -32,18 +32,18 @@ func Execute(input string, vars map[string]string) (string, error) { escaped := strings.Replace(v, "\"", "\\\"", -1) tmplVars = tmplVars + fmt.Sprintf("{{ $%s := \"%s\" }}", k, escaped) } - tmpl, err := template.New("template").Funcs(fnMap).Parse(tmplVars + input) + preprocessed, err := posix.ExpandEnv(input) if err != nil { return "", err } - var buf bytes.Buffer - err = tmpl.Execute(&buf, nil) + tmpl, err := template.New("template").Funcs(fnMap).Parse(tmplVars + preprocessed) if err != nil { return "", err } - postprocessed, err := posix.ExpandEnv(buf.String()) + var buf bytes.Buffer + err = tmpl.Execute(&buf, nil) if err != nil { return "", err } - return postprocessed, nil + return buf.String(), nil } From ba9c3d4ef07a6cd8ebec49661cddda606dd794d0 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Mon, 18 May 2015 13:55:52 -0500 Subject: [PATCH 5/7] make posix preprocessing capable of being turned off --- Makefile | 2 +- cmd/sigil.go | 7 +++++++ sigil.go | 14 +++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index c2689e2..d80c1b3 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ NAME=sigil ARCH=$(shell uname -m) ORG=gliderlabs -VERSION=0.1.0 +VERSION=0.2.0 build: mkdir -p build/Linux && GOOS=linux go build -ldflags "-X main.Version $(VERSION)" -o build/Linux/$(NAME) ./cmd diff --git a/cmd/sigil.go b/cmd/sigil.go index 2ef1678..78b5329 100644 --- a/cmd/sigil.go +++ b/cmd/sigil.go @@ -16,6 +16,7 @@ var Version string var ( filename = flag.String("f", "", "use template file instead of STDIN") + posix = flag.Bool("p", true, "preprocess with POSIX variable expansion") version = flag.Bool("v", false, "prints version") ) @@ -41,8 +42,14 @@ func main() { fmt.Println(Version) os.Exit(0) } + if *posix { + sigil.PosixPreprocess = true + } vars := make(map[string]string) for _, arg := range os.Args { + if strings.HasPrefix(arg, "-") { + continue + } parts := strings.SplitN(arg, "=", 2) if len(parts) == 2 { vars[parts[0]] = parts[1] diff --git a/sigil.go b/sigil.go index e78046f..300d0ba 100644 --- a/sigil.go +++ b/sigil.go @@ -11,7 +11,8 @@ import ( ) var ( - TemplateDir string + TemplateDir string + PosixPreprocess bool ) var fnMap = template.FuncMap{} @@ -24,6 +25,7 @@ func Register(fm template.FuncMap) { func Execute(input string, vars map[string]string) (string, error) { var tmplVars string + var err error for k, v := range vars { err := os.Setenv(k, v) if err != nil { @@ -32,11 +34,13 @@ func Execute(input string, vars map[string]string) (string, error) { escaped := strings.Replace(v, "\"", "\\\"", -1) tmplVars = tmplVars + fmt.Sprintf("{{ $%s := \"%s\" }}", k, escaped) } - preprocessed, err := posix.ExpandEnv(input) - if err != nil { - return "", err + if PosixPreprocess { + input, err = posix.ExpandEnv(input) + if err != nil { + return "", err + } } - tmpl, err := template.New("template").Funcs(fnMap).Parse(tmplVars + preprocessed) + tmpl, err := template.New("template").Funcs(fnMap).Parse(tmplVars + input) if err != nil { return "", err } From 78c22d5cbb281fdbc3f16788cbd9517e3ee196b4 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Mon, 18 May 2015 15:22:19 -0500 Subject: [PATCH 6/7] clear cached go workspace every time --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d80c1b3..6550cce 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ release: circleci: rm ~/.gitconfig - test -d /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) || { cd .. \ + rm -rf /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) || { cd .. \ && mkdir -p /home/ubuntu/.go_workspace/src/github.com/$(ORG) \ && mv $(NAME) /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) \ && ln -s /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) $(NAME); } From 23988cb902b5be324e22df9be20282b7aa55d5f4 Mon Sep 17 00:00:00 2001 From: Jeff Lindsay Date: Mon, 18 May 2015 15:26:09 -0500 Subject: [PATCH 7/7] different logic now --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6550cce..328b4f5 100644 --- a/Makefile +++ b/Makefile @@ -22,10 +22,10 @@ release: circleci: rm ~/.gitconfig - rm -rf /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) || { cd .. \ + rm -rf /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) && cd .. \ && mkdir -p /home/ubuntu/.go_workspace/src/github.com/$(ORG) \ && mv $(NAME) /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) \ - && ln -s /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) $(NAME); } + && ln -s /home/ubuntu/.go_workspace/src/github.com/$(ORG)/$(NAME) $(NAME) clean: rm -rf build release