github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/cmd/podmanV2/volumes/list.go (about)

     1  package volumes
     2  
     3  import (
     4  	"context"
     5  	"html/template"
     6  	"io"
     7  	"os"
     8  	"strings"
     9  	"text/tabwriter"
    10  
    11  	"github.com/containers/libpod/cmd/podmanV2/registry"
    12  	"github.com/containers/libpod/pkg/domain/entities"
    13  	"github.com/pkg/errors"
    14  	"github.com/spf13/cobra"
    15  )
    16  
    17  var (
    18  	volumeLsDescription = `
    19  podman volume ls
    20  
    21  List all available volumes. The output of the volumes can be filtered
    22  and the output format can be changed to JSON or a user specified Go template.`
    23  	lsCommand = &cobra.Command{
    24  		Use:     "ls",
    25  		Aliases: []string{"list"},
    26  		Args:    cobra.NoArgs,
    27  		Short:   "List volumes",
    28  		Long:    volumeLsDescription,
    29  		RunE:    list,
    30  	}
    31  )
    32  
    33  var (
    34  	// Temporary struct to hold cli values.
    35  	cliOpts = struct {
    36  		Filter []string
    37  		Format string
    38  		Quiet  bool
    39  	}{}
    40  	lsOpts = entities.VolumeListOptions{}
    41  )
    42  
    43  func init() {
    44  	registry.Commands = append(registry.Commands, registry.CliCommand{
    45  		Mode:    []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
    46  		Command: lsCommand,
    47  		Parent:  volumeCmd,
    48  	})
    49  	flags := lsCommand.Flags()
    50  	flags.StringSliceVarP(&cliOpts.Filter, "filter", "f", []string{}, "Filter volume output")
    51  	flags.StringVar(&cliOpts.Format, "format", "{{.Driver}}\t{{.Name}}\n", "Format volume output using Go template")
    52  	flags.BoolVarP(&cliOpts.Quiet, "quiet", "q", false, "Print volume output in quiet mode")
    53  }
    54  
    55  func list(cmd *cobra.Command, args []string) error {
    56  	var w io.Writer = os.Stdout
    57  	if cliOpts.Quiet && cmd.Flag("format").Changed {
    58  		return errors.New("quiet and format flags cannot be used together")
    59  	}
    60  	for _, f := range cliOpts.Filter {
    61  		filterSplit := strings.Split(f, "=")
    62  		if len(filterSplit) < 2 {
    63  			return errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
    64  		}
    65  		lsOpts.Filter[filterSplit[0]] = append(lsOpts.Filter[filterSplit[0]], filterSplit[1:]...)
    66  	}
    67  	responses, err := registry.ContainerEngine().VolumeList(context.Background(), lsOpts)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	// "\t" from the command line is not being recognized as a tab
    72  	// replacing the string "\t" to a tab character if the user passes in "\t"
    73  	cliOpts.Format = strings.Replace(cliOpts.Format, `\t`, "\t", -1)
    74  	if cliOpts.Quiet {
    75  		cliOpts.Format = "{{.Name}}\n"
    76  	}
    77  	headers := "DRIVER\tVOLUME NAME\n"
    78  	row := cliOpts.Format
    79  	if !strings.HasSuffix(cliOpts.Format, "\n") {
    80  		row += "\n"
    81  	}
    82  	format := "{{range . }}" + row + "{{end}}"
    83  	if !cliOpts.Quiet && !cmd.Flag("format").Changed {
    84  		w = tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0)
    85  		format = headers + format
    86  	}
    87  	tmpl, err := template.New("listVolume").Parse(format)
    88  	if err != nil {
    89  		return err
    90  	}
    91  	if err := tmpl.Execute(w, responses); err != nil {
    92  		return err
    93  	}
    94  	if flusher, ok := w.(interface{ Flush() error }); ok {
    95  		return flusher.Flush()
    96  	}
    97  	return nil
    98  }