github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/history.go (about)

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