Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression in ps with custom format #2232

Merged
merged 1 commit into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions cmd/podman/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,6 @@ func imagesCmd(c *cli.Context) error {
}

opts.outputformat = opts.setOutputFormat()
/*
podman does not implement --all for images

intermediate images are only generated during the build process. they are
children to the image once built. until buildah supports caching builds,
it will not generate these intermediate images.
*/
images, err := runtime.GetImages()
if err != nil {
return errors.Wrapf(err, "unable to get images")
Expand Down
41 changes: 36 additions & 5 deletions cmd/podman/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,19 +606,50 @@ func portsToString(ports []ocicni.PortMapping) string {
}

func printFormat(format string, containers []shared.PsContainerOutput) error {
out := template.New("output")
out, err := out.Parse(format + "\n")
// return immediately if no containers are present
if len(containers) == 0 {
return nil
}

// Use a tabwriter to align column format
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)

// Make a map of the field names for the headers
headerNames := make(map[string]string)
v := reflect.ValueOf(containers[0])
t := v.Type()
for i := 0; i < t.NumField(); i++ {
headerNames[t.Field(i).Name] = t.Field(i).Name
}

// Spit out the header if "table" is present in the format
if strings.HasPrefix(format, "table") {
hformat := strings.Replace(strings.TrimSpace(format[5:]), " ", "\t", -1)
format = hformat
headerTmpl, err := template.New("header").Parse(hformat)
if err != nil {
return err
}
if err := headerTmpl.Execute(w, headerNames); err != nil {
return err
}
fmt.Fprintln(w, "")
}

// Spit out the data rows now
dataTmpl, err := template.New("data").Parse(format)
if err != nil {
return err
}

for _, container := range containers {
if err := out.Execute(os.Stdout, container); err != nil {
if err := dataTmpl.Execute(w, container); err != nil {
return err
}

fmt.Fprintln(w, "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt.Fprintf(w, "\n") Seems clearer as to intention.

}
return nil
// Flush the writer
return w.Flush()
}

func dumpJSON(containers []shared.PsContainerOutput) error {
Expand Down
7 changes: 5 additions & 2 deletions test/e2e/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"regexp"
"sort"
"strings"

. "github.com/containers/libpod/test/utils"
"github.com/docker/go-units"
Expand Down Expand Up @@ -148,10 +149,12 @@ var _ = Describe("Podman ps", func() {
_, ec, _ := podmanTest.RunLsContainer("test1")
Expect(ec).To(Equal(0))

result := podmanTest.Podman([]string{"ps", "-a", "--format", "\"table {{.ID}} {{.Image}} {{.Labels}}\""})
result := podmanTest.Podman([]string{"ps", "-a", "--format", "table {{.ID}} {{.Image}} {{.Labels}}"})
result.WaitWithDefaultTimeout()
Expect(strings.Contains(result.OutputToStringArray()[0], "table")).To(BeFalse())
Expect(strings.Contains(result.OutputToStringArray()[0], "ID")).To(BeTrue())
Expect(strings.Contains(result.OutputToStringArray()[1], "alpine:latest")).To(BeTrue())
Expect(result.ExitCode()).To(Equal(0))
Expect(result.IsJSONOutputValid()).To(BeTrue())
})

It("podman ps ancestor filter flag", func() {
Expand Down