2016-06-05 18:13:55 -04:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/client"
|
|
|
|
"github.com/docker/docker/cli"
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type diffOptions struct {
|
|
|
|
container string
|
|
|
|
}
|
|
|
|
|
2016-06-21 03:28:30 -04:00
|
|
|
// NewDiffCommand creates a new cobra.Command for `docker diff`
|
2016-06-05 18:13:55 -04:00
|
|
|
func NewDiffCommand(dockerCli *client.DockerCli) *cobra.Command {
|
|
|
|
var opts diffOptions
|
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "diff CONTAINER",
|
|
|
|
Short: "Inspect changes on a container's filesystem",
|
|
|
|
Args: cli.ExactArgs(1),
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
opts.container = args[0]
|
|
|
|
return runDiff(dockerCli, &opts)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.SetFlagErrorFunc(flagErrorFunc)
|
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDiff(dockerCli *client.DockerCli, opts *diffOptions) error {
|
2016-06-06 07:58:29 -04:00
|
|
|
if opts.container == "" {
|
|
|
|
return fmt.Errorf("Container name cannot be empty")
|
|
|
|
}
|
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"
|
|
|
|
}
|
|
|
|
fmt.Fprintf(dockerCli.Out(), "%s %s\n", kind, change.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|