github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/images/diff.go (about)

     1  package images
     2  
     3  import (
     4  	"github.com/containers/libpod/cmd/podmanV2/registry"
     5  	"github.com/containers/libpod/cmd/podmanV2/report"
     6  	"github.com/containers/libpod/pkg/domain/entities"
     7  	"github.com/pkg/errors"
     8  	"github.com/spf13/cobra"
     9  )
    10  
    11  var (
    12  	// podman container _inspect_
    13  	diffCmd = &cobra.Command{
    14  		Use:     "diff [flags] CONTAINER",
    15  		Args:    registry.IdOrLatestArgs,
    16  		Short:   "Inspect changes on image's file systems",
    17  		Long:    `Displays changes on a image's filesystem.  The image will be compared to its parent layer.`,
    18  		PreRunE: preRunE,
    19  		RunE:    diff,
    20  		Example: `podman image diff myImage
    21    podman image diff --format json redis:alpine`,
    22  	}
    23  	diffOpts *entities.DiffOptions
    24  )
    25  
    26  func init() {
    27  	registry.Commands = append(registry.Commands, registry.CliCommand{
    28  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    29  		Command: diffCmd,
    30  		Parent:  imageCmd,
    31  	})
    32  
    33  	diffOpts = &entities.DiffOptions{}
    34  	flags := diffCmd.Flags()
    35  	flags.BoolVar(&diffOpts.Archive, "archive", true, "Save the diff as a tar archive")
    36  	_ = flags.MarkHidden("archive")
    37  	flags.StringVar(&diffOpts.Format, "format", "", "Change the output format")
    38  }
    39  
    40  func diff(cmd *cobra.Command, args []string) error {
    41  	if len(args) == 0 && !diffOpts.Latest {
    42  		return errors.New("image must be specified: podman image diff [options [...]] ID-NAME")
    43  	}
    44  
    45  	results, err := registry.ImageEngine().Diff(registry.GetContext(), args[0], entities.DiffOptions{})
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	switch diffOpts.Format {
    51  	case "":
    52  		return report.ChangesToTable(results)
    53  	case "json":
    54  		return report.ChangesToJSON(results)
    55  	default:
    56  		return errors.New("only supported value for '--format' is 'json'")
    57  	}
    58  }
    59  
    60  func Diff(cmd *cobra.Command, args []string, options entities.DiffOptions) error {
    61  	diffOpts = &options
    62  	return diff(cmd, args)
    63  }