From ce46678c931223dcc1a243c1389538d9e25b938e Mon Sep 17 00:00:00 2001 From: zhangyue Date: Sun, 11 Nov 2018 15:12:10 +0800 Subject: [PATCH] feature: add volume ls quiet flag support Signed-off-by: zhangyue --- cli/volume.go | 51 +++++++++++++++++++++++++------------ test/cli_volume_test.go | 25 +++++++++++++++++- test/environment/cleanup.go | 16 ++++++++++++ 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/cli/volume.go b/cli/volume.go index 10cce3e0a9..35cbcd06ed 100644 --- a/cli/volume.go +++ b/cli/volume.go @@ -284,6 +284,7 @@ type VolumeListCommand struct { size bool mountPoint bool + quiet bool } // Init initializes VolumeListCommand command. @@ -308,6 +309,7 @@ func (v *VolumeListCommand) addFlags() { flagSet := v.cmd.Flags() flagSet.BoolVar(&v.size, "size", false, "Display volume size") flagSet.BoolVar(&v.mountPoint, "mountpoint", false, "Display volume mountpoint") + flagSet.BoolVarP(&v.quiet, "quiet", "q", false, "Only display volume names") } // runVolumeList is the entry of VolumeListCommand command. @@ -322,27 +324,39 @@ func (v *VolumeListCommand) runVolumeList(args []string) error { return err } - display := v.cli.NewTableDisplay() - displayHead := []string{"DRIVER", "VOLUME NAME"} - if v.size { - displayHead = append(displayHead, "SIZE") + if (v.size || v.mountPoint) && v.quiet { + return fmt.Errorf("Conflicting options: --size (or --mountpoint) and -q") } - if v.mountPoint { - displayHead = append(displayHead, "MOUNT POINT") + + display := v.cli.NewTableDisplay() + displayHead := []string{"VOLUME NAME"} + + if !v.quiet { + displayHead = append([]string{"DRIVER"}, displayHead...) + if v.size { + displayHead = append(displayHead, "SIZE") + } + if v.mountPoint { + displayHead = append(displayHead, "MOUNT POINT") + } } + display.AddRow(displayHead) for _, volume := range volumeList.Volumes { - displayLine := []string{volume.Driver, volume.Name} - if v.size { - if s, ok := volume.Status["size"]; ok { - displayLine = append(displayLine, s.(string)) - } else { - displayLine = append(displayLine, "ulimit") + displayLine := []string{volume.Name} + if !v.quiet { + displayLine = append([]string{volume.Driver}, displayLine...) + if v.size { + if s, ok := volume.Status["size"]; ok { + displayLine = append(displayLine, s.(string)) + } else { + displayLine = append(displayLine, "ulimit") + } + } + if v.mountPoint { + displayLine = append(displayLine, volume.Mountpoint) } - } - if v.mountPoint { - displayLine = append(displayLine, volume.Mountpoint) } display.AddRow(displayLine) } @@ -358,5 +372,10 @@ func volumeListExample() string { DRIVER VOLUME NAME local pouch-volume-1 local pouch-volume-2 -local pouch-volume-3` +local pouch-volume-3 +$ pouch volume list --quiet +VOLUME NAME +pouch-volume-1 +pouch-volume-2 +pouch-volume-3` } diff --git a/test/cli_volume_test.go b/test/cli_volume_test.go index ace9f15bb6..1c752086b5 100644 --- a/test/cli_volume_test.go +++ b/test/cli_volume_test.go @@ -26,6 +26,8 @@ func init() { func (suite *PouchVolumeSuite) SetUpSuite(c *check.C) { SkipIfFalse(c, environment.IsLinux) + environment.PruneAllVolumes(apiClient) + environment.PruneAllContainers(apiClient) PullImage(c, busyboxImage) } @@ -293,7 +295,7 @@ func (suite *PouchVolumeSuite) TestVolumeList(c *check.C) { } } -// TestVolumeListOptions tests the volume list with options: size and mountpoint. +// TestVolumeListOptions tests the volume list with options: size, mountpoint, quiet. func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) { pc, _, _, _ := runtime.Caller(0) tmpname := strings.Split(runtime.FuncForPC(pc).Name(), ".") @@ -315,6 +317,7 @@ func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) { command.PouchRun("volume", "create", "--name", volumeName3, "-o", "opt.size=3g").Assert(c, icmd.Success) defer command.PouchRun("volume", "rm", volumeName3) + // test --size and --mountpoint options ret := command.PouchRun("volume", "list", "--size", "--mountpoint") ret.Assert(c, icmd.Success) @@ -327,4 +330,24 @@ func (suite *PouchVolumeSuite) TestVolumeListOptions(c *check.C) { } } } + + // test --quiet options + ret = command.PouchRun("volume", "list", "--quiet") + ret.Assert(c, icmd.Success) + + lines := strings.Split(ret.Stdout(), "\n") + fields := strings.Split(lines[1], " ") + c.Assert(len(fields), check.Equals, 1) + + for _, line := range lines { + if !strings.Contains(line, volumeName) { + continue + } + if !strings.EqualFold(line, volumeName1) && + !strings.EqualFold(line, volumeName2) && + !strings.EqualFold(line, volumeName3) { + c.Errorf("list volume doesn't match any existing volume name, line: %s", line) + break + } + } } diff --git a/test/environment/cleanup.go b/test/environment/cleanup.go index dce8388f34..ef00f28064 100644 --- a/test/environment/cleanup.go +++ b/test/environment/cleanup.go @@ -44,3 +44,19 @@ func PruneAllContainers(apiClient client.ContainerAPIClient) error { } return nil } + +// PruneAllVolumes deletes all volumes from pouchd +func PruneAllVolumes(apiClient client.VolumeAPIClient) error { + ctx := context.Background() + volumes, err := apiClient.VolumeList(ctx) + if err != nil { + return errors.Wrap(err, "fail to list volumes") + } + + for _, volume := range volumes.Volumes { + if err := apiClient.VolumeRemove(ctx, volume.Name); err != nil { + return errors.Wrap(err, fmt.Sprintf("fail to remove volume (%s)", volume.Name)) + } + } + return nil +}