diff.go 1.1 KB

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