github.com/a4a881d4/docker@v1.9.0-rc2/api/client/history.go (about)

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