github.com/crquan/docker@v1.8.1/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  	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/docker/pkg/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"}, "Show the history of an image", 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{"#notrunc", "-no-trunc"}, false, "Don't truncate output")
    25  	cmd.Require(flag.Exact, 1)
    26  
    27  	cmd.ParseFlags(args, true)
    28  
    29  	serverResp, err := cli.call("GET", "/images/"+cmd.Arg(0)+"/history", nil, nil)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	defer serverResp.body.Close()
    35  
    36  	history := []types.ImageHistory{}
    37  	if err := json.NewDecoder(serverResp.body).Decode(&history); err != nil {
    38  		return err
    39  	}
    40  
    41  	w := tabwriter.NewWriter(cli.out, 20, 1, 3, ' ', 0)
    42  	if !*quiet {
    43  		fmt.Fprintln(w, "IMAGE\tCREATED\tCREATED BY\tSIZE\tCOMMENT")
    44  	}
    45  
    46  	for _, entry := range history {
    47  		if *noTrunc {
    48  			fmt.Fprintf(w, entry.ID)
    49  		} else {
    50  			fmt.Fprintf(w, stringid.TruncateID(entry.ID))
    51  		}
    52  		if !*quiet {
    53  			if *human {
    54  				fmt.Fprintf(w, "\t%s ago\t", units.HumanDuration(time.Now().UTC().Sub(time.Unix(entry.Created, 0))))
    55  			} else {
    56  				fmt.Fprintf(w, "\t%s\t", time.Unix(entry.Created, 0).Format(time.RFC3339))
    57  			}
    58  
    59  			if *noTrunc {
    60  				fmt.Fprintf(w, "%s\t", entry.CreatedBy)
    61  			} else {
    62  				fmt.Fprintf(w, "%s\t", stringutils.Truncate(entry.CreatedBy, 45))
    63  			}
    64  
    65  			if *human {
    66  				fmt.Fprintf(w, "%s\t", units.HumanSize(float64(entry.Size)))
    67  			} else {
    68  				fmt.Fprintf(w, "%d\t", entry.Size)
    69  			}
    70  
    71  			fmt.Fprintf(w, "%s", entry.Comment)
    72  		}
    73  		fmt.Fprintf(w, "\n")
    74  	}
    75  	w.Flush()
    76  	return nil
    77  }