Merge pull request #290 from umohnani8/templates

Fix when the --format flag prints a new line at the end
This commit is contained in:
Daniel J Walsh
2018-02-07 16:50:23 -05:00
committed by GitHub
4 changed files with 29 additions and 10 deletions

View File

@ -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()
}
}

View File

@ -32,7 +32,7 @@ var _ = Describe("Podman images", func() {
session := podmanTest.Podman([]string{"images"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 3))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 2))
Expect(session.LineInOuputStartsWith("docker.io/library/alpine")).To(BeTrue())
Expect(session.LineInOuputStartsWith("docker.io/library/busybox")).To(BeTrue())
})
@ -48,6 +48,6 @@ var _ = Describe("Podman images", func() {
session := podmanTest.Podman([]string{"images", "-qn"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 2))
Expect(len(session.OutputToStringArray())).To(BeNumerically(">", 1))
})
})

View File

@ -62,7 +62,7 @@ var _ = Describe("Podman import", func() {
results := podmanTest.Podman([]string{"images", "-q"})
results.WaitWithDefaultTimeout()
Expect(results.ExitCode()).To(Equal(0))
Expect(len(results.OutputToStringArray())).To(Equal(4))
Expect(len(results.OutputToStringArray())).To(Equal(3))
})
It("podman import with message flag", func() {

View File

@ -4,6 +4,7 @@ import (
"os"
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
@ -100,7 +101,7 @@ var _ = Describe("Podman ps", func() {
result := podmanTest.Podman([]string{"ps", "--last", "2"})
result.WaitWithDefaultTimeout()
Expect(result.ExitCode()).To(Equal(0))
Expect(len(result.OutputToStringArray())).Should(Equal(4))
Expect(len(result.OutputToStringArray())).Should(Equal(3))
})
It("podman ps no-trunc", func() {