mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
d5179cb4d4
This change does some minor cleanups in the cli/command/container package; - sort imports - replace `fmt.Fprintf()` with `fmt.Fprintln()` if no formatting is used - replace `fmt.Errorf()` with `errors.New()` if no formatting is used - remove some redundant `else`'s Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package container
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/docker/docker/cli"
|
|
"github.com/docker/docker/cli/command"
|
|
"github.com/docker/docker/pkg/archive"
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
type diffOptions struct {
|
|
container string
|
|
}
|
|
|
|
// NewDiffCommand creates a new cobra.Command for `docker diff`
|
|
func NewDiffCommand(dockerCli *command.DockerCli) *cobra.Command {
|
|
var opts diffOptions
|
|
|
|
return &cobra.Command{
|
|
Use: "diff CONTAINER",
|
|
Short: "Inspect changes to files or directories 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)
|
|
},
|
|
}
|
|
}
|
|
|
|
func runDiff(dockerCli *command.DockerCli, opts *diffOptions) error {
|
|
if opts.container == "" {
|
|
return errors.New("Container name cannot be empty")
|
|
}
|
|
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.Fprintln(dockerCli.Out(), kind, change.Path)
|
|
}
|
|
|
|
return nil
|
|
}
|