history.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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", []string{"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. defer rdr.Close()
  28. history := []types.ImageHistory{}
  29. if err := json.NewDecoder(rdr).Decode(&history); err != nil {
  30. return err
  31. }
  32. w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
  33. if !*quiet {
  34. fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
  35. }
  36. for _, entry := range history {
  37. if *noTrunc {
  38. fmt.Fprintf(w, entry.ID)
  39. } else {
  40. fmt.Fprintf(w, stringid.TruncateID(entry.ID))
  41. }
  42. if !*quiet {
  43. if *human {
  44. fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
  45. } else {
  46. fmt.Fprintf(w, "\t%s\t", time.Unix(entry.Created, 0).Format(time.RFC3339))
  47. }
  48. if *noTrunc {
  49. fmt.Fprintf(w, "%s\t", entry.CreatedBy)
  50. } else {
  51. fmt.Fprintf(w, "%s\t", stringutils.Truncate(entry.CreatedBy, 45))
  52. }
  53. if *human {
  54. fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
  55. } else {
  56. fmt.Fprintf(w, "%d\t", entry.Size)
  57. }
  58. fmt.Fprintf(w, "%s", entry.Comment)
  59. }
  60. fmt.Fprintf(w, "\n")
  61. }
  62. w.Flush()
  63. return nil
  64. }