2015-03-24 23:57:23 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2015-04-03 11:31:30 -04:00
|
|
|
"encoding/json"
|
2015-03-24 23:57:23 -04:00
|
|
|
"fmt"
|
2015-07-07 16:10:29 -04:00
|
|
|
"strings"
|
2015-03-24 23:57:23 -04:00
|
|
|
"text/tabwriter"
|
|
|
|
"time"
|
|
|
|
|
2015-04-03 11:31:30 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-05-05 00:18:28 -04:00
|
|
|
Cli "github.com/docker/docker/cli"
|
2015-03-24 23:57:23 -04:00
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-03-29 17:17:23 -04:00
|
|
|
"github.com/docker/docker/pkg/stringutils"
|
2015-03-24 23:57:23 -04:00
|
|
|
"github.com/docker/docker/pkg/units"
|
|
|
|
)
|
|
|
|
|
2015-03-25 13:34:41 -04:00
|
|
|
// CmdHistory shows the history of an image.
|
|
|
|
//
|
|
|
|
// Usage: docker history [OPTIONS] IMAGE
|
2015-03-24 23:57:23 -04:00
|
|
|
func (cli *DockerCli) CmdHistory(args ...string) error {
|
2015-05-05 00:18:28 -04:00
|
|
|
cmd := Cli.Subcmd("history", []string{"IMAGE"}, "Show the history of an image", true)
|
2015-04-16 11:29:04 -04:00
|
|
|
human := cmd.Bool([]string{"H", "-human"}, true, "Print sizes and dates in human readable format")
|
2015-03-24 23:57:23 -04:00
|
|
|
quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
|
|
|
|
noTrunc := cmd.Bool([]string{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
|
|
|
|
cmd.Require(flag.Exact, 1)
|
2015-07-03 05:26:09 -04:00
|
|
|
|
2015-03-28 21:22:46 -04:00
|
|
|
cmd.ParseFlags(args, true)
|
2015-03-24 23:57:23 -04:00
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
serverResp, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil, nil)
|
2015-03-24 23:57:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-09 22:05:50 -04:00
|
|
|
defer serverResp.body.Close()
|
2015-07-03 02:19:23 -04:00
|
|
|
|
2015-04-03 11:31:30 -04:00
|
|
|
history := []types.ImageHistory{}
|
2015-07-09 22:05:50 -04:00
|
|
|
if err := json.NewDecoder(serverResp.body).Decode(&history); err != nil {
|
2015-03-24 23:57:23 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
|
|
|
|
if !*quiet {
|
2015-01-04 01:47:01 -05:00
|
|
|
fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
|
2015-04-03 11:31:30 -04:00
|
|
|
for _, entry := range history {
|
|
|
|
if *noTrunc {
|
|
|
|
fmt.Fprintf(w, entry.ID)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, stringid.TruncateID(entry.ID))
|
|
|
|
}
|
2015-03-24 23:57:23 -04:00
|
|
|
if !*quiet {
|
2015-04-16 11:29:04 -04:00
|
|
|
if *human {
|
2015-05-14 20:31:34 -04:00
|
|
|
fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
|
2015-04-16 11:29:04 -04:00
|
|
|
} else {
|
2015-05-14 20:31:34 -04:00
|
|
|
fmt.Fprintf(w, "\t%s\t", time.Unix(entry.Created, 0).Format(time.RFC3339))
|
2015-04-16 11:29:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if *noTrunc {
|
2015-07-07 16:10:29 -04:00
|
|
|
fmt.Fprintf(w, "%s\t", strings.Replace(entry.CreatedBy, "\t", " ", -1))
|
2015-03-24 23:57:23 -04:00
|
|
|
} else {
|
2015-07-07 16:10:29 -04:00
|
|
|
fmt.Fprintf(w, "%s\t", stringutils.Truncate(strings.Replace(entry.CreatedBy, "\t", " ", -1), 45))
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-04-16 11:29:04 -04:00
|
|
|
|
|
|
|
if *human {
|
|
|
|
fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "%d\t", entry.Size)
|
|
|
|
}
|
|
|
|
|
2015-04-08 19:43:25 -04:00
|
|
|
fmt.Fprintf(w, "%s", entry.Comment)
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
2015-04-03 11:31:30 -04:00
|
|
|
fmt.Fprintf(w, "\n")
|
2015-03-24 23:57:23 -04:00
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
return nil
|
|
|
|
}
|