diff options
Diffstat (limited to 'cmd/podman/volume_ls.go')
-rw-r--r-- | cmd/podman/volume_ls.go | 115 |
1 files changed, 51 insertions, 64 deletions
diff --git a/cmd/podman/volume_ls.go b/cmd/podman/volume_ls.go index 0f94549ee..5adfc1e91 100644 --- a/cmd/podman/volume_ls.go +++ b/cmd/podman/volume_ls.go @@ -4,11 +4,11 @@ import ( "reflect" "strings" + "github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/cmd/podman/formats" - "github.com/containers/libpod/cmd/podman/libpodruntime" - "github.com/containers/libpod/libpod" + "github.com/containers/libpod/pkg/adapter" "github.com/pkg/errors" - "github.com/urfave/cli" + "github.com/spf13/cobra" ) // volumeOptions is the "ls" command options @@ -37,70 +37,64 @@ type volumeLsJSONParams struct { Scope string `json:"scope"` } -var volumeLsDescription = ` +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"}, + Short: "List volumes", + Long: volumeLsDescription, + RunE: func(cmd *cobra.Command, args []string) error { + volumeLsCommand.InputArgs = args + volumeLsCommand.GlobalFlags = MainGlobalOpts + return volumeLsCmd(&volumeLsCommand) + }, + } +) -var volumeLsFlags = []cli.Flag{ - cli.StringFlag{ - Name: "filter, f", - Usage: "Filter volume output", - }, - cli.StringFlag{ - Name: "format", - Usage: "Format volume output using Go template", - Value: "table {{.Driver}}\t{{.Name}}", - }, - cli.BoolFlag{ - Name: "quiet, q", - Usage: "Print volume output in quiet mode", - }, -} +func init() { + volumeLsCommand.Command = _volumeLsCommand + volumeLsCommand.SetUsageTemplate(UsageTemplate()) + flags := volumeLsCommand.Flags() -var volumeLsCommand = cli.Command{ - Name: "ls", - Aliases: []string{"list"}, - Usage: "List volumes", - Description: volumeLsDescription, - Flags: volumeLsFlags, - Action: volumeLsCmd, - SkipArgReorder: true, - UseShortOptionHandling: true, + 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 *cli.Context) error { - if err := validateFlags(c, volumeLsFlags); err != nil { - return err - } - - runtime, err := libpodruntime.GetRuntime(c) +func volumeLsCmd(c *cliconfig.VolumeLsValues) error { + runtime, err := adapter.GetRuntime(&c.PodmanCommand) if err != nil { return errors.Wrapf(err, "error creating libpod runtime") } defer runtime.Shutdown(false) - if len(c.Args()) > 0 { + if len(c.InputArgs) > 0 { return errors.Errorf("too many arguments, ls takes no arguments") } opts := volumeLsOptions{ - Quiet: c.Bool("quiet"), + Quiet: c.Quiet, } opts.Format = genVolLsFormat(c) // Get the filter functions based on any filters set - var filterFuncs []libpod.VolumeFilter - if c.String("filter") != "" { - filters := strings.Split(c.String("filter"), ",") + 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], runtime) + generatedFunc, err := generateVolumeFilterFuncs(filterSplit[0], filterSplit[1]) if err != nil { return errors.Wrapf(err, "invalid filter") } @@ -108,13 +102,12 @@ func volumeLsCmd(c *cli.Context) error { } } - volumes, err := runtime.GetAllVolumes() + volumes, err := runtime.Volumes(getContext()) if err != nil { return err } - // Get the volumes that match the filter - volsFiltered := make([]*libpod.Volume, 0, len(volumes)) + volsFiltered := make([]*adapter.Volume, 0, len(volumes)) for _, vol := range volumes { include := true for _, filter := range filterFuncs { @@ -125,18 +118,18 @@ func volumeLsCmd(c *cli.Context) error { volsFiltered = append(volsFiltered, vol) } } - return generateVolLsOutput(volsFiltered, opts, runtime) + return generateVolLsOutput(volsFiltered, opts) } // generate the template based on conditions given -func genVolLsFormat(c *cli.Context) string { +func genVolLsFormat(c *cliconfig.VolumeLsValues) string { var format string - if c.String("format") != "" { + 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.String("format"), `\t`, "\t", -1) + format = strings.Replace(c.Format, `\t`, "\t", -1) } - if c.Bool("quiet") { + if c.Quiet { format = "{{.Name}}" } return format @@ -211,7 +204,7 @@ func getVolTemplateOutput(lsParams []volumeLsJSONParams, opts volumeLsOptions) ( } // getVolJSONParams returns the volumes in JSON format -func getVolJSONParams(volumes []*libpod.Volume, opts volumeLsOptions, runtime *libpod.Runtime) ([]volumeLsJSONParams, error) { +func getVolJSONParams(volumes []*adapter.Volume) []volumeLsJSONParams { var lsOutput []volumeLsJSONParams for _, volume := range volumes { @@ -226,25 +219,19 @@ func getVolJSONParams(volumes []*libpod.Volume, opts volumeLsOptions, runtime *l lsOutput = append(lsOutput, params) } - return lsOutput, nil + return lsOutput } // generateVolLsOutput generates the output based on the format, JSON or Go Template, and prints it out -func generateVolLsOutput(volumes []*libpod.Volume, opts volumeLsOptions, runtime *libpod.Runtime) error { +func generateVolLsOutput(volumes []*adapter.Volume, opts volumeLsOptions) error { if len(volumes) == 0 && opts.Format != formats.JSONString { return nil } - lsOutput, err := getVolJSONParams(volumes, opts, runtime) - if err != nil { - return err - } + lsOutput := getVolJSONParams(volumes) var out formats.Writer switch opts.Format { case formats.JSONString: - if err != nil { - return errors.Wrapf(err, "unable to create JSON for volume output") - } out = formats.JSONStructArray{Output: volLsToGeneric([]volumeLsTemplateParams{}, lsOutput)} default: lsOutput, err := getVolTemplateOutput(lsOutput, opts) @@ -257,18 +244,18 @@ func generateVolLsOutput(volumes []*libpod.Volume, opts volumeLsOptions, runtime } // generateVolumeFilterFuncs returns the true if the volume matches the filter set, otherwise it returns false. -func generateVolumeFilterFuncs(filter, filterValue string, runtime *libpod.Runtime) (func(volume *libpod.Volume) bool, error) { +func generateVolumeFilterFuncs(filter, filterValue string) (func(volume *adapter.Volume) bool, error) { switch filter { case "name": - return func(v *libpod.Volume) bool { + return func(v *adapter.Volume) bool { return strings.Contains(v.Name(), filterValue) }, nil case "driver": - return func(v *libpod.Volume) bool { + return func(v *adapter.Volume) bool { return v.Driver() == filterValue }, nil case "scope": - return func(v *libpod.Volume) bool { + return func(v *adapter.Volume) bool { return v.Scope() == filterValue }, nil case "label": @@ -279,7 +266,7 @@ func generateVolumeFilterFuncs(filter, filterValue string, runtime *libpod.Runti } else { filterValue = "" } - return func(v *libpod.Volume) bool { + return func(v *adapter.Volume) bool { for labelKey, labelValue := range v.Labels() { if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) { return true @@ -295,7 +282,7 @@ func generateVolumeFilterFuncs(filter, filterValue string, runtime *libpod.Runti } else { filterValue = "" } - return func(v *libpod.Volume) bool { + return func(v *adapter.Volume) bool { for labelKey, labelValue := range v.Options() { if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) { return true |