mirror of
https://github.com/containers/podman.git
synced 2025-12-04 04:09:40 +08:00
Podman V2 birth
remote podman v1 and replace with podman v2. Signed-off-by: Brent Baude <bbaude@redhat.com>
This commit is contained in:
@@ -3,139 +3,59 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/containers/buildah/pkg/formats"
|
||||
"github.com/containers/libpod/cmd/podman/cliconfig"
|
||||
"github.com/containers/libpod/pkg/adapter"
|
||||
"github.com/containers/storage/pkg/archive"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/containers/libpod/cmd/podman/containers"
|
||||
"github.com/containers/libpod/cmd/podman/images"
|
||||
"github.com/containers/libpod/cmd/podman/registry"
|
||||
"github.com/containers/libpod/pkg/domain/entities"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type diffJSONOutput struct {
|
||||
Changed []string `json:"changed,omitempty"`
|
||||
Added []string `json:"added,omitempty"`
|
||||
Deleted []string `json:"deleted,omitempty"`
|
||||
}
|
||||
|
||||
type diffOutputParams struct {
|
||||
Change archive.ChangeType
|
||||
Path string
|
||||
}
|
||||
|
||||
type stdoutStruct struct {
|
||||
output []diffOutputParams
|
||||
}
|
||||
|
||||
func (so stdoutStruct) Out() error {
|
||||
for _, d := range so.output {
|
||||
fmt.Printf("%s %s\n", d.Change, d.Path)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Inspect is one of the outlier commands in that it operates on images/containers/...
|
||||
|
||||
var (
|
||||
diffCommand cliconfig.DiffValues
|
||||
diffDescription = fmt.Sprint(`Displays changes on a container or image's filesystem. The container or image will be compared to its parent layer.`)
|
||||
|
||||
_diffCommand = &cobra.Command{
|
||||
Use: "diff [flags] CONTAINER | IMAGE",
|
||||
Short: "Inspect changes on container's file systems",
|
||||
Long: diffDescription,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
diffCommand.InputArgs = args
|
||||
diffCommand.GlobalFlags = MainGlobalOpts
|
||||
diffCommand.Remote = remoteclient
|
||||
return diffCmd(&diffCommand)
|
||||
},
|
||||
// Command: podman _diff_ Object_ID
|
||||
diffDescription = `Displays changes on a container or image's filesystem. The container or image will be compared to its parent layer.`
|
||||
diffCmd = &cobra.Command{
|
||||
Use: "diff [flags] {CONTAINER_ID | IMAGE_ID}",
|
||||
Args: registry.IdOrLatestArgs,
|
||||
Short: "Display the changes of object's file system",
|
||||
Long: diffDescription,
|
||||
TraverseChildren: true,
|
||||
RunE: diff,
|
||||
Example: `podman diff imageID
|
||||
podman diff ctrID
|
||||
podman diff --format json redis:alpine`,
|
||||
}
|
||||
|
||||
diffOpts = entities.DiffOptions{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
diffCommand.Command = _diffCommand
|
||||
diffCommand.SetHelpTemplate(HelpTemplate())
|
||||
diffCommand.SetUsageTemplate(UsageTemplate())
|
||||
flags := diffCommand.Flags()
|
||||
|
||||
flags.BoolVar(&diffCommand.Archive, "archive", true, "Save the diff as a tar archive")
|
||||
flags.StringVar(&diffCommand.Format, "format", "", "Change the output format")
|
||||
flags.BoolVarP(&diffCommand.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
|
||||
markFlagHidden(flags, "archive")
|
||||
markFlagHiddenForRemoteClient("latest", flags)
|
||||
registry.Commands = append(registry.Commands, registry.CliCommand{
|
||||
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
|
||||
Command: diffCmd,
|
||||
})
|
||||
flags := diffCmd.Flags()
|
||||
flags.BoolVar(&diffOpts.Archive, "archive", true, "Save the diff as a tar archive")
|
||||
_ = flags.MarkHidden("archive")
|
||||
flags.StringVar(&diffOpts.Format, "format", "", "Change the output format")
|
||||
|
||||
if !registry.IsRemote() {
|
||||
flags.BoolVarP(&diffOpts.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
|
||||
}
|
||||
}
|
||||
|
||||
func formatJSON(output []diffOutputParams) (diffJSONOutput, error) {
|
||||
jsonStruct := diffJSONOutput{}
|
||||
for _, output := range output {
|
||||
switch output.Change {
|
||||
case archive.ChangeModify:
|
||||
jsonStruct.Changed = append(jsonStruct.Changed, output.Path)
|
||||
case archive.ChangeAdd:
|
||||
jsonStruct.Added = append(jsonStruct.Added, output.Path)
|
||||
case archive.ChangeDelete:
|
||||
jsonStruct.Deleted = append(jsonStruct.Deleted, output.Path)
|
||||
default:
|
||||
return jsonStruct, errors.Errorf("output kind %q not recognized", output.Change.String())
|
||||
}
|
||||
func diff(cmd *cobra.Command, args []string) error {
|
||||
if found, err := registry.ImageEngine().Exists(registry.GetContext(), args[0]); err != nil {
|
||||
return err
|
||||
} else if found.Value {
|
||||
return images.Diff(cmd, args, diffOpts)
|
||||
}
|
||||
return jsonStruct, nil
|
||||
}
|
||||
|
||||
func diffCmd(c *cliconfig.DiffValues) error {
|
||||
if len(c.InputArgs) != 1 && !c.Latest {
|
||||
return errors.Errorf("container, image, or layer name must be specified: podman diff [options [...]] ID-NAME")
|
||||
}
|
||||
|
||||
runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "could not get runtime")
|
||||
}
|
||||
defer runtime.DeferredShutdown(false)
|
||||
|
||||
var to string
|
||||
if c.Latest {
|
||||
ctr, err := runtime.GetLatestContainer()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unable to get latest container")
|
||||
}
|
||||
to = ctr.ID()
|
||||
} else {
|
||||
to = c.InputArgs[0]
|
||||
}
|
||||
changes, err := runtime.Diff(c, to)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "could not get changes for %q", to)
|
||||
}
|
||||
diffOutput := []diffOutputParams{}
|
||||
outputFormat := c.Format
|
||||
|
||||
for _, change := range changes {
|
||||
|
||||
params := diffOutputParams{
|
||||
Change: change.Kind,
|
||||
Path: change.Path,
|
||||
}
|
||||
diffOutput = append(diffOutput, params)
|
||||
}
|
||||
|
||||
var out formats.Writer
|
||||
|
||||
if outputFormat != "" {
|
||||
switch outputFormat {
|
||||
case formats.JSONString:
|
||||
data, err := formatJSON(diffOutput)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out = formats.JSONStruct{Output: data}
|
||||
default:
|
||||
return errors.New("only valid format for diff is 'json'")
|
||||
}
|
||||
} else {
|
||||
out = stdoutStruct{output: diffOutput}
|
||||
}
|
||||
return out.Out()
|
||||
|
||||
if found, err := registry.ContainerEngine().ContainerExists(registry.GetContext(), args[0]); err != nil {
|
||||
return err
|
||||
} else if found.Value {
|
||||
return containers.Diff(cmd, args, diffOpts)
|
||||
}
|
||||
return fmt.Errorf("%s not found on system", args[0])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user