Merge pull request #23283 from yongtang/23211-spf13-cobra-diff

Use spf13/cobra for docker diff
This commit is contained in:
Vincent Demeester 2016-06-06 09:05:18 +02:00
commit 2becfab55f
6 changed files with 60 additions and 52 deletions

View file

@ -7,7 +7,6 @@ func (cli *DockerCli) Command(name string) func(...string) error {
"build": cli.CmdBuild,
"commit": cli.CmdCommit,
"cp": cli.CmdCp,
"diff": cli.CmdDiff,
"events": cli.CmdEvents,
"exec": cli.CmdExec,
"history": cli.CmdHistory,

View file

@ -0,0 +1,58 @@
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
}
// NewDiffCommand creats a new cobra.Command for `docker diff`
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 {
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
}

View file

@ -1,49 +0,0 @@
package client
import (
"fmt"
"golang.org/x/net/context"
Cli "github.com/docker/docker/cli"
"github.com/docker/docker/pkg/archive"
flag "github.com/docker/docker/pkg/mflag"
)
// CmdDiff shows changes on a container's filesystem.
//
// Each changed file is printed on a separate line, prefixed with a single
// character that indicates the status of the file: C (modified), A (added),
// or D (deleted).
//
// Usage: docker diff CONTAINER
func (cli *DockerCli) CmdDiff(args ...string) error {
cmd := Cli.Subcmd("diff", []string{"CONTAINER"}, Cli.DockerCommands["diff"].Description, true)
cmd.Require(flag.Exact, 1)
cmd.ParseFlags(args, true)
if cmd.Arg(0) == "" {
return fmt.Errorf("Container name cannot be empty")
}
changes, err := cli.client.ContainerDiff(context.Background(), cmd.Arg(0))
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(cli.out, "%s %s\n", kind, change.Path)
}
return nil
}

View file

@ -34,6 +34,7 @@ func NewCobraAdaptor(clientFlags *cliflags.ClientFlags) CobraAdaptor {
rootCmd.SetOutput(stdout)
rootCmd.AddCommand(
container.NewCreateCommand(dockerCli),
container.NewDiffCommand(dockerCli),
container.NewExportCommand(dockerCli),
container.NewRunCommand(dockerCli),
container.NewStartCommand(dockerCli),

View file

@ -12,7 +12,6 @@ var DockerCommandUsage = []Command{
{"build", "Build an image from a Dockerfile"},
{"commit", "Create a new image from a container's changes"},
{"cp", "Copy files/folders between a container and the local filesystem"},
{"diff", "Inspect changes on a container's filesystem"},
{"events", "Get real time events from the server"},
{"exec", "Run a command in a running container"},
{"history", "Show the history of an image"},

View file

@ -83,5 +83,5 @@ func (s *DockerSuite) TestDiffEnsureDefaultDevs(c *check.C) {
func (s *DockerSuite) TestDiffEmptyArgClientError(c *check.C) {
out, _, err := dockerCmdWithError("diff", "")
c.Assert(err, checker.NotNil)
c.Assert(strings.TrimSpace(out), checker.Equals, "Container name cannot be empty")
c.Assert(strings.TrimSpace(out), checker.Contains, "\"docker diff\" requires exactly 1 argument(s).")
}