Skip to content

Commit 804536d

Browse files
committed
Merge pull request moby#23291 from yongtang/23211-spf13-cobra-history
Use spf13/cobra for docker history
2 parents e83dad0 + a76d387 commit 804536d

File tree

5 files changed

+100
-78
lines changed

5 files changed

+100
-78
lines changed

api/client/commands.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
88
"cp": cli.CmdCp,
99
"events": cli.CmdEvents,
1010
"exec": cli.CmdExec,
11-
"history": cli.CmdHistory,
1211
"images": cli.CmdImages,
1312
"import": cli.CmdImport,
1413
"info": cli.CmdInfo,

api/client/history.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

api/client/image/history.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package image
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
"text/tabwriter"
8+
"time"
9+
10+
"golang.org/x/net/context"
11+
12+
"github.com/docker/docker/api/client"
13+
"github.com/docker/docker/cli"
14+
"github.com/docker/docker/pkg/stringid"
15+
"github.com/docker/docker/pkg/stringutils"
16+
"github.com/docker/go-units"
17+
"github.com/spf13/cobra"
18+
)
19+
20+
type historyOptions struct {
21+
image string
22+
23+
human bool
24+
quiet bool
25+
noTrunc bool
26+
}
27+
28+
// NewHistoryCommand create a new `docker history` command
29+
func NewHistoryCommand(dockerCli *client.DockerCli) *cobra.Command {
30+
var opts historyOptions
31+
32+
cmd := &cobra.Command{
33+
Use: "history [OPTIONS] IMAGE",
34+
Short: "Show the history of an image",
35+
Args: cli.ExactArgs(1),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
opts.image = args[0]
38+
return runHistory(dockerCli, opts)
39+
},
40+
}
41+
42+
flags := cmd.Flags()
43+
44+
flags.BoolVarP(&opts.human, "human", "H", true, "Print sizes and dates in human readable format")
45+
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show numeric IDs")
46+
flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
47+
48+
return cmd
49+
}
50+
51+
func runHistory(dockerCli *client.DockerCli, opts historyOptions) error {
52+
ctx := context.Background()
53+
54+
history, err := dockerCli.Client().ImageHistory(ctx, opts.image)
55+
if err != nil {
56+
return err
57+
}
58+
59+
w := tabwriter.NewWriter(dockerCli.Out(), 20, 1, 3, ' ', 0)
60+
61+
if opts.quiet {
62+
for _, entry := range history {
63+
if opts.noTrunc {
64+
fmt.Fprintf(w, "%s\n", entry.ID)
65+
} else {
66+
fmt.Fprintf(w, "%s\n", stringid.TruncateID(entry.ID))
67+
}
68+
}
69+
w.Flush()
70+
return nil
71+
}
72+
73+
var imageID string
74+
var createdBy string
75+
var created string
76+
var size string
77+
78+
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
79+
for _, entry := range history {
80+
imageID = entry.ID
81+
createdBy = strings.Replace(entry.CreatedBy, "\t", " ", -1)
82+
if opts.noTrunc == false {
83+
createdBy = stringutils.Truncate(createdBy, 45)
84+
imageID = stringid.TruncateID(entry.ID)
85+
}
86+
87+
if opts.human {
88+
created = units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))) + " ago"
89+
size = units.HumanSize(float64(entry.Size))
90+
} else {
91+
created = time.Unix(entry.Created, 0).Format(time.RFC3339)
92+
size = strconv.FormatInt(entry.Size, 10)
93+
}
94+
95+
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\n", imageID, created, createdBy, size, entry.Comment)
96+
}
97+
w.Flush()
98+
return nil
99+
}

cli/cobraadaptor/adaptor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
4747
container.NewTopCommand(dockerCli),
4848
container.NewUnpauseCommand(dockerCli),
4949
container.NewWaitCommand(dockerCli),
50+
image.NewHistoryCommand(dockerCli),
5051
image.NewRemoveCommand(dockerCli),
5152
image.NewSearchCommand(dockerCli),
5253
network.NewNetworkCommand(dockerCli),

cli/usage.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var DockerCommandUsage = []Command{
1313
{"cp", "Copy files/folders between a container and the local filesystem"},
1414
{"events", "Get real time events from the server"},
1515
{"exec", "Run a command in a running container"},
16-
{"history", "Show the history of an image"},
1716
{"images", "List images"},
1817
{"import", "Import the contents from a tarball to create a filesystem image"},
1918
{"info", "Display system-wide information"},

0 commit comments

Comments
 (0)