2017-08-01 20:47:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/containers/storage/pkg/archive"
|
2017-08-14 19:32:00 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/cmd/kpod/formats"
|
2017-08-01 20:47:12 +00:00
|
|
|
"github.com/kubernetes-incubator/cri-o/libkpod"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
type diffJSONOutput struct {
|
|
|
|
Changed []string `json:"changed,omitempty"`
|
|
|
|
Added []string `json:"added,omitempty"`
|
|
|
|
Deleted []string `json:"deleted,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:47:12 +00:00
|
|
|
var (
|
|
|
|
diffFlags = []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "archive",
|
|
|
|
Usage: "Save the diff as a tar archive",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
2017-08-14 19:32:00 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "format",
|
|
|
|
Usage: "Change the output format.",
|
2017-08-01 20:47:12 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
diffDescription = fmt.Sprint(`Displays changes on a container or image's filesystem. The
|
|
|
|
container or image will be compared to its parent layer`)
|
|
|
|
|
|
|
|
diffCommand = cli.Command{
|
|
|
|
Name: "diff",
|
|
|
|
Usage: "Inspect changes on container's file systems",
|
|
|
|
Description: diffDescription,
|
|
|
|
Flags: diffFlags,
|
|
|
|
Action: diffCmd,
|
|
|
|
ArgsUsage: "ID-NAME",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return jsonStruct, nil
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:47:12 +00:00
|
|
|
func diffCmd(c *cli.Context) error {
|
|
|
|
if len(c.Args()) != 1 {
|
|
|
|
return errors.Errorf("container, layer, or image name must be specified: kpod diff [options [...]] ID-NAME")
|
2017-09-28 18:44:48 +00:00
|
|
|
}
|
|
|
|
if err := validateFlags(c, diffFlags); err != nil {
|
|
|
|
return err
|
2017-08-01 20:47:12 +00:00
|
|
|
}
|
|
|
|
config, err := getConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get config")
|
|
|
|
}
|
|
|
|
|
|
|
|
server, err := libkpod.New(config)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get container server")
|
|
|
|
}
|
2017-08-17 14:23:54 +00:00
|
|
|
defer server.Shutdown()
|
2017-08-01 20:47:12 +00:00
|
|
|
|
|
|
|
to := c.Args().Get(0)
|
|
|
|
changes, err := server.GetDiff("", to)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not get changes for %q", to)
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:32:00 +00:00
|
|
|
diffOutput := []diffOutputParams{}
|
|
|
|
outputFormat := c.String("format")
|
|
|
|
|
|
|
|
for _, change := range changes {
|
|
|
|
|
|
|
|
params := diffOutputParams{
|
|
|
|
Change: change.Kind,
|
|
|
|
Path: change.Path,
|
2017-08-01 20:47:12 +00:00
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
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'")
|
2017-08-01 20:47:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-08-14 19:32:00 +00:00
|
|
|
out = stdoutStruct{output: diffOutput}
|
2017-08-01 20:47:12 +00:00
|
|
|
}
|
2017-08-14 19:32:00 +00:00
|
|
|
formats.Writer(out).Out()
|
2017-08-01 20:47:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|