summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2020-03-21 14:29:30 -0500
committerBrent Baude <bbaude@redhat.com>2020-03-24 16:03:49 -0500
commitae614920bfe2510ca6d1dd6cea02bbe17ddb245c (patch)
tree5edd6e5fd01f12154c82146ce6170aaf7d716e19 /cmd
parent0c084d9719772a9b68d5eb67114cf5bf001958b2 (diff)
downloadpodman-ae614920bfe2510ca6d1dd6cea02bbe17ddb245c.tar.gz
podman-ae614920bfe2510ca6d1dd6cea02bbe17ddb245c.tar.bz2
podman-ae614920bfe2510ca6d1dd6cea02bbe17ddb245c.zip
podmanv2 volumes
add volume commands: create, inspect, ls, prune, and rm Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podmanV2/volumes/inspect.go74
-rw-r--r--cmd/podmanV2/volumes/list.go98
-rw-r--r--cmd/podmanV2/volumes/prune.go74
-rw-r--r--cmd/podmanV2/volumes/rm.go64
4 files changed, 310 insertions, 0 deletions
diff --git a/cmd/podmanV2/volumes/inspect.go b/cmd/podmanV2/volumes/inspect.go
new file mode 100644
index 000000000..4d9720432
--- /dev/null
+++ b/cmd/podmanV2/volumes/inspect.go
@@ -0,0 +1,74 @@
+package volumes
+
+import (
+ "encoding/json"
+ "fmt"
+ "html/template"
+ "os"
+
+ "github.com/containers/buildah/pkg/formats"
+ "github.com/containers/libpod/cmd/podmanV2/registry"
+ "github.com/containers/libpod/pkg/domain/entities"
+ "github.com/pkg/errors"
+ "github.com/spf13/cobra"
+ "golang.org/x/net/context"
+)
+
+var (
+ volumeInspectDescription = `Display detailed information on one or more volumes.
+
+ Use a Go template to change the format from JSON.`
+ inspectCommand = &cobra.Command{
+ Use: "inspect [flags] VOLUME [VOLUME...]",
+ Short: "Display detailed information on one or more volumes",
+ Long: volumeInspectDescription,
+ RunE: inspect,
+ Example: `podman volume inspect myvol
+ podman volume inspect --all
+ podman volume inspect --format "{{.Driver}} {{.Scope}}" myvol`,
+ }
+)
+
+var (
+ inspectOpts = entities.VolumeInspectOptions{}
+ inspectFormat string
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+ Command: inspectCommand,
+ Parent: volumeCmd,
+ })
+ flags := inspectCommand.Flags()
+ flags.BoolVarP(&inspectOpts.All, "all", "a", false, "Inspect all volumes")
+ flags.StringVarP(&inspectFormat, "format", "f", "json", "Format volume output using Go template")
+}
+
+func inspect(cmd *cobra.Command, args []string) error {
+ if (inspectOpts.All && len(args) > 0) || (!inspectOpts.All && len(args) < 1) {
+ return errors.New("provide one or more volume names or use --all")
+ }
+ responses, err := registry.ContainerEngine().VolumeInspect(context.Background(), args, inspectOpts)
+ if err != nil {
+ return err
+ }
+ switch inspectFormat {
+ case "", formats.JSONString:
+ jsonOut, err := json.MarshalIndent(responses, "", " ")
+ if err != nil {
+ return errors.Wrapf(err, "error marshalling inspect JSON")
+ }
+ fmt.Println(string(jsonOut))
+ default:
+ tmpl, err := template.New("volumeInspect").Parse(inspectFormat)
+ if err != nil {
+ return err
+ }
+ if err := tmpl.Execute(os.Stdout, responses); err != nil {
+ return err
+ }
+ }
+ return nil
+
+}
diff --git a/cmd/podmanV2/volumes/list.go b/cmd/podmanV2/volumes/list.go
new file mode 100644
index 000000000..c38f78c73
--- /dev/null
+++ b/cmd/podmanV2/volumes/list.go
@@ -0,0 +1,98 @@
+package volumes
+
+import (
+ "context"
+ "html/template"
+ "io"
+ "os"
+ "strings"
+ "text/tabwriter"
+
+ "github.com/containers/libpod/cmd/podmanV2/registry"
+ "github.com/containers/libpod/pkg/domain/entities"
+ "github.com/pkg/errors"
+ "github.com/spf13/cobra"
+)
+
+var (
+ 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.`
+ lsCommand = &cobra.Command{
+ Use: "ls",
+ Aliases: []string{"list"},
+ Args: cobra.NoArgs,
+ Short: "List volumes",
+ Long: volumeLsDescription,
+ RunE: list,
+ }
+)
+
+var (
+ // Temporary struct to hold cli values.
+ cliOpts = struct {
+ Filter []string
+ Format string
+ Quiet bool
+ }{}
+ lsOpts = entities.VolumeListOptions{}
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+ Command: lsCommand,
+ Parent: volumeCmd,
+ })
+ flags := lsCommand.Flags()
+ flags.StringSliceVarP(&cliOpts.Filter, "filter", "f", []string{}, "Filter volume output")
+ flags.StringVar(&cliOpts.Format, "format", "{{.Driver}}\t{{.Name}}\n", "Format volume output using Go template")
+ flags.BoolVarP(&cliOpts.Quiet, "quiet", "q", false, "Print volume output in quiet mode")
+}
+
+func list(cmd *cobra.Command, args []string) error {
+ var w io.Writer = os.Stdout
+ if cliOpts.Quiet && cmd.Flag("format").Changed {
+ return errors.New("quiet and format flags cannot be used together")
+ }
+ for _, f := range cliOpts.Filter {
+ 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)
+ }
+ lsOpts.Filter[filterSplit[0]] = append(lsOpts.Filter[filterSplit[0]], filterSplit[1:]...)
+ }
+ responses, err := registry.ContainerEngine().VolumeList(context.Background(), lsOpts)
+ if err != nil {
+ return err
+ }
+ // "\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"
+ cliOpts.Format = strings.Replace(cliOpts.Format, `\t`, "\t", -1)
+ if cliOpts.Quiet {
+ cliOpts.Format = "{{.Name}}\n"
+ }
+ headers := "DRIVER\tVOLUME NAME\n"
+ row := cliOpts.Format
+ if !strings.HasSuffix(cliOpts.Format, "\n") {
+ row += "\n"
+ }
+ format := "{{range . }}" + row + "{{end}}"
+ if !cliOpts.Quiet && !cmd.Flag("format").Changed {
+ w = tabwriter.NewWriter(os.Stdout, 12, 2, 2, ' ', 0)
+ format = headers + format
+ }
+ tmpl, err := template.New("listVolume").Parse(format)
+ if err != nil {
+ return err
+ }
+ if err := tmpl.Execute(w, responses); err != nil {
+ return err
+ }
+ if flusher, ok := w.(interface{ Flush() error }); ok {
+ return flusher.Flush()
+ }
+ return nil
+}
diff --git a/cmd/podmanV2/volumes/prune.go b/cmd/podmanV2/volumes/prune.go
new file mode 100644
index 000000000..148065f56
--- /dev/null
+++ b/cmd/podmanV2/volumes/prune.go
@@ -0,0 +1,74 @@
+package volumes
+
+import (
+ "bufio"
+ "context"
+ "fmt"
+ "os"
+ "strings"
+
+ "github.com/containers/libpod/cmd/podmanV2/registry"
+ "github.com/containers/libpod/cmd/podmanV2/utils"
+ "github.com/containers/libpod/pkg/domain/entities"
+ "github.com/pkg/errors"
+ "github.com/spf13/cobra"
+)
+
+var (
+ volumePruneDescription = `Volumes that are not currently owned by a container will be removed.
+
+ The command prompts for confirmation which can be overridden with the --force flag.
+ Note all data will be destroyed.`
+ pruneCommand = &cobra.Command{
+ Use: "prune",
+ Args: cobra.NoArgs,
+ Short: "Remove all unused volumes",
+ Long: volumePruneDescription,
+ RunE: prune,
+ }
+)
+
+var (
+ pruneOptions entities.VolumePruneOptions
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+ Command: pruneCommand,
+ Parent: volumeCmd,
+ })
+ flags := pruneCommand.Flags()
+ flags.BoolVarP(&pruneOptions.Force, "force", "f", false, "Do not prompt for confirmation")
+}
+
+func prune(cmd *cobra.Command, args []string) error {
+ var (
+ errs utils.OutputErrors
+ )
+ // Prompt for confirmation if --force is not set
+ if !pruneOptions.Force {
+ reader := bufio.NewReader(os.Stdin)
+ fmt.Println("WARNING! This will remove all volumes not used by at least one container.")
+ fmt.Print("Are you sure you want to continue? [y/N] ")
+ answer, err := reader.ReadString('\n')
+ if err != nil {
+ return errors.Wrapf(err, "error reading input")
+ }
+ if strings.ToLower(answer)[0] != 'y' {
+ return nil
+ }
+ }
+ responses, err := registry.ContainerEngine().VolumePrune(context.Background(), pruneOptions)
+ if err != nil {
+ return err
+ }
+ for _, r := range responses {
+ if r.Err == nil {
+ fmt.Println(r.Id)
+ } else {
+ errs = append(errs, r.Err)
+ }
+ }
+ return errs.PrintErrors()
+}
diff --git a/cmd/podmanV2/volumes/rm.go b/cmd/podmanV2/volumes/rm.go
new file mode 100644
index 000000000..b019285d8
--- /dev/null
+++ b/cmd/podmanV2/volumes/rm.go
@@ -0,0 +1,64 @@
+package volumes
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/containers/libpod/cmd/podmanV2/registry"
+ "github.com/containers/libpod/cmd/podmanV2/utils"
+ "github.com/containers/libpod/pkg/domain/entities"
+ "github.com/pkg/errors"
+ "github.com/spf13/cobra"
+)
+
+var (
+ volumeRmDescription = `Remove one or more existing volumes.
+
+ By default only volumes that are not being used by any containers will be removed. To remove the volumes anyways, use the --force flag.`
+ rmCommand = &cobra.Command{
+ Use: "rm [flags] VOLUME [VOLUME...]",
+ Aliases: []string{"remove"},
+ Short: "Remove one or more volumes",
+ Long: volumeRmDescription,
+ RunE: rm,
+ Example: `podman volume rm myvol1 myvol2
+ podman volume rm --all
+ podman volume rm --force myvol`,
+ }
+)
+
+var (
+ rmOptions = entities.VolumeRmOptions{}
+)
+
+func init() {
+ registry.Commands = append(registry.Commands, registry.CliCommand{
+ Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
+ Command: rmCommand,
+ Parent: volumeCmd,
+ })
+ flags := rmCommand.Flags()
+ flags.BoolVarP(&rmOptions.All, "all", "a", false, "Remove all volumes")
+ flags.BoolVarP(&rmOptions.Force, "force", "f", false, "Remove a volume by force, even if it is being used by a container")
+}
+
+func rm(cmd *cobra.Command, args []string) error {
+ var (
+ errs utils.OutputErrors
+ )
+ if (len(args) > 0 && rmOptions.All) || (len(args) < 1 && !rmOptions.All) {
+ return errors.New("choose either one or more volumes or all")
+ }
+ responses, err := registry.ContainerEngine().VolumeRm(context.Background(), args, rmOptions)
+ if err != nil {
+ return err
+ }
+ for _, r := range responses {
+ if r.Err == nil {
+ fmt.Println(r.Id)
+ } else {
+ errs = append(errs, r.Err)
+ }
+ }
+ return errs.PrintErrors()
+}