diff options
author | Paul Holzinger <pholzing@redhat.com> | 2021-07-01 15:24:20 +0200 |
---|---|---|
committer | Paul Holzinger <pholzing@redhat.com> | 2021-07-02 17:11:56 +0200 |
commit | 8f6a0243f4b7f861a116c0dba5967b3cfe23d61f (patch) | |
tree | 4525ea01079f4f61104e34b5f19b0a823c17f6a0 /cmd/podman/diff | |
parent | fd1715568b7c14451dcf2581c385c8d3e307d30e (diff) | |
download | podman-8f6a0243f4b7f861a116c0dba5967b3cfe23d61f.tar.gz podman-8f6a0243f4b7f861a116c0dba5967b3cfe23d61f.tar.bz2 podman-8f6a0243f4b7f861a116c0dba5967b3cfe23d61f.zip |
podman diff accept two images or containers
First, make podman diff accept optionally a second argument. This allows
the user to specify a second image/container to compare the first with.
If it is not set the parent layer will be used as before.
Second, podman container diff should only use containers and podman
image diff should only use images. Previously, podman container diff
would use the image when both an image and container with this name
exists.
To make this work two new parameters have been added to the api. If they
are not used the previous behaviour is used. The same applies to the
bindings.
Fixes #10649
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'cmd/podman/diff')
-rw-r--r-- | cmd/podman/diff/diff.go | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/cmd/podman/diff/diff.go b/cmd/podman/diff/diff.go new file mode 100644 index 000000000..81bbb6c43 --- /dev/null +++ b/cmd/podman/diff/diff.go @@ -0,0 +1,79 @@ +package diff + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/containers/common/pkg/report" + "github.com/containers/podman/v3/cmd/podman/registry" + "github.com/containers/podman/v3/pkg/domain/entities" + "github.com/docker/docker/pkg/archive" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +func Diff(cmd *cobra.Command, args []string, options entities.DiffOptions) error { + results, err := registry.ContainerEngine().Diff(registry.GetContext(), args, options) + if err != nil { + return err + } + + switch { + case report.IsJSON(options.Format): + return changesToJSON(results) + case options.Format == "": + return changesToTable(results) + default: + return errors.New("only supported value for '--format' is 'json'") + } +} + +type ChangesReportJSON struct { + Changed []string `json:"changed,omitempty"` + Added []string `json:"added,omitempty"` + Deleted []string `json:"deleted,omitempty"` +} + +func changesToJSON(diffs *entities.DiffReport) error { + body := ChangesReportJSON{} + for _, row := range diffs.Changes { + switch row.Kind { + case archive.ChangeAdd: + body.Added = append(body.Added, row.Path) + case archive.ChangeDelete: + body.Deleted = append(body.Deleted, row.Path) + case archive.ChangeModify: + body.Changed = append(body.Changed, row.Path) + default: + return errors.Errorf("output kind %q not recognized", row.Kind) + } + } + + // Pull in configured json library + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + return enc.Encode(body) +} + +func changesToTable(diffs *entities.DiffReport) error { + for _, row := range diffs.Changes { + fmt.Fprintln(os.Stdout, row.String()) + } + return nil +} + +// IDOrLatestArgs used to validate a nameOrId was provided or the "--latest" flag +func ValidateContainerDiffArgs(cmd *cobra.Command, args []string) error { + given, _ := cmd.Flags().GetBool("latest") + if len(args) > 0 && !given { + return cobra.RangeArgs(1, 2)(cmd, args) + } + if len(args) > 0 && given { + return errors.New("--latest and containers cannot be used together") + } + if len(args) == 0 && !given { + return errors.Errorf("%q requires a name, id, or the \"--latest\" flag", cmd.CommandPath()) + } + return nil +} |