github.com/kunnos/engine@v1.13.1/cli/command/container/diff.go (about)

     1  package container
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/cli"
     9  	"github.com/docker/docker/cli/command"
    10  	"github.com/docker/docker/pkg/archive"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type diffOptions struct {
    15  	container string
    16  }
    17  
    18  // NewDiffCommand creates a new cobra.Command for `docker diff`
    19  func NewDiffCommand(dockerCli *command.DockerCli) *cobra.Command {
    20  	var opts diffOptions
    21  
    22  	return &cobra.Command{
    23  		Use:   "diff CONTAINER",
    24  		Short: "Inspect changes on a container's filesystem",
    25  		Args:  cli.ExactArgs(1),
    26  		RunE: func(cmd *cobra.Command, args []string) error {
    27  			opts.container = args[0]
    28  			return runDiff(dockerCli, &opts)
    29  		},
    30  	}
    31  }
    32  
    33  func runDiff(dockerCli *command.DockerCli, opts *diffOptions) error {
    34  	if opts.container == "" {
    35  		return fmt.Errorf("Container name cannot be empty")
    36  	}
    37  	ctx := context.Background()
    38  
    39  	changes, err := dockerCli.Client().ContainerDiff(ctx, opts.container)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	for _, change := range changes {
    45  		var kind string
    46  		switch change.Kind {
    47  		case archive.ChangeModify:
    48  			kind = "C"
    49  		case archive.ChangeAdd:
    50  			kind = "A"
    51  		case archive.ChangeDelete:
    52  			kind = "D"
    53  		}
    54  		fmt.Fprintf(dockerCli.Out(), "%s %s\n", kind, change.Path)
    55  	}
    56  
    57  	return nil
    58  }