github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/diff.go (about)

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