summaryrefslogtreecommitdiff
path: root/cmd/podman/diff.go
blob: 04a659ab6dc4ac7daf1bcf2c8be8e2faa558b535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main

import (
	"fmt"
	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/cmd/podman/formats"
	"github.com/containers/libpod/cmd/podman/libpodruntime"
	"github.com/containers/storage/pkg/archive"
	"github.com/pkg/errors"
	"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
}

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",
		Short: "Inspect changes on container's file systems",
		Long:  diffDescription,
		RunE: func(cmd *cobra.Command, args []string) error {
			diffCommand.InputArgs = args
			diffCommand.GlobalFlags = MainGlobalOpts
			return diffCmd(&diffCommand)
		},
		Example: "ID-NAME",
	}
)

func init() {
	diffCommand.Command = _diffCommand
	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.MarkHidden("archive")

	rootCmd.AddCommand(diffCommand.Command)

}
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
}

func diffCmd(c *cliconfig.DiffValues) error {
	if len(c.InputArgs) != 1 {
		return errors.Errorf("container, image, or layer name must be specified: podman diff [options [...]] ID-NAME")
	}

	runtime, err := libpodruntime.GetRuntime(&c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "could not get runtime")
	}
	defer runtime.Shutdown(false)

	to := c.InputArgs[0]
	changes, err := runtime.GetDiff("", 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}
	}
	formats.Writer(out).Out()

	return nil
}