Use spf13/cobra for docker diff

This fix is part of the effort to convert commands to spf13/cobra #23211.

Thif fix coverted command `docker diff` to use spf13/cobra

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-06-05 15:13:55 -07:00
parent 951702a567
commit 5899afae52
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.NewStopCommand(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).")
}