summaryrefslogtreecommitdiff
path: root/cmd/podman/volume_ls.go
blob: 938124278efc2d2d613c3756af4e5910416b67b5 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main

import (
	"reflect"
	"strings"

	"github.com/containers/buildah/pkg/formats"
	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/pkg/adapter"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

// volumeOptions is the "ls" command options
type volumeLsOptions struct {
	Format string
	Quiet  bool
}

// volumeLsTemplateParams is the template parameters to list the volumes
type volumeLsTemplateParams struct {
	Name       string
	Labels     string
	MountPoint string
	Driver     string
	Options    string
	Scope      string
}

// volumeLsJSONParams is the JSON parameters to list the volumes
type volumeLsJSONParams struct {
	Name       string            `json:"name"`
	Labels     map[string]string `json:"labels"`
	MountPoint string            `json:"mountPoint"`
	Driver     string            `json:"driver"`
	Options    map[string]string `json:"options"`
	Scope      string            `json:"scope"`
}

var (
	volumeLsCommand cliconfig.VolumeLsValues

	volumeLsDescription = `
podman volume ls

List all available volumes. The output of the volumes can be filtered
and the output format can be changed to JSON or a user specified Go template.`
	_volumeLsCommand = &cobra.Command{
		Use:     "ls",
		Aliases: []string{"list"},
		Args:    noSubArgs,
		Short:   "List volumes",
		Long:    volumeLsDescription,
		RunE: func(cmd *cobra.Command, args []string) error {
			volumeLsCommand.InputArgs = args
			volumeLsCommand.GlobalFlags = MainGlobalOpts
			volumeLsCommand.Remote = remoteclient
			return volumeLsCmd(&volumeLsCommand)
		},
	}
)

func init() {
	volumeLsCommand.Command = _volumeLsCommand
	volumeLsCommand.SetHelpTemplate(HelpTemplate())
	volumeLsCommand.SetUsageTemplate(UsageTemplate())
	flags := volumeLsCommand.Flags()

	flags.StringVarP(&volumeLsCommand.Filter, "filter", "f", "", "Filter volume output")
	flags.StringVar(&volumeLsCommand.Format, "format", "table {{.Driver}}\t{{.Name}}", "Format volume output using Go template")
	flags.BoolVarP(&volumeLsCommand.Quiet, "quiet", "q", false, "Print volume output in quiet mode")
}

func volumeLsCmd(c *cliconfig.VolumeLsValues) error {
	runtime, err := adapter.GetRuntime(getContext(), &c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "error creating libpod runtime")
	}
	defer runtime.DeferredShutdown(false)

	opts := volumeLsOptions{
		Quiet: c.Quiet,
	}
	opts.Format = genVolLsFormat(c)

	// Get the filter functions based on any filters set
	var filterFuncs []adapter.VolumeFilter
	if c.Filter != "" {
		filters := strings.Split(c.Filter, ",")
		for _, f := range filters {
			filterSplit := strings.Split(f, "=")
			if len(filterSplit) < 2 {
				return errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
			}
			generatedFunc, err := generateVolumeFilterFuncs(filterSplit[0], filterSplit[1])
			if err != nil {
				return errors.Wrapf(err, "invalid filter")
			}
			filterFuncs = append(filterFuncs, generatedFunc)
		}
	}

	volumes, err := runtime.Volumes(getContext())
	if err != nil {
		return err
	}
	// Get the volumes that match the filter
	volsFiltered := make([]*adapter.Volume, 0, len(volumes))
	for _, vol := range volumes {
		include := true
		for _, filter := range filterFuncs {
			include = include && filter(vol)
		}

		if include {
			volsFiltered = append(volsFiltered, vol)
		}
	}
	return generateVolLsOutput(volsFiltered, opts)
}

// generate the template based on conditions given
func genVolLsFormat(c *cliconfig.VolumeLsValues) string {
	var format string
	if c.Format != "" {
		// "\t" from the command line is not being recognized as a tab
		// replacing the string "\t" to a tab character if the user passes in "\t"
		format = strings.Replace(c.Format, `\t`, "\t", -1)
	}
	if c.Quiet {
		format = "{{.Name}}"
	}
	return format
}

// Convert output to genericParams for printing
func volLsToGeneric(templParams []volumeLsTemplateParams, jsonParams []volumeLsJSONParams) (genericParams []interface{}) {
	if len(templParams) > 0 {
		for _, v := range templParams {
			genericParams = append(genericParams, interface{}(v))
		}
		return
	}
	for _, v := range jsonParams {
		genericParams = append(genericParams, interface{}(v))
	}
	return
}

// generate the accurate header based on template given
func (vol *volumeLsTemplateParams) volHeaderMap() map[string]string {
	v := reflect.Indirect(reflect.ValueOf(vol))
	values := make(map[string]string)

	for i := 0; i < v.NumField(); i++ {
		key := v.Type().Field(i).Name
		value := key
		if value == "Name" {
			value = "Volume" + value
		}
		values[key] = strings.ToUpper(splitCamelCase(value))
	}
	return values
}

// getVolTemplateOutput returns all the volumes in the volumeLsTemplateParams format
func getVolTemplateOutput(lsParams []volumeLsJSONParams, opts volumeLsOptions) ([]volumeLsTemplateParams, error) {
	var lsOutput []volumeLsTemplateParams

	for _, lsParam := range lsParams {
		var (
			labels  string
			options string
		)

		for k, v := range lsParam.Labels {
			label := k
			if v != "" {
				label += "=" + v
			}
			labels += label
		}
		for k, v := range lsParam.Options {
			option := k
			if v != "" {
				option += "=" + v
			}
			options += option
		}
		params := volumeLsTemplateParams{
			Name:       lsParam.Name,
			Driver:     lsParam.Driver,
			MountPoint: lsParam.MountPoint,
			Scope:      lsParam.Scope,
			Labels:     labels,
			Options:    options,
		}

		lsOutput = append(lsOutput, params)
	}
	return lsOutput, nil
}

// getVolJSONParams returns the volumes in JSON format
func getVolJSONParams(volumes []*adapter.Volume) []volumeLsJSONParams {
	var lsOutput []volumeLsJSONParams

	for _, volume := range volumes {
		params := volumeLsJSONParams{
			Name:       volume.Name(),
			Labels:     volume.Labels(),
			MountPoint: volume.MountPoint(),
			Driver:     volume.Driver(),
			Options:    volume.Options(),
			Scope:      volume.Scope(),
		}

		lsOutput = append(lsOutput, params)
	}
	return lsOutput
}

// generateVolLsOutput generates the output based on the format, JSON or Go Template, and prints it out
func generateVolLsOutput(volumes []*adapter.Volume, opts volumeLsOptions) error {
	if len(volumes) == 0 && opts.Format != formats.JSONString {
		return nil
	}
	lsOutput := getVolJSONParams(volumes)
	var out formats.Writer

	switch opts.Format {
	case formats.JSONString:
		out = formats.JSONStructArray{Output: volLsToGeneric([]volumeLsTemplateParams{}, lsOutput)}
	default:
		lsOutput, err := getVolTemplateOutput(lsOutput, opts)
		if err != nil {
			return errors.Wrapf(err, "unable to create volume output")
		}
		out = formats.StdoutTemplateArray{Output: volLsToGeneric(lsOutput, []volumeLsJSONParams{}), Template: opts.Format, Fields: lsOutput[0].volHeaderMap()}
	}
	return out.Out()
}

// generateVolumeFilterFuncs returns the true if the volume matches the filter set, otherwise it returns false.
func generateVolumeFilterFuncs(filter, filterValue string) (func(volume *adapter.Volume) bool, error) {
	switch filter {
	case "name":
		return func(v *adapter.Volume) bool {
			return strings.Contains(v.Name(), filterValue)
		}, nil
	case "driver":
		return func(v *adapter.Volume) bool {
			return v.Driver() == filterValue
		}, nil
	case "scope":
		return func(v *adapter.Volume) bool {
			return v.Scope() == filterValue
		}, nil
	case "label":
		filterArray := strings.SplitN(filterValue, "=", 2)
		filterKey := filterArray[0]
		if len(filterArray) > 1 {
			filterValue = filterArray[1]
		} else {
			filterValue = ""
		}
		return func(v *adapter.Volume) bool {
			for labelKey, labelValue := range v.Labels() {
				if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) {
					return true
				}
			}
			return false
		}, nil
	case "opt":
		filterArray := strings.SplitN(filterValue, "=", 2)
		filterKey := filterArray[0]
		if len(filterArray) > 1 {
			filterValue = filterArray[1]
		} else {
			filterValue = ""
		}
		return func(v *adapter.Volume) bool {
			for labelKey, labelValue := range v.Options() {
				if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) {
					return true
				}
			}
			return false
		}, nil
	}
	return nil, errors.Errorf("%s is an invalid filter", filter)
}