summaryrefslogtreecommitdiff
path: root/template.go
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2024-05-11 15:20:45 -0400
committerGitHub <noreply@github.com>2024-05-11 19:20:45 +0000
commit916ebe6ea6d0319a37f0c4b62fd5e4a189f3dc50 (patch)
tree89422b20e758e8ccf5c35cf6cd1f36f759532362 /template.go
parenta55441b886ee692f8734e6c561ae0ebebff64a55 (diff)
feat!: Always suppress empty output (#1981)
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'template.go')
-rw-r--r--template.go37
1 files changed, 14 insertions, 23 deletions
diff --git a/template.go b/template.go
index dbdabf36..812aafe4 100644
--- a/template.go
+++ b/template.go
@@ -192,7 +192,7 @@ func gatherTemplates(ctx context.Context, cfg *config.Config, outFileNamer func(
case cfg.Input != "":
// open the output file - no need to close it, as it will be closed by the
// caller later
- target, oerr := openOutFile(ctx, cfg.OutputFiles[0], 0o755, mode, modeOverride, cfg.Stdout, cfg.SuppressEmpty)
+ target, oerr := openOutFile(ctx, cfg.OutputFiles[0], 0o755, mode, modeOverride, cfg.Stdout)
if oerr != nil {
return nil, fmt.Errorf("openOutFile: %w", oerr)
}
@@ -367,7 +367,7 @@ func readInFile(ctx context.Context, cfg *config.Config, inFile string, mode os.
func getOutfileHandler(ctx context.Context, cfg *config.Config, outFile string, mode os.FileMode, modeOverride bool) (io.Writer, error) {
// open the output file - no need to close it, as it will be closed by the
// caller later
- target, err := openOutFile(ctx, outFile, 0o755, mode, modeOverride, cfg.Stdout, cfg.SuppressEmpty)
+ target, err := openOutFile(ctx, outFile, 0o755, mode, modeOverride, cfg.Stdout)
if err != nil {
return nil, fmt.Errorf("openOutFile: %w", err)
}
@@ -417,29 +417,20 @@ func fileToTemplate(ctx context.Context, cfg *config.Config, inFile, outFile str
// openOutFile returns a writer for the given file, creating the file if it
// doesn't exist yet, and creating the parent directories if necessary. Will
-// defer actual opening until the first write (or the first non-empty write if
-// 'suppressEmpty' is true). If the file already exists, it will not be
-// overwritten until the first difference is encountered.
+// defer actual opening until the first non-empty write. If the file already
+// exists, it will not be overwritten until the first difference is encountered.
//
-// TODO: the 'suppressEmpty' behaviour should be always enabled, in the next
-// major release (v4.x).
+// TODO: dirMode is always called with 0o755 - should either remove or make it configurable
//
-//nolint:unparam // TODO: dirMode is always called with 0o755 - should either remove or make it configurable
-func openOutFile(ctx context.Context, filename string, dirMode, mode os.FileMode, modeOverride bool, stdout io.Writer, suppressEmpty bool) (out io.Writer, err error) {
- if suppressEmpty {
- out = iohelpers.NewEmptySkipper(func() (io.Writer, error) {
- if filename == "-" {
- return stdout, nil
- }
- return createOutFile(ctx, filename, dirMode, mode, modeOverride)
- })
- return out, nil
- }
-
- if filename == "-" {
- return stdout, nil
- }
- return createOutFile(ctx, filename, dirMode, mode, modeOverride)
+//nolint:unparam
+func openOutFile(ctx context.Context, filename string, dirMode, mode os.FileMode, modeOverride bool, stdout io.Writer) (out io.Writer, err error) {
+ out = iohelpers.NewEmptySkipper(func() (io.Writer, error) {
+ if filename == "-" {
+ return iohelpers.NopCloser(stdout), nil
+ }
+ return createOutFile(ctx, filename, dirMode, mode, modeOverride)
+ })
+ return out, nil
}
func createOutFile(ctx context.Context, filename string, dirMode, mode os.FileMode, modeOverride bool) (out io.WriteCloser, err error) {