2016-06-05 18:13:55 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2016-12-24 19:05:37 -05:00
|
|
|
"errors"
|
2016-06-05 18:13:55 -04:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/docker/docker/cli"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/cli/command"
|
2016-06-05 18:13:55 -04:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/spf13/cobra"
|
2016-12-24 19:05:37 -05:00
|
|
|
"golang.org/x/net/context"
|
2016-06-05 18:13:55 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type diffOptions struct {
|
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
2016-06-21 03:28:30 -04:00
|
|
|
// NewDiffCommand creates a new cobra.Command for `docker diff`
|
2016-09-08 13:11:39 -04:00
|
|
|
func NewDiffCommand(dockerCli *command.DockerCli) *cobra.Command {
|
2016-06-05 18:13:55 -04:00
|
|
|
var opts diffOptions
|
|
|
|
|
2016-06-22 18:36:51 -04:00
|
|
|
return &cobra.Command{
|
2016-06-05 18:13:55 -04:00
|
|
|
Use: "diff CONTAINER",
|
2016-12-20 14:47:54 -05:00
|
|
|
Short: "Inspect changes to files or directories on a container's filesystem",
|
2016-06-05 18:13:55 -04:00
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
return runDiff(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func runDiff(dockerCli *command.DockerCli, opts *diffOptions) error {
|
2016-06-06 07:58:29 -04:00
|
|
|
if opts.container == "" {
|
2016-12-24 19:05:37 -05:00
|
|
|
return errors.New("Container name cannot be empty")
|
2016-06-06 07:58:29 -04:00
|
|
|
}
|
2016-06-05 18:13:55 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
changes, err := dockerCli.Client().ContainerDiff(ctx, opts.container)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, change := range changes {
|
|
|
|
var kind string
|
|
|
|
switch change.Kind {
|
|
|
|
case archive.ChangeModify:
|
|
|
|
kind = "C"
|
|
|
|
case archive.ChangeAdd:
|
|
|
|
kind = "A"
|
|
|
|
case archive.ChangeDelete:
|
|
|
|
kind = "D"
|
|
|
|
}
|
2016-12-24 19:05:37 -05:00
|
|
|
fmt.Fprintln(dockerCli.Out(), kind, change.Path)
|
2016-06-05 18:13:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|