Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 8b6826d

Browse files
author
Jean-Christophe Sirot
committed
Add -q flag for image ls command
Signed-off-by: Jean-Christophe Sirot <[email protected]>
1 parent 64e685e commit 8b6826d

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

e2e/images_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package e2e
22

33
import (
4+
"bufio"
45
"fmt"
56
"path/filepath"
7+
"strings"
68
"testing"
79

810
"gotest.tools/assert"
@@ -26,6 +28,23 @@ func expectImageListOutput(t *testing.T, cmd icmd.Cmd, output string) {
2628
assert.Equal(t, result.Stdout(), output)
2729
}
2830

31+
func verifyImageIDListOutput(t *testing.T, cmd icmd.Cmd, count int, distinct int) {
32+
cmd.Command = dockerCli.Command("app", "image", "ls", "-q")
33+
result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
34+
scanner := bufio.NewScanner(strings.NewReader(result.Stdout()))
35+
lines := []string{}
36+
counter := make(map[string]int)
37+
for scanner.Scan() {
38+
lines = append(lines, scanner.Text())
39+
counter[scanner.Text()]++
40+
}
41+
if err := scanner.Err(); err != nil {
42+
assert.Error(t, err, "Verification failed")
43+
}
44+
assert.Equal(t, len(lines), count)
45+
assert.Equal(t, len(counter), distinct)
46+
}
47+
2948
func TestImageList(t *testing.T) {
3049
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
3150
cmd := info.configuredCmd
@@ -44,6 +63,16 @@ b-simple-app:latest simple
4463
})
4564
}
4665

66+
func TestImageListQuiet(t *testing.T) {
67+
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
68+
cmd := info.configuredCmd
69+
dir := fs.NewDir(t, "")
70+
defer dir.Remove()
71+
insertBundles(t, cmd, info)
72+
verifyImageIDListOutput(t, cmd, 3, 2)
73+
})
74+
}
75+
4776
func TestImageRm(t *testing.T) {
4877
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
4978
cmd := info.configuredCmd

internal/commands/image/list.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package image
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
67
"strings"
@@ -11,10 +12,16 @@ import (
1112
"github.com/docker/cli/cli/command"
1213
"github.com/docker/cli/cli/config"
1314
"github.com/docker/distribution/reference"
15+
"github.com/docker/docker/pkg/stringid"
1416
"github.com/spf13/cobra"
1517
)
1618

19+
type imageListOption struct {
20+
quiet bool
21+
}
22+
1723
func listCmd(dockerCli command.Cli) *cobra.Command {
24+
options := imageListOption{}
1825
cmd := &cobra.Command{
1926
Short: "List application images",
2027
Use: "ls",
@@ -30,14 +37,16 @@ func listCmd(dockerCli command.Cli) *cobra.Command {
3037
return err
3138
}
3239

33-
return runList(dockerCli, bundleStore)
40+
return runList(dockerCli, options, bundleStore)
3441
},
3542
}
43+
flags := cmd.Flags()
44+
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only show numeric IDs")
3645

3746
return cmd
3847
}
3948

40-
func runList(dockerCli command.Cli, bundleStore store.BundleStore) error {
49+
func runList(dockerCli command.Cli, options imageListOption, bundleStore store.BundleStore) error {
4150
bundles, err := bundleStore.List()
4251
if err != nil {
4352
return err
@@ -48,6 +57,9 @@ func runList(dockerCli command.Cli, bundleStore store.BundleStore) error {
4857
return err
4958
}
5059

60+
if options.quiet {
61+
return printImageIDs(dockerCli, pkgs)
62+
}
5163
return printImages(dockerCli, pkgs)
5264
}
5365

@@ -81,6 +93,24 @@ func printImages(dockerCli command.Cli, refs []pkg) error {
8193
return w.Flush()
8294
}
8395

96+
func printImageIDs(dockerCli command.Cli, refs []pkg) error {
97+
var buf bytes.Buffer
98+
99+
for _, ref := range refs {
100+
id, ok := ref.ref.(store.ID)
101+
if !ok {
102+
var err error
103+
id, err = store.FromBundle(ref.bundle)
104+
if err != nil {
105+
return err
106+
}
107+
}
108+
fmt.Fprintln(&buf, stringid.TruncateID(id.String()))
109+
}
110+
fmt.Fprintf(dockerCli.Out(), buf.String())
111+
return nil
112+
}
113+
84114
func printHeaders(w io.Writer) {
85115
var headers []string
86116
for _, column := range listColumns {

internal/store/digest.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"regexp"
88

9+
"github.com/deislabs/cnab-go/bundle"
910
"github.com/docker/distribution/reference"
1011
"github.com/opencontainers/go-digest"
1112
)
@@ -27,6 +28,11 @@ func FromString(s string) (ID, error) {
2728
return ID{s}, nil
2829
}
2930

31+
func FromBundle(bndle *bundle.Bundle) (ID, error) {
32+
digest, err := ComputeDigest(bndle)
33+
return ID{digest.Encoded()}, err
34+
}
35+
3036
// ID is an unique identifier for docker app image bundle, implementing reference.Reference
3137
type ID struct {
3238
digest string

0 commit comments

Comments
 (0)