diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2018-04-24 00:54:42 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2018-05-02 19:22:59 -0400 |
| commit | 2fefe92d679d1f28a0f8a7d11f224a1e784a366e (patch) | |
| tree | 3c67c38a5f8204d6245925b0350d78b68ffe7596 | |
| parent | 03cbf49cd896e55a2fb9c02f07c0c231e221c39b (diff) | |
Execute additional command after --
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
| -rw-r--r-- | cmd/gomplate/main.go | 27 | ||||
| -rw-r--r-- | docs/content/usage.md | 10 | ||||
| -rw-r--r-- | test/integration/basic_test.go | 14 |
3 files changed, 49 insertions, 2 deletions
diff --git a/cmd/gomplate/main.go b/cmd/gomplate/main.go index 256bed1f..1fcb1a5b 100644 --- a/cmd/gomplate/main.go +++ b/cmd/gomplate/main.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "os/exec" "github.com/hairyhenderson/gomplate" "github.com/hairyhenderson/gomplate/env" @@ -45,6 +46,29 @@ func printVersion(name string) { fmt.Printf("%s version %s\n", name, version.Version) } +// postRunExec - if templating succeeds, the command following a '--' will be executed +func postRunExec(cmd *cobra.Command, args []string) error { + if len(args) > 0 { + name := args[0] + args = args[1:] + c := exec.Command(name, args...) + c.Stdin = os.Stdin + c.Stderr = os.Stderr + c.Stdout = os.Stdout + return c.Run() + } + return nil +} + +// optionalExecArgs - implements cobra.PositionalArgs. Allows extra args following +// a '--', but not otherwise. +func optionalExecArgs(cmd *cobra.Command, args []string) error { + if cmd.ArgsLenAtDash() == 0 { + return nil + } + return cobra.NoArgs(cmd, args) +} + func newGomplateCmd() *cobra.Command { rootCmd := &cobra.Command{ Use: "gomplate", @@ -57,7 +81,8 @@ func newGomplateCmd() *cobra.Command { } return gomplate.RunTemplates(&opts) }, - Args: cobra.NoArgs, + PostRunE: postRunExec, + Args: optionalExecArgs, } return rootCmd } diff --git a/docs/content/usage.md b/docs/content/usage.md index de4ec93b..30f931dd 100644 --- a/docs/content/usage.md +++ b/docs/content/usage.md @@ -74,3 +74,13 @@ A few different forms are valid: Sometimes it's necessary to override the default template delimiters (`{{`/`}}`). Use `--left-delim`/`--right-delim` or set `$GOMPLATE_LEFT_DELIM`/`$GOMPLATE_RIGHT_DELIM`. + +## Post-template command execution + +Gomplate can launch other commands when template execution is successful. Simply +add the command to the command-line after a `--` argument: + +```console +$ gomplate -i 'hello world' -o out.txt -- cat out.txt +hello world +``` diff --git a/test/integration/basic_test.go b/test/integration/basic_test.go index c0a52a0b..2565341b 100644 --- a/test/integration/basic_test.go +++ b/test/integration/basic_test.go @@ -1,4 +1,4 @@ -//+build integration +// +build integration //+build !windows package integration @@ -152,3 +152,15 @@ func (s *BasicSuite) TestUnknownArgErrors(c *C) { result := icmd.RunCommand(GomplateBin, "-in", "flibbit") result.Assert(c, icmd.Expected{ExitCode: 1, Out: `unknown command "flibbit" for "gomplate"`}) } + +func (s *BasicSuite) TestExecCommand(c *C) { + out := s.tmpDir.Join("out") + result := icmd.RunCmd(icmd.Command(GomplateBin, + "-i", `{{print "hello world"}}`, + "-o", out, + "--", "cat", out)) + result.Assert(c, icmd.Expected{ + ExitCode: 0, + Out: "hello world", + }) +} |
