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
New command to list application images #639
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As
runWithDindSwarmAndRegistry
is defined ine2e/pushpull_test.go
maybe we can move the method ine2e/helper_test.go
. This way this test only depends on the helper and not on an other test file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done