From 26013fcdd64221c858022392a1430a6c43eaaa26 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Thu, 19 Jan 2017 09:50:28 +0000 Subject: [PATCH] Fix broken JSON support in cli/command/formatter How to test (it should not print `{}`, and just returns JSON with the actual data): $ docker images --format '{{json .}}' $ docker container stats --format '{{json .}}' Signed-off-by: Akihiro Suda --- cli/command/formatter/disk_usage.go | 12 ++++++++++++ cli/command/formatter/image.go | 4 ++++ cli/command/formatter/stats.go | 4 ++++ 3 files changed, 20 insertions(+) diff --git a/cli/command/formatter/disk_usage.go b/cli/command/formatter/disk_usage.go index f4abac59e6..ff1ab768c0 100644 --- a/cli/command/formatter/disk_usage.go +++ b/cli/command/formatter/disk_usage.go @@ -158,6 +158,10 @@ type diskUsageImagesContext struct { images []*types.ImageSummary } +func (c *diskUsageImagesContext) MarshalJSON() ([]byte, error) { + return marshalJSON(c) +} + func (c *diskUsageImagesContext) Type() string { c.AddHeader(typeHeader) return "Images" @@ -209,6 +213,10 @@ type diskUsageContainersContext struct { containers []*types.Container } +func (c *diskUsageContainersContext) MarshalJSON() ([]byte, error) { + return marshalJSON(c) +} + func (c *diskUsageContainersContext) Type() string { c.AddHeader(typeHeader) return "Containers" @@ -273,6 +281,10 @@ type diskUsageVolumesContext struct { volumes []*types.Volume } +func (c *diskUsageVolumesContext) MarshalJSON() ([]byte, error) { + return marshalJSON(c) +} + func (c *diskUsageVolumesContext) Type() string { c.AddHeader(typeHeader) return "Local Volumes" diff --git a/cli/command/formatter/image.go b/cli/command/formatter/image.go index 9187dfb2e2..3dbb1b9642 100644 --- a/cli/command/formatter/image.go +++ b/cli/command/formatter/image.go @@ -190,6 +190,10 @@ type imageContext struct { digest string } +func (c *imageContext) MarshalJSON() ([]byte, error) { + return marshalJSON(c) +} + func (c *imageContext) ID() string { c.AddHeader(imageIDHeader) if c.trunc { diff --git a/cli/command/formatter/stats.go b/cli/command/formatter/stats.go index 7997f996d8..a37e9d7923 100644 --- a/cli/command/formatter/stats.go +++ b/cli/command/formatter/stats.go @@ -138,6 +138,10 @@ type containerStatsContext struct { s StatsEntry } +func (c *containerStatsContext) MarshalJSON() ([]byte, error) { + return marshalJSON(c) +} + func (c *containerStatsContext) Container() string { c.AddHeader(containerHeader) return c.s.Container