github.com/circular-dark/docker@v1.7.0/api/client/history.go (about)

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