github.com/hamo/docker@v1.11.1/api/client/history.go (about)

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