diff options
-rw-r--r-- | cmd/podman/images/list.go | 79 | ||||
-rw-r--r-- | libpod/events/events.go | 2 | ||||
-rw-r--r-- | pkg/bindings/containers/attach.go | 62 | ||||
-rw-r--r-- | test/e2e/images_test.go | 9 |
4 files changed, 88 insertions, 64 deletions
diff --git a/cmd/podman/images/list.go b/cmd/podman/images/list.go index 236ae15b4..83acb53d9 100644 --- a/cmd/podman/images/list.go +++ b/cmd/podman/images/list.go @@ -98,57 +98,79 @@ func images(cmd *cobra.Command, args []string) error { return err } + imgs := sortImages(summaries) switch { case listFlag.quiet: - return writeID(summaries) + return writeID(imgs) case cmd.Flag("format").Changed && listFlag.format == "json": - return writeJSON(summaries) + return writeJSON(imgs) default: - return writeTemplate(summaries) + return writeTemplate(imgs) } } -func writeID(imageS []*entities.ImageSummary) error { - var ids = map[string]struct{}{} - for _, e := range imageS { - i := "sha256:" + e.ID - if !listFlag.noTrunc { - i = fmt.Sprintf("%12.12s", e.ID) +func writeID(imgs []imageReporter) error { + lookup := make(map[string]struct{}, len(imgs)) + ids := make([]string, 0) + + for _, e := range imgs { + if _, found := lookup[e.ID()]; !found { + lookup[e.ID()] = struct{}{} + ids = append(ids, e.ID()) } - ids[i] = struct{}{} } - for k := range ids { - fmt.Fprint(os.Stdout, k+"\n") + for _, k := range ids { + fmt.Println(k) } return nil } -func writeJSON(imageS []*entities.ImageSummary) error { +func writeJSON(images []imageReporter) error { type image struct { entities.ImageSummary Created string CreatedAt string } - imgs := make([]image, 0, len(imageS)) - for _, e := range imageS { + imgs := make([]image, 0, len(images)) + for _, e := range images { var h image - h.ImageSummary = *e - h.Created = units.HumanDuration(time.Since(e.Created)) + " ago" - h.CreatedAt = e.Created.Format(time.RFC3339Nano) + h.ImageSummary = e.ImageSummary + h.Created = units.HumanDuration(time.Since(e.ImageSummary.Created)) + " ago" + h.CreatedAt = e.ImageSummary.Created.Format(time.RFC3339Nano) h.RepoTags = nil imgs = append(imgs, h) } - enc := json.NewEncoder(os.Stdout) - return enc.Encode(imgs) + prettyJSON, err := json.MarshalIndent(imgs, "", " ") + if err != nil { + return err + } + fmt.Println(string(prettyJSON)) + return nil } -func writeTemplate(imageS []*entities.ImageSummary) error { +func writeTemplate(imgs []imageReporter) error { var ( hdr, row string ) + if len(listFlag.format) < 1 { + hdr, row = imageListFormat(listFlag) + } else { + row = listFlag.format + if !strings.HasSuffix(row, "\n") { + row += "\n" + } + } + format := hdr + "{{range . }}" + row + "{{end}}" + tmpl := template.Must(template.New("list").Parse(format)) + w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) + defer w.Flush() + return tmpl.Execute(w, imgs) +} + +func sortImages(imageS []*entities.ImageSummary) []imageReporter { imgs := make([]imageReporter, 0, len(imageS)) for _, e := range imageS { var h imageReporter @@ -167,20 +189,7 @@ func writeTemplate(imageS []*entities.ImageSummary) error { } sort.Slice(imgs, sortFunc(listFlag.sort, imgs)) - - if len(listFlag.format) < 1 { - hdr, row = imageListFormat(listFlag) - } else { - row = listFlag.format - if !strings.HasSuffix(row, "\n") { - row += "\n" - } - } - format := hdr + "{{range . }}" + row + "{{end}}" - tmpl := template.Must(template.New("list").Parse(format)) - w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0) - defer w.Flush() - return tmpl.Execute(w, imgs) + return imgs } func tokenRepoTag(tag string) (string, string) { diff --git a/libpod/events/events.go b/libpod/events/events.go index 0d8c6b7d6..0253b1ee5 100644 --- a/libpod/events/events.go +++ b/libpod/events/events.go @@ -202,5 +202,5 @@ func (e EventLogFile) getTail(options ReadOptions) (*tail.Tail, error) { if len(options.Until) > 0 { stream = false } - return tail.TailFile(e.options.LogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger}) + return tail.TailFile(e.options.LogFilePath, tail.Config{ReOpen: reopen, Follow: stream, Location: &seek, Logger: tail.DiscardingLogger, Poll: true}) } diff --git a/pkg/bindings/containers/attach.go b/pkg/bindings/containers/attach.go index 44c7f4002..22ab2d72d 100644 --- a/pkg/bindings/containers/attach.go +++ b/pkg/bindings/containers/attach.go @@ -178,25 +178,28 @@ func Attach(ctx context.Context, nameOrID string, detachKeys *string, logs, stre } switch { - case fd == 0 && isSet.stdout: - _, err := stdout.Write(frame[0:l]) - if err != nil { - return err + case fd == 0: + if isSet.stdout { + if _, err := stdout.Write(frame[0:l]); err != nil { + return err + } } - case fd == 1 && isSet.stdout: - _, err := stdout.Write(frame[0:l]) - if err != nil { - return err + case fd == 1: + if isSet.stdout { + if _, err := stdout.Write(frame[0:l]); err != nil { + return err + } } - case fd == 2 && isSet.stderr: - _, err := stderr.Write(frame[0:l]) - if err != nil { - return err + case fd == 2: + if isSet.stderr { + if _, err := stderr.Write(frame[0:l]); err != nil { + return err + } } case fd == 3: return fmt.Errorf("error from service from stream: %s", frame) default: - return fmt.Errorf("unrecognized channel in header: %d, 0-3 supported", fd) + return fmt.Errorf("unrecognized channel '%d' in header, 0-3 supported", fd) } } } @@ -453,27 +456,30 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, streams *define.A } switch { - case fd == 0 && streams.AttachOutput: - _, err := streams.OutputStream.Write(frame[0:l]) - if err != nil { - return err + case fd == 0: + if streams.AttachOutput { + if _, err := streams.OutputStream.Write(frame[0:l]); err != nil { + return err + } } - case fd == 1 && streams.AttachInput: - // Write STDIN to STDOUT (echoing characters - // typed by another attach session) - _, err := streams.OutputStream.Write(frame[0:l]) - if err != nil { - return err + case fd == 1: + if streams.AttachInput { + // Write STDIN to STDOUT (echoing characters + // typed by another attach session) + if _, err := streams.OutputStream.Write(frame[0:l]); err != nil { + return err + } } - case fd == 2 && streams.AttachError: - _, err := streams.ErrorStream.Write(frame[0:l]) - if err != nil { - return err + case fd == 2: + if streams.AttachError { + if _, err := streams.ErrorStream.Write(frame[0:l]); err != nil { + return err + } } case fd == 3: return fmt.Errorf("error from service from stream: %s", frame) default: - return fmt.Errorf("unrecognized channel in header: %d, 0-3 supported", fd) + return fmt.Errorf("unrecognized channel '%d' in header, 0-3 supported", fd) } } } diff --git a/test/e2e/images_test.go b/test/e2e/images_test.go index 0ee7260c2..b6391cebf 100644 --- a/test/e2e/images_test.go +++ b/test/e2e/images_test.go @@ -296,6 +296,15 @@ WORKDIR /test sortValueTest("id", 125, "badvalue") }) + It("test for issue #6670", func() { + expected := podmanTest.Podman([]string{"images", "--sort", "created", "--format", "{{.ID}}", "-q"}) + expected.WaitWithDefaultTimeout() + + actual := podmanTest.Podman([]string{"images", "--sort", "created", "-q"}) + actual.WaitWithDefaultTimeout() + Expect(expected.Out).Should(Equal(actual.Out)) + }) + It("podman images --all flag", func() { podmanTest.RestoreAllArtifacts() dockerfile := `FROM docker.io/library/alpine:latest |