Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add volume ls quiet flag support #2464

Merged
merged 1 commit into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions cli/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ type VolumeListCommand struct {

size bool
mountPoint bool
quiet bool
}

// Init initializes VolumeListCommand command.
Expand All @@ -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.
Expand All @@ -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...)
fuweid marked this conversation as resolved.
Show resolved Hide resolved
if v.size {
displayHead = append(displayHead, "SIZE")
}
if v.mountPoint {
displayHead = append(displayHead, "MOUNT POINT")
}
}
fuweid marked this conversation as resolved.
Show resolved Hide resolved

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)
}
Expand All @@ -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`
}
25 changes: 24 additions & 1 deletion test/cli_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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(), ".")
Expand All @@ -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)

Expand All @@ -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
}
}
}
16 changes: 16 additions & 0 deletions test/environment/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}