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