diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2018-02-07 16:50:23 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-07 16:50:23 -0500 |
commit | dd9ecc7a23595e0478a0e4606e22bd48276ba4f1 (patch) | |
tree | 4ec2a358a102e1bb4e1f7a71cb27201d9639b3e6 /cmd/podman | |
parent | c40888cf9f35c188662e2ac52a3a3c01939deae9 (diff) | |
parent | 363cfcb0ce1477249a40861ef936f377b95aaf09 (diff) | |
download | podman-dd9ecc7a23595e0478a0e4606e22bd48276ba4f1.tar.gz podman-dd9ecc7a23595e0478a0e4606e22bd48276ba4f1.tar.bz2 podman-dd9ecc7a23595e0478a0e4606e22bd48276ba4f1.zip |
Merge pull request #290 from umohnani8/templates
Fix when the --format flag prints a new line at the end
Diffstat (limited to 'cmd/podman')
-rw-r--r-- | cmd/podman/formats/formats.go | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/cmd/podman/formats/formats.go b/cmd/podman/formats/formats.go index 4b6527b30..bfd773c45 100644 --- a/cmd/podman/formats/formats.go +++ b/cmd/podman/formats/formats.go @@ -11,6 +11,7 @@ import ( "github.com/ghodss/yaml" "github.com/pkg/errors" + "golang.org/x/crypto/ssh/terminal" ) const ( @@ -70,7 +71,8 @@ func (j JSONStructArray) Out() error { // If the we did get NULL back, we should spit out {} which is // at least valid JSON for the consumer. - fmt.Printf("%s\n", data) + fmt.Printf("%s", data) + humanNewLine() return nil } @@ -95,13 +97,20 @@ func (t StdoutTemplateArray) Out() error { if err != nil { return errors.Wrapf(err, "Template parsing error") } - for _, img := range t.Output { + for i, img := range t.Output { basicTmpl := tmpl.Funcs(basicFunctions) err = basicTmpl.Execute(w, img) if err != nil { return err } - fmt.Fprintln(w, "") + if i != len(t.Output)-1 { + fmt.Fprintln(w, "") + continue + } + // Only print new line at the end of the output if stdout is the terminal + if terminal.IsTerminal(int(os.Stdout.Fd())) { + fmt.Fprintln(w, "") + } } return w.Flush() } @@ -112,7 +121,8 @@ func (j JSONStruct) Out() error { if err != nil { return err } - fmt.Printf("%s\n", data) + fmt.Printf("%s", data) + humanNewLine() return nil } @@ -126,7 +136,7 @@ func (t StdoutTemplate) Out() error { if err != nil { return err } - fmt.Println() + humanNewLine() return nil } @@ -138,6 +148,14 @@ func (y YAMLStruct) Out() error { if err != nil { return err } - fmt.Println(string(buf)) + fmt.Printf("%s", string(buf)) + humanNewLine() return nil } + +// humanNewLine prints a new line at the end of the output only if stdout is the terminal +func humanNewLine() { + if terminal.IsTerminal(int(os.Stdout.Fd())) { + fmt.Println() + } +} |