Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / docker / cli / cli / command / formatter / history.go
1 package formatter
2
3 import (
4         "strconv"
5         "strings"
6         "time"
7
8         "github.com/docker/docker/api/types/image"
9         "github.com/docker/docker/pkg/stringid"
10         "github.com/docker/docker/pkg/stringutils"
11         units "github.com/docker/go-units"
12 )
13
14 const (
15         defaultHistoryTableFormat  = "table {{.ID}}\t{{.CreatedSince}}\t{{.CreatedBy}}\t{{.Size}}\t{{.Comment}}"
16         nonHumanHistoryTableFormat = "table {{.ID}}\t{{.CreatedAt}}\t{{.CreatedBy}}\t{{.Size}}\t{{.Comment}}"
17
18         historyIDHeader = "IMAGE"
19         createdByHeader = "CREATED BY"
20         commentHeader   = "COMMENT"
21 )
22
23 // NewHistoryFormat returns a format for rendering an HistoryContext
24 func NewHistoryFormat(source string, quiet bool, human bool) Format {
25         switch source {
26         case TableFormatKey:
27                 switch {
28                 case quiet:
29                         return defaultQuietFormat
30                 case !human:
31                         return nonHumanHistoryTableFormat
32                 default:
33                         return defaultHistoryTableFormat
34                 }
35         }
36
37         return Format(source)
38 }
39
40 // HistoryWrite writes the context
41 func HistoryWrite(ctx Context, human bool, histories []image.HistoryResponseItem) error {
42         render := func(format func(subContext subContext) error) error {
43                 for _, history := range histories {
44                         historyCtx := &historyContext{trunc: ctx.Trunc, h: history, human: human}
45                         if err := format(historyCtx); err != nil {
46                                 return err
47                         }
48                 }
49                 return nil
50         }
51         historyCtx := &historyContext{}
52         historyCtx.header = map[string]string{
53                 "ID":           historyIDHeader,
54                 "CreatedSince": createdSinceHeader,
55                 "CreatedAt":    createdAtHeader,
56                 "CreatedBy":    createdByHeader,
57                 "Size":         sizeHeader,
58                 "Comment":      commentHeader,
59         }
60         return ctx.Write(historyCtx, render)
61 }
62
63 type historyContext struct {
64         HeaderContext
65         trunc bool
66         human bool
67         h     image.HistoryResponseItem
68 }
69
70 func (c *historyContext) MarshalJSON() ([]byte, error) {
71         return marshalJSON(c)
72 }
73
74 func (c *historyContext) ID() string {
75         if c.trunc {
76                 return stringid.TruncateID(c.h.ID)
77         }
78         return c.h.ID
79 }
80
81 func (c *historyContext) CreatedAt() string {
82         return units.HumanDuration(time.Now().UTC().Sub(time.Unix(c.h.Created, 0)))
83 }
84
85 func (c *historyContext) CreatedSince() string {
86         created := units.HumanDuration(time.Now().UTC().Sub(time.Unix(c.h.Created, 0)))
87         return created + " ago"
88 }
89
90 func (c *historyContext) CreatedBy() string {
91         createdBy := strings.Replace(c.h.CreatedBy, "\t", " ", -1)
92         if c.trunc {
93                 return stringutils.Ellipsis(createdBy, 45)
94         }
95         return createdBy
96 }
97
98 func (c *historyContext) Size() string {
99         if c.human {
100                 return units.HumanSizeWithPrecision(float64(c.h.Size), 3)
101         }
102         return strconv.FormatInt(c.h.Size, 10)
103 }
104
105 func (c *historyContext) Comment() string {
106         return c.h.Comment
107 }