Skip to content

Commit

Permalink
add --output-files flag
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed May 28, 2020
1 parent c131a50 commit e560fd6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
13 changes: 9 additions & 4 deletions docs/outputs.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
## Outputs

ytt supports two different output destinations:
ytt supports three different output destinations:

- stdout, which is default
- output directory, controlled via `--output-directory`

When destination is stdout, all YAML documents are combined into one document set. Non-yaml files are not printed anywhere.
All YAML documents are combined into one document set. Non-YAML files are not printed anywhere.

When destination is an output directory, ytt will _empty out_ directory beforehand and write out result files preserving file names.
- output files, controlled via `--output-files` flag (v0.28.0+)

Output files will be added to given directory, preserving file names. Example: `ytt -f config.yml --output-files tmp/`.

- output directory, controlled via `--dangerous-emptied-output-directory` flag

Given directory will be _emptied out_ beforehand and output files will be added preserving file names. Example: `ytt -f config.yml --dangerous-emptied-output-files tmp/ytt/`.

If you want to control which files are included in the output use `--file-mark 'something.yml:exclusive-for-output=true'` flag to mark one or more files.
13 changes: 9 additions & 4 deletions pkg/cmd/template/regular_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ type RegularFilesSourceOpts struct {
files []string
fileMarks []string

outputDir string
outputType string
outputDir string
outputFiles string
outputType string

files.SymlinkAllowOpts
}
Expand All @@ -30,7 +31,8 @@ func (s *RegularFilesSourceOpts) Set(cmd *cobra.Command) {
cmd.Flags().StringArrayVarP(&s.files, "file", "f", nil, "File (ie local path, HTTP URL, -) (can be specified multiple times)")

cmd.Flags().StringVar(&s.outputDir, "dangerous-emptied-output-directory", "",
"Output destination directory, which _will be emptied_ before usage")
"Delete given directory, and then create it with output files")
cmd.Flags().StringVar(&s.outputFiles, "output-files", "", "Add output files to given directory")

cmd.Flags().StringVarP(&s.outputType, "output", "o", regularFilesOutputTypeYAML, "Output type (yaml, json, pos)")

Expand Down Expand Up @@ -66,8 +68,11 @@ func (s *RegularFilesSource) Output(out TemplateOutput) error {
return out.Err
}

if len(s.opts.outputDir) > 0 {
switch {
case len(s.opts.outputDir) > 0:
return files.NewOutputDirectory(s.opts.outputDir, out.Files, s.ui).Write()
case len(s.opts.outputFiles) > 0:
return files.NewOutputDirectory(s.opts.outputFiles, out.Files, s.ui).WriteFiles()
}

var printerFunc func(io.Writer) yamlmeta.DocumentPrinter
Expand Down
6 changes: 5 additions & 1 deletion pkg/files/output_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ func (d *OutputDirectory) Write() error {
return err
}

err = os.MkdirAll(d.path, 0700)
return d.WriteFiles()
}

func (d *OutputDirectory) WriteFiles() error {
err := os.MkdirAll(d.path, 0700)
if err != nil {
return err
}
Expand Down

0 comments on commit e560fd6

Please sign in to comment.