This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New command to list application images
For now we show the reference, name and version. Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
- Loading branch information
Showing
9 changed files
with
336 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package e2e | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"gotest.tools/fs" | ||
"gotest.tools/golden" | ||
"gotest.tools/icmd" | ||
) | ||
|
||
func TestImageList(t *testing.T) { | ||
cmd, cleanup := dockerCli.createTestCmd() | ||
defer cleanup() | ||
dir := fs.NewDir(t, "") | ||
defer dir.Remove() | ||
|
||
cmd.Command = dockerCli.Command("app", "bundle", filepath.Join("testdata", "simple", "simple.dockerapp"), "--tag", "b-simple-app", "--output", dir.Join("simple-bundle.json")) | ||
icmd.RunCmd(cmd).Assert(t, icmd.Success) | ||
cmd.Command = dockerCli.Command("app", "bundle", filepath.Join("testdata", "simple", "simple.dockerapp"), "--tag", "a-simple-app", "--output", dir.Join("simple-bundle.json")) | ||
icmd.RunCmd(cmd).Assert(t, icmd.Success) | ||
|
||
cmd.Command = dockerCli.Command("app", "image", "ls") | ||
result := icmd.RunCmd(cmd).Assert(t, icmd.Success) | ||
golden.Assert(t, result.Stdout(), "expected-image-ls.golden") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
REFERENCE NAME VERSION | ||
a-simple-app:latest simple 1.1.0-beta1 | ||
b-simple-app:latest simple 1.1.0-beta1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package image | ||
|
||
import ( | ||
"github.com/docker/cli/cli/command" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// Cmd is the image top level command | ||
func Cmd(dockerCli command.Cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Short: "Manage application images", | ||
Use: "image", | ||
} | ||
|
||
cmd.AddCommand(listCmd(dockerCli)) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package image | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
"text/tabwriter" | ||
|
||
"github.com/deislabs/cnab-go/bundle" | ||
"github.com/docker/app/internal/store" | ||
"github.com/docker/cli/cli/command" | ||
"github.com/docker/cli/cli/config" | ||
"github.com/docker/distribution/reference" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func listCmd(dockerCli command.Cli) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Short: "List application images", | ||
Use: "ls", | ||
Aliases: []string{"list"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return runList(dockerCli) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runList(dockerCli command.Cli) error { | ||
appstore, err := store.NewApplicationStore(config.Dir()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bundleStore, err := appstore.BundleStore() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bundles, err := bundleStore.List() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
pkgs, err := getPackages(bundleStore, bundles) | ||
if err != nil { | ||
return err | ||
} | ||
return printImages(dockerCli, pkgs) | ||
} | ||
|
||
func getPackages(bundleStore store.BundleStore, references []reference.Named) ([]pkg, error) { | ||
packages := make([]pkg, len(references)) | ||
for i, ref := range references { | ||
b, err := bundleStore.Read(ref) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
packages[i] = pkg{ | ||
ref: ref, | ||
bundle: b, | ||
} | ||
} | ||
|
||
return packages, nil | ||
} | ||
|
||
func printImages(dockerCli command.Cli, refs []pkg) error { | ||
w := tabwriter.NewWriter(dockerCli.Out(), 0, 0, 1, ' ', 0) | ||
|
||
printHeaders(w) | ||
for _, ref := range refs { | ||
printValues(w, ref) | ||
} | ||
|
||
return w.Flush() | ||
} | ||
|
||
func printHeaders(w io.Writer) { | ||
var headers []string | ||
for _, column := range listColumns { | ||
headers = append(headers, column.header) | ||
} | ||
fmt.Fprintln(w, strings.Join(headers, "\t")) | ||
} | ||
|
||
func printValues(w io.Writer, ref pkg) { | ||
var values []string | ||
for _, column := range listColumns { | ||
values = append(values, column.value(ref)) | ||
} | ||
fmt.Fprintln(w, strings.Join(values, "\t")) | ||
} | ||
|
||
var ( | ||
listColumns = []struct { | ||
header string | ||
value func(p pkg) string | ||
}{ | ||
{"REFERENCE", func(p pkg) string { | ||
return reference.FamiliarString(p.ref) | ||
}}, | ||
{"NAME", func(p pkg) string { return p.bundle.Name }}, | ||
{"VERSION", func(p pkg) string { return p.bundle.Version }}, | ||
} | ||
) | ||
|
||
type pkg struct { | ||
ref reference.Named | ||
bundle *bundle.Bundle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters