history.go 1.9 KB

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