2015-03-25 03:57:23 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-03 15:31:30 +00:00
|
|
|
"encoding/json"
|
2015-03-25 03:57:23 +00:00
|
|
|
"fmt"
|
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2015-04-03 15:31:30 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-03-25 03:57:23 +00:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-03-29 21:17:23 +00:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-03-25 03:57:23 +00:00
|
|
|
"github.com/docker/docker/pkg/units"
|
|
|
|
)
|
|
|
|
|
2015-03-25 17:34:41 +00:00
|
|
|
// CmdHistory shows the history of an image.
|
|
|
|
//
|
|
|
|
// Usage: docker history [OPTIONS] IMAGE
|
2015-03-25 03:57:23 +00:00
|
|
|
func (cli *DockerCli) CmdHistory(args ...string) error {
|
2015-05-14 02:45:01 +00:00
|
|
|
cmd := cli.Subcmd("history", []string{"IMAGE"}, "Show the history of an image", true)
|
2015-04-16 15:29:04 +00:00
|
|
|
human := cmd.Bool([]string{"H", "-human"}, true, "Print sizes and dates in human readable format")
|
2015-03-25 03:57:23 +00:00
|
|
|
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
|
|
|
|
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
|
|
|
cmd.Require(flag.Exact, 1)
|
2015-07-03 09:26:09 +00:00
|
|
|
|
2015-03-29 01:22:46 +00:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-25 03:57:23 +00:00
|
|
|
|
2015-05-14 01:36:36 +00:00
|
|
|
rdr, _, _, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil, nil)
|
2015-03-25 03:57:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-03 06:19:23 +00:00
|
|
|
defer rdr.Close()
|
|
|
|
|
2015-04-03 15:31:30 +00:00
|
|
|
history := []types.ImageHistory{}
|
2015-04-26 16:50:25 +00:00
|
|
|
if err := json.NewDecoder(rdr).Decode(&history); err != nil {
|
2015-03-25 03:57:23 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
if !*quiet {
|
2015-01-04 06:47:01 +00:00
|
|
|
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 15:31:30 +00:00
|
|
|
for _, entry := range history {
|
|
|
|
if *noTrunc {
|
|
|
|
fmt.Fprintf(w, entry.ID)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, stringid.TruncateID(entry.ID))
|
|
|
|
}
|
2015-03-25 03:57:23 +00:00
|
|
|
if !*quiet {
|
2015-04-16 15:29:04 +00:00
|
|
|
if *human {
|
2015-05-15 00:31:34 +00:00
|
|
|
fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
|
2015-04-16 15:29:04 +00:00
|
|
|
} else {
|
2015-05-15 00:31:34 +00:00
|
|
|
fmt.Fprintf(w, "\t%s\t", time.Unix(entry.Created, 0).Format(time.RFC3339))
|
2015-04-16 15:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *noTrunc {
|
2015-04-03 15:31:30 +00:00
|
|
|
fmt.Fprintf(w, "%s\t", entry.CreatedBy)
|
2015-03-25 03:57:23 +00:00
|
|
|
} else {
|
2015-03-29 21:17:23 +00:00
|
|
|
fmt.Fprintf(w, "%s\t", stringutils.Truncate(entry.CreatedBy, 45))
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-04-16 15:29:04 +00:00
|
|
|
|
|
|
|
if *human {
|
|
|
|
fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "%d\t", entry.Size)
|
|
|
|
}
|
|
|
|
|
2015-04-08 23:43:25 +00:00
|
|
|
fmt.Fprintf(w, "%s", entry.Comment)
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
2015-04-03 15:31:30 +00:00
|
|
|
fmt.Fprintf(w, "\n")
|
2015-03-25 03:57:23 +00:00
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|