Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
fix(output): don't output empty files if there is nothing to write
Browse files Browse the repository at this point in the history
Signed-off-by: Jeff Davis <[email protected]>
  • Loading branch information
Jeff Davis authored and JefeDavis committed Jun 1, 2021
1 parent 257003f commit 3e69cb8
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 87 deletions.
105 changes: 105 additions & 0 deletions internal/instructions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@
package instructions

import (
"bytes"
"fmt"
"os"
"path"

"github.com/op/go-logging"
"github.com/vmware-tanzu-labs/yaml-overlay-tool/internal/actions"
"gopkg.in/yaml.v3"
)

// Config contains configuration options used with instruction files.
Expand All @@ -19,3 +25,102 @@ type Config struct {
Styles actions.Styles
Values []string
}

// doPostProcessing renders a document and outputs it to the location specified in config.
func (cfg *Config) doPostProcessing(yf *YamlFile) error {
var o *os.File

var err error

output, err := cfg.encodeNodes(yf.Nodes)
if err != nil {
return fmt.Errorf("unable to marshal final document %s, error: %w", yf.Path, err)
}

if output.Len() == 0 {
log.Debugf("File %s was omitted from output", yf.Path)

return nil
}

if cfg.StdOut {
o = os.Stdout
} else {
log.Debugf("Final: >>>\n%s\n", output)
o, err = cfg.openOutputFile(yf)
if err != nil {
return err
}

defer CloseFile(o)
}

if _, err = output.WriteTo(o); err != nil {
return fmt.Errorf("failed to %w", err)
}

output.Reset()

return nil
}

// openOutputFile opens or creates a file for outputing results.
func (cfg *Config) openOutputFile(yf *YamlFile) (*os.File, error) {
fileName := path.Join(cfg.OutputDir, yf.OutputPath)
dirName := path.Dir(fileName)

if _, err := os.Stat(dirName); os.IsNotExist(err) {
if err := os.MkdirAll(dirName, 0755); err != nil {
return nil, fmt.Errorf("failed to create output directory %s, %w", dirName, err)
}
}

file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return nil, fmt.Errorf("failed to create/open file %s: %w", fileName, err)
}

os.Stdout.WriteString(fileName + "\n")

return file, nil
}

// encodeNodes processes each node given and encodes them into a single buffer for output.
func (cfg *Config) encodeNodes(nodes []*yaml.Node) (*bytes.Buffer, error) {
var fileWritten bool

output := new(bytes.Buffer)

ye := yaml.NewEncoder(output)

defer func() {
if fileWritten {
if err := ye.Close(); err != nil {
log.Criticalf("error closing encoder, %s", err)
}
}
}()

ye.SetIndent(cfg.Indent)

for i, node := range nodes {
if len(node.Content) == 0 {
continue
}

if i == 0 {
output.WriteString("---\n")
}

actions.SetStyle(cfg.Styles, node)

err := ye.Encode(node)
if err != nil {
return nil, fmt.Errorf("%w", err)
}

fileWritten = true
}

return output, nil
}
2 changes: 1 addition & 1 deletion internal/instructions/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func Execute(cfg *Config) error {
// saving to file or displaying to screen.
func PostProcessHandler(cfg *Config, pChan <-chan *YamlFile) error {
for yf := range pChan {
if err := yf.doPostProcessing(cfg); err != nil {
if err := cfg.doPostProcessing(yf); err != nil {
return err
}
}
Expand Down
86 changes: 0 additions & 86 deletions internal/instructions/yamlfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
package instructions

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path"

"github.com/vmware-tanzu-labs/yaml-overlay-tool/internal/actions"
"github.com/vmware-tanzu-labs/yaml-overlay-tool/internal/overlays"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -59,69 +57,6 @@ func (yf *YamlFile) queueOverlays(stream *overlays.WorkStream) {
stream.CloseStream()
}

// doPostProcessing renders a document and outputs it to the location specified in config.
func (yf *YamlFile) doPostProcessing(cfg *Config) error {
var o *os.File

var err error

var fileWritten bool

output := new(bytes.Buffer)

ye := yaml.NewEncoder(output)

defer func() {
if fileWritten {
if err = ye.Close(); err != nil {
log.Criticalf("error closing encoder, %s", err)
}
}
}()

ye.SetIndent(cfg.Indent)

for i, node := range yf.Nodes {
if len(node.Content) == 0 {
continue
}

if i == 0 {
output.WriteString("---\n")
}

actions.SetStyle(cfg.Styles, node)

err = ye.Encode(node)
if err != nil {
return fmt.Errorf("unable to marshal final document %s, error: %w", yf.Path, err)
}

fileWritten = true
}

// added so we can quickly see the results of the run
if cfg.StdOut {
o = os.Stdout
} else {
log.Debugf("Final: >>>\n%s\n", output)
o, err = yf.OpenOutputFile(cfg)
if err != nil {
return err
}

defer CloseFile(o)
}

if _, err = output.WriteTo(o); err != nil {
return fmt.Errorf("failed to %w", err)
}

output.Reset()

return nil
}

func (yfs *YamlFiles) UnmarshalYAML(value *yaml.Node) error {
var yft []*YamlFile

Expand All @@ -147,27 +82,6 @@ func (yfs *YamlFiles) UnmarshalYAML(value *yaml.Node) error {
return nil
}

// OpenOutputFile opens or creates a file for outputing results.
func (yf *YamlFile) OpenOutputFile(o *Config) (*os.File, error) {
fileName := path.Join(o.OutputDir, yf.OutputPath)
dirName := path.Dir(fileName)

if _, err := os.Stat(dirName); os.IsNotExist(err) {
if err := os.MkdirAll(dirName, 0755); err != nil {
return nil, fmt.Errorf("failed to create output directory %s, %w", dirName, err)
}
}

file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
if err != nil {
return nil, fmt.Errorf("failed to create/open file %s: %w", fileName, err)
}

os.Stdout.WriteString(fileName + "\n")

return file, nil
}

func (yf *YamlFile) readYamlFile() error {
reader, err := ReadStream(yf.Path)
if err != nil {
Expand Down

0 comments on commit 3e69cb8

Please sign in to comment.