github.com/tsuna/docker@v1.7.0-rc3/api/client/diff.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/docker/docker/api/types"
     8  	"github.com/docker/docker/pkg/archive"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  )
    11  
    12  // CmdDiff shows changes on a container's filesystem.
    13  //
    14  // Each changed file is printed on a separate line, prefixed with a single
    15  // character that indicates the status of the file: C (modified), A (added),
    16  // or D (deleted).
    17  //
    18  // Usage: docker diff CONTAINER
    19  func (cli *DockerCli) CmdDiff(args ...string) error {
    20  	cmd := cli.Subcmd("diff", "CONTAINER", "Inspect changes on a container's filesystem", true)
    21  	cmd.Require(flag.Exact, 1)
    22  	cmd.ParseFlags(args, true)
    23  
    24  	if cmd.Arg(0) == "" {
    25  		return fmt.Errorf("Container name cannot be empty")
    26  	}
    27  
    28  	rdr, _, err := cli.call("GET", "/containers/"+cmd.Arg(0)+"/changes", nil, nil)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	changes := []types.ContainerChange{}
    34  	if err := json.NewDecoder(rdr).Decode(&changes); err != nil {
    35  		return err
    36  	}
    37  
    38  	for _, change := range changes {
    39  		var kind string
    40  		switch change.Kind {
    41  		case archive.ChangeModify:
    42  			kind = "C"
    43  		case archive.ChangeAdd:
    44  			kind = "A"
    45  		case archive.ChangeDelete:
    46  			kind = "D"
    47  		}
    48  		fmt.Fprintf(cli.out, "%s %s\n", kind, change.Path)
    49  	}
    50  
    51  	return nil
    52  }