history.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
  19. noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
  20. cmd.Require(flag.Exact, 1)
  21. cmd.ParseFlags(args, true)
  22. rdr, _, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil, nil)
  23. if err != nil {
  24. return err
  25. }
  26. history := []types.ImageHistory{}
  27. err = json.NewDecoder(rdr).Decode(&history)
  28. if 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. fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
  43. if *noTrunc {
  44. fmt.Fprintf(w, "%s\t", entry.CreatedBy)
  45. } else {
  46. fmt.Fprintf(w, "%s\t", stringutils.Truncate(entry.CreatedBy, 45))
  47. }
  48. fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
  49. fmt.Fprintf(w, "%s", entry.Comment)
  50. }
  51. fmt.Fprintf(w, "\n")
  52. }
  53. w.Flush()
  54. return nil
  55. }