history.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package client
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "text/tabwriter"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. flag "github.com/docker/docker/pkg/mflag"
  9. "github.com/docker/docker/pkg/stringid"
  10. "github.com/docker/docker/pkg/stringutils"
  11. "github.com/docker/docker/pkg/units"
  12. )
  13. // CmdHistory shows the history of an image.
  14. //
  15. // Usage: docker history [OPTIONS] IMAGE
  16. func (cli *DockerCli) CmdHistory(args ...string) error {
  17. cmd := cli.Subcmd("history", "IMAGE", "Show the history of an image", true)
  18. human := cmd.Bool([]string{"H", "-human"}, true, "Print sizes and dates in human readable format")
  19. quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
  20. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  21. cmd.Require(flag.Exact, 1)
  22. cmd.ParseFlags(args, true)
  23. rdr, _, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil, nil)
  24. if err != nil {
  25. return err
  26. }
  27. history := []types.ImageHistory{}
  28. if err := json.NewDecoder(rdr).Decode(&history); err != nil {
  29. return err
  30. }
  31. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  32. if !*quiet {
  33. fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
  34. }
  35. for _, entry := range history {
  36. if *noTrunc {
  37. fmt.Fprintf(w, entry.ID)
  38. } else {
  39. fmt.Fprintf(w, stringid.TruncateID(entry.ID))
  40. }
  41. if !*quiet {
  42. if *human {
  43. fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
  44. } else {
  45. fmt.Fprintf(w, "\t%s\t", time.Unix(entry.Created, 0).Format(time.RFC3339))
  46. }
  47. if *noTrunc {
  48. fmt.Fprintf(w, "%s\t", entry.CreatedBy)
  49. } else {
  50. fmt.Fprintf(w, "%s\t", stringutils.Truncate(entry.CreatedBy, 45))
  51. }
  52. if *human {
  53. fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
  54. } else {
  55. fmt.Fprintf(w, "%d\t", entry.Size)
  56. }
  57. fmt.Fprintf(w, "%s", entry.Comment)
  58. }
  59. fmt.Fprintf(w, "\n")
  60. }
  61. w.Flush()
  62. return nil
  63. }