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
10 changed files
with
434 additions
and
66 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
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,52 @@ | ||
package e2e | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
"gotest.tools/fs" | ||
"gotest.tools/icmd" | ||
) | ||
|
||
var ( | ||
reg = regexp.MustCompile("Digest is (.*).") | ||
expected = `REPOSITORY TAG APP NAME | ||
%s push-pull | ||
a-simple-app latest simple | ||
b-simple-app latest simple | ||
` | ||
) | ||
|
||
func TestImageList(t *testing.T) { | ||
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) { | ||
cmd := info.configuredCmd | ||
dir := fs.NewDir(t, "") | ||
defer dir.Remove() | ||
|
||
// Push an application so that we can later pull it by digest | ||
cmd.Command = dockerCli.Command("app", "push", "--tag", info.registryAddress+"/c-myapp", "--insecure-registries="+info.registryAddress, filepath.Join("testdata", "push-pull", "push-pull.dockerapp")) | ||
r := icmd.RunCmd(cmd).Assert(t, icmd.Success) | ||
|
||
// Get the digest from the output of the pull command | ||
out := r.Stdout() | ||
matches := reg.FindAllStringSubmatch(out, 1) | ||
digest := matches[0][1] | ||
|
||
// Pull the app by digest | ||
cmd.Command = dockerCli.Command("app", "pull", "--insecure-registries="+info.registryAddress, info.registryAddress+"/c-myapp@"+digest) | ||
icmd.RunCmd(cmd).Assert(t, icmd.Success) | ||
|
||
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) | ||
|
||
expectedOutput := fmt.Sprintf(expected, info.registryAddress+"/c-myapp") | ||
cmd.Command = dockerCli.Command("app", "image", "ls") | ||
result := icmd.RunCmd(cmd).Assert(t, icmd.Success) | ||
assert.Equal(t, result.Stdout(), expectedOutput) | ||
}) | ||
} |
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
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,128 @@ | ||
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 { | ||
appstore, err := store.NewApplicationStore(config.Dir()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bundleStore, err := appstore.BundleStore() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return runList(dockerCli, bundleStore) | ||
}, | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runList(dockerCli command.Cli, bundleStore store.BundleStore) error { | ||
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 | ||
} | ||
|
||
pk := pkg{ | ||
bundle: b, | ||
ref: ref, | ||
} | ||
|
||
if r, ok := ref.(reference.NamedTagged); ok { | ||
pk.taggedRef = r | ||
} | ||
|
||
packages[i] = pk | ||
} | ||
|
||
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 | ||
}{ | ||
{"REPOSITORY", func(p pkg) string { | ||
return reference.FamiliarName(p.ref) | ||
}}, | ||
{"TAG", func(p pkg) string { | ||
if p.taggedRef != nil { | ||
return p.taggedRef.Tag() | ||
} | ||
return "" | ||
}}, | ||
{"APP NAME", func(p pkg) string { | ||
return p.bundle.Name | ||
}}, | ||
} | ||
) | ||
|
||
type pkg struct { | ||
ref reference.Named | ||
taggedRef reference.NamedTagged | ||
bundle *bundle.Bundle | ||
} |
Oops, something went wrong.