Skip to content

Commit

Permalink
Always suppress empty output
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Henderson <[email protected]>
  • Loading branch information
hairyhenderson committed Feb 6, 2024
1 parent 4d24b9d commit be09edf
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 155 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (o *Config) toNewConfig() (*config.Config, error) {
LDelim: o.LDelim,
RDelim: o.RDelim,
Stdin: os.Stdin,
Stdout: &iohelpers.NopCloser{Writer: o.Out},
Stdout: iohelpers.NopCloser(o.Out),
Stderr: os.Stderr,
}
err := cfg.ParsePluginFlags(o.Plugins)
Expand Down
10 changes: 0 additions & 10 deletions docs/content/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,16 +447,6 @@ Overrides the right template delimiter.
rightDelim: '))'
```

## `suppressEmpty`

See _[Suppressing empty output](../usage/#suppressing-empty-output)_

Suppresses empty output (i.e. output consisting of only whitespace). Can also be set with the `GOMPLATE_SUPPRESS_EMPTY` environment variable.

```yaml
suppressEmpty: true
```

## `templates`

See [`--template`/`-t`](../usage/#template-t).
Expand Down
11 changes: 0 additions & 11 deletions docs/content/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,6 @@ hello world
See also [`--exec-pipe`](#exec-pipe) for piping output directly into the
post-exec command.

## Suppressing empty output

Sometimes it can be desirable to suppress empty output (i.e. output consisting of only whitespace). To do so, set `suppressEmpty: true` in your [config](../config/#suppressempty) file, or `GOMPLATE_SUPPRESS_EMPTY=true` in your environment:

```console
$ export GOMPLATE_SUPPRESS_EMPTY=true
$ gomplate -i '{{ print " \n" }}' -o out
$ cat out
cat: out: No such file or directory
```

[default context]: ../syntax/#the-context
[context]: ../syntax/#the-context
[external templates]: ../syntax/#external-templates
Expand Down
4 changes: 0 additions & 4 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,6 @@ func applyEnvVars(_ context.Context, cfg *config.Config) (*config.Config, error)
cfg.PluginTimeout = t
}

if !cfg.SuppressEmpty && conv.ToBool(env.Getenv("GOMPLATE_SUPPRESS_EMPTY", "false")) {
cfg.SuppressEmpty = true
}

if !cfg.Experimental && conv.ToBool(env.Getenv("GOMPLATE_EXPERIMENTAL", "false")) {
cfg.Experimental = true
}
Expand Down
15 changes: 0 additions & 15 deletions internal/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,6 @@ func TestApplyEnvVars(t *testing.T) {
&config.Config{PluginTimeout: 100 * time.Millisecond},
"GOMPLATE_PLUGIN_TIMEOUT", "2s",
},
{
&config.Config{},
&config.Config{SuppressEmpty: false},
"GOMPLATE_SUPPRESS_EMPTY", "bogus",
},
{
&config.Config{},
&config.Config{SuppressEmpty: true},
"GOMPLATE_SUPPRESS_EMPTY", "true",
},
{
&config.Config{SuppressEmpty: true},
&config.Config{SuppressEmpty: true},
"GOMPLATE_SUPPRESS_EMPTY", "false",
},
{
&config.Config{},
&config.Config{Experimental: false},
Expand Down
5 changes: 2 additions & 3 deletions internal/config/configfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ type Config struct {

PluginTimeout time.Duration `yaml:"pluginTimeout,omitempty"`

ExecPipe bool `yaml:"execPipe,omitempty"`
SuppressEmpty bool `yaml:"suppressEmpty,omitempty"`
Experimental bool `yaml:"experimental,omitempty"`
ExecPipe bool `yaml:"execPipe,omitempty"`
Experimental bool `yaml:"experimental,omitempty"`
}

type experimentalCtxKey struct{}
Expand Down
10 changes: 7 additions & 3 deletions internal/iohelpers/writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,21 @@ func allWhitespace(p []byte) bool {

// NopCloser returns a WriteCloser with a no-op Close method wrapping
// the provided io.Writer.
type NopCloser struct {
func NopCloser(w io.Writer) io.WriteCloser {
return &nopCloser{Writer: w}
}

type nopCloser struct {
io.Writer
}

// Close - implements io.Closer
func (n *NopCloser) Close() error {
func (n *nopCloser) Close() error {
return nil
}

var (
_ io.WriteCloser = (*NopCloser)(nil)
_ io.WriteCloser = (*nopCloser)(nil)
_ io.WriteCloser = (*emptySkipper)(nil)
_ io.WriteCloser = (*sameSkipper)(nil)
)
Expand Down
3 changes: 1 addition & 2 deletions internal/tests/integration/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,7 @@ func TestBasic_EmptyOutputSuppression(t *testing.T) {
tmpDir := setupBasicTest(t)
out := tmpDir.Join("out")
o, e, err := cmd(t, "-i", `{{print "\t \n\n\r\n\t\t \v\n"}}`,
"-o", out).
withEnv("GOMPLATE_SUPPRESS_EMPTY", "true").run()
"-o", out).run()
assertSuccess(t, o, e, err, "")

_, err = os.Stat(out)
Expand Down
136 changes: 68 additions & 68 deletions internal/tests/integration/gomplateignore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ func TestGomplateignore_Simple(t *testing.T) {
files, err := execute(t, `# all dot files
.*
*.log`,
tfs.WithFile("empty.log", ""),
tfs.WithFile("rain.txt", ""))
tfs.WithFile("foo.log", "..."),
tfs.WithFile("rain.txt", "..."))

require.NoError(t, err)
assert.Equal(t, []string{"rain.txt"}, files)
Expand All @@ -91,12 +91,12 @@ f[o]o/bar
tfs.WithDir("foo",
tfs.WithDir("bar",
tfs.WithDir("tool",
tfs.WithFile("lex.txt", ""),
tfs.WithFile("lex.txt", "..."),
),
tfs.WithFile("1.txt", ""),
tfs.WithFile("1.txt", "..."),
),
tfs.WithDir("tar",
tfs.WithFile("2.txt", ""),
tfs.WithFile("2.txt", "..."),
),
),
)
Expand All @@ -110,10 +110,10 @@ func TestGomplateignore_Root(t *testing.T) {
files, err := execute(t, `.gomplateignore
/1.txt`,
tfs.WithDir("sub",
tfs.WithFile("1.txt", ""),
tfs.WithFile("2.txt", ""),
tfs.WithFile("1.txt", "..."),
tfs.WithFile("2.txt", "..."),
),
tfs.WithFile("1.txt", ""),
tfs.WithFile("1.txt", "..."),
)

require.NoError(t, err)
Expand All @@ -127,14 +127,14 @@ func TestGomplateignore_Exclusion(t *testing.T) {
!/e2.txt
en/e3.txt
!`,
tfs.WithFile("!", ""),
tfs.WithFile("e1.txt", ""),
tfs.WithFile("e2.txt", ""),
tfs.WithFile("e3.txt", ""),
tfs.WithFile("!", "xxx"),
tfs.WithFile("e1.txt", "xxx"),
tfs.WithFile("e2.txt", "xxx"),
tfs.WithFile("e3.txt", "xxx"),
tfs.WithDir("en",
tfs.WithFile("e1.txt", ""),
tfs.WithFile("e2.txt", ""),
tfs.WithFile("e3.txt", ""),
tfs.WithFile("e1.txt", "xxx"),
tfs.WithFile("e2.txt", "xxx"),
tfs.WithFile("e3.txt", "xxx"),
),
)

Expand All @@ -148,13 +148,13 @@ func TestGomplateignore_Nested(t *testing.T) {
tfs.WithDir("inner",
tfs.WithDir("inner2",
tfs.WithFile(".gomplateignore", "moss.ini\n!/jess.ini"),
tfs.WithFile("jess.ini", ""),
tfs.WithFile("moss.ini", "")),
tfs.WithFile("jess.ini", "xxx"),
tfs.WithFile("moss.ini", "xxx")),
tfs.WithFile(".gomplateignore", "*.lst\njess.ini"),
tfs.WithFile("2.lst", ""),
tfs.WithFile("foo.md", ""),
tfs.WithFile("2.lst", "xxx"),
tfs.WithFile("foo.md", "xxx"),
),
tfs.WithFile("1.txt", ""),
tfs.WithFile("1.txt", "xxx"),
)

require.NoError(t, err)
Expand All @@ -170,17 +170,17 @@ world.txt`,
tfs.WithDir("aa",
tfs.WithDir("a1",
tfs.WithDir("a2",
tfs.WithFile("hello.txt", ""),
tfs.WithFile("world.txt", "")),
tfs.WithFile("hello.txt", ""),
tfs.WithFile("world.txt", "")),
tfs.WithFile("hello.txt", ""),
tfs.WithFile("world.txt", "")),
tfs.WithFile("hello.txt", "..."),
tfs.WithFile("world.txt", "...")),
tfs.WithFile("hello.txt", "..."),
tfs.WithFile("world.txt", "...")),
tfs.WithFile("hello.txt", "..."),
tfs.WithFile("world.txt", "...")),
tfs.WithDir("bb",
tfs.WithFile("hello.txt", ""),
tfs.WithFile("world.txt", "")),
tfs.WithFile("hello.txt", ""),
tfs.WithFile("world.txt", ""),
tfs.WithFile("hello.txt", "..."),
tfs.WithFile("world.txt", "...")),
tfs.WithFile("hello.txt", "..."),
tfs.WithFile("world.txt", "..."),
)

require.NoError(t, err)
Expand All @@ -195,11 +195,11 @@ loss.txt
!2.log
`,
tfs.WithDir("loss.txt",
tfs.WithFile("1.log", ""),
tfs.WithFile("2.log", "")),
tfs.WithFile("1.log", "xxx"),
tfs.WithFile("2.log", "xxx")),
tfs.WithDir("foo",
tfs.WithFile("loss.txt", ""),
tfs.WithFile("bare.txt", "")),
tfs.WithFile("loss.txt", "xxx"),
tfs.WithFile("bare.txt", "xxx")),
)

require.NoError(t, err)
Expand All @@ -215,11 +215,11 @@ func TestGomplateignore_LeadingSpace(t *testing.T) {
! dart.log
`,
tfs.WithDir("inner",
tfs.WithFile(" what.txt", ""),
tfs.WithFile(" dart.log", "")),
tfs.WithFile(" what.txt", "xxx"),
tfs.WithFile(" dart.log", "xxx")),
tfs.WithDir("inner2",
tfs.WithFile(" what.txt", "")),
tfs.WithFile(" what.txt", ""),
tfs.WithFile(" what.txt", "xxx")),
tfs.WithFile(" what.txt", "xxx"),
)

require.NoError(t, err)
Expand All @@ -236,19 +236,19 @@ func TestGomplateignore_WithExcludes(t *testing.T) {
"--exclude", "sprites/*.ini",
},
tfs.WithDir("logs",
tfs.WithFile("archive.zip", ""),
tfs.WithFile("engine.log", ""),
tfs.WithFile("skills.log", "")),
tfs.WithFile("archive.zip", "x"),
tfs.WithFile("engine.log", "x"),
tfs.WithFile("skills.log", "x")),
tfs.WithDir("rules",
tfs.WithFile("index.csv", ""),
tfs.WithFile("fire.txt", ""),
tfs.WithFile("earth.txt", "")),
tfs.WithFile("index.csv", "x"),
tfs.WithFile("fire.txt", "x"),
tfs.WithFile("earth.txt", "x")),
tfs.WithDir("sprites",
tfs.WithFile("human.csv", ""),
tfs.WithFile("demon.xml", ""),
tfs.WithFile("alien.ini", "")),
tfs.WithFile("manifest.json", ""),
tfs.WithFile("crash.bin", ""),
tfs.WithFile("human.csv", "x"),
tfs.WithFile("demon.xml", "x"),
tfs.WithFile("alien.ini", "x")),
tfs.WithFile("manifest.json", "x"),
tfs.WithFile("crash.bin", "x"),
)

require.NoError(t, err)
Expand All @@ -265,15 +265,15 @@ func TestGomplateignore_WithIncludes(t *testing.T) {
"--exclude", "rules/*.txt",
},
tfs.WithDir("logs",
tfs.WithFile("archive.zip", ""),
tfs.WithFile("engine.log", ""),
tfs.WithFile("skills.log", "")),
tfs.WithFile("archive.zip", "x"),
tfs.WithFile("engine.log", "x"),
tfs.WithFile("skills.log", "x")),
tfs.WithDir("rules",
tfs.WithFile("index.csv", ""),
tfs.WithFile("fire.txt", ""),
tfs.WithFile("earth.txt", "")),
tfs.WithFile("manifest.json", ""),
tfs.WithFile("crash.bin", ""),
tfs.WithFile("index.csv", "x"),
tfs.WithFile("fire.txt", "x"),
tfs.WithFile("earth.txt", "x")),
tfs.WithFile("manifest.json", "x"),
tfs.WithFile("crash.bin", "x"),
)

require.NoError(t, err)
Expand All @@ -290,19 +290,19 @@ func TestGomplateignore_WithExcludeProcessing(t *testing.T) {
"--exclude", "sprites/*.ini",
},
tfs.WithDir("logs",
tfs.WithFile("archive.zip", ""),
tfs.WithFile("engine.log", ""),
tfs.WithFile("skills.log", "")),
tfs.WithFile("archive.zip", "xxx"),
tfs.WithFile("engine.log", "xxx"),
tfs.WithFile("skills.log", "xxx")),
tfs.WithDir("rules",
tfs.WithFile("index.csv", ""),
tfs.WithFile("fire.txt", ""),
tfs.WithFile("earth.txt", "")),
tfs.WithFile("index.csv", "xxx"),
tfs.WithFile("fire.txt", "xxx"),
tfs.WithFile("earth.txt", "xxx")),
tfs.WithDir("sprites",
tfs.WithFile("human.csv", ""),
tfs.WithFile("demon.xml", ""),
tfs.WithFile("alien.ini", "")),
tfs.WithFile("manifest.json", ""),
tfs.WithFile("crash.bin", ""),
tfs.WithFile("human.csv", "xxx"),
tfs.WithFile("demon.xml", "xxx"),
tfs.WithFile("alien.ini", "xxx")),
tfs.WithFile("manifest.json", "xxx"),
tfs.WithFile("crash.bin", "xxx"),
)

require.NoError(t, err)
Expand Down
4 changes: 1 addition & 3 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"net/http"
"net/url"
"os"
"sync"
"text/template"
"time"
Expand Down Expand Up @@ -244,8 +243,7 @@ func (t *Renderer) renderTemplatesWithData(ctx context.Context, templates []Temp

func (t *Renderer) renderTemplate(ctx context.Context, template Template, f template.FuncMap, tmplctx interface{}) error {
if template.Writer != nil {
wr, ok := template.Writer.(io.Closer)
if ok && wr != os.Stdout {
if wr, ok := template.Writer.(io.Closer); ok {
defer wr.Close()
}
}
Expand Down
Loading

0 comments on commit be09edf

Please sign in to comment.