github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/container/diff.go (about)

     1  package container
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/formatter"
     9  	"github.com/pkg/errors"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type diffOptions struct {
    14  	container string
    15  }
    16  
    17  // NewDiffCommand creates a new cobra.Command for `docker diff`
    18  func NewDiffCommand(dockerCli command.Cli) *cobra.Command {
    19  	var opts diffOptions
    20  
    21  	return &cobra.Command{
    22  		Use:   "diff CONTAINER",
    23  		Short: "Inspect changes to files or directories on a container's filesystem",
    24  		Args:  cli.ExactArgs(1),
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			opts.container = args[0]
    27  			return runDiff(dockerCli, &opts)
    28  		},
    29  	}
    30  }
    31  
    32  func runDiff(dockerCli command.Cli, opts *diffOptions) error {
    33  	if opts.container == "" {
    34  		return errors.New("Container name cannot be empty")
    35  	}
    36  	ctx := context.Background()
    37  
    38  	changes, err := dockerCli.Client().ContainerDiff(ctx, opts.container)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	diffCtx := formatter.Context{
    43  		Output: dockerCli.Out(),
    44  		Format: NewDiffFormat("{{.Type}} {{.Path}}"),
    45  	}
    46  	return DiffFormatWrite(diffCtx, changes)
    47  }