mirror of https://github.com/docker/cli.git
Reduce complexity of two formatters
Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
parent
7e52344cd2
commit
d318c4112b
|
@ -1,9 +1,10 @@
|
||||||
package formatter
|
package formatter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func compareMultipleValues(t *testing.T, value, expected string) {
|
func compareMultipleValues(t *testing.T, value, expected string) {
|
||||||
|
@ -22,7 +23,5 @@ func compareMultipleValues(t *testing.T, value, expected string) {
|
||||||
keyval := strings.Split(expected, "=")
|
keyval := strings.Split(expected, "=")
|
||||||
expMap[keyval[0]] = keyval[1]
|
expMap[keyval[0]] = keyval[1]
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(expMap, entriesMap) {
|
assert.Equal(t, expMap, entriesMap)
|
||||||
t.Fatalf("Expected entries: %v, got: %v", expected, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,9 +86,9 @@ func needDigest(ctx ImageContext) bool {
|
||||||
|
|
||||||
func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subContext subContext) error) error {
|
func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subContext subContext) error) error {
|
||||||
for _, image := range images {
|
for _, image := range images {
|
||||||
images := []*imageContext{}
|
formatted := []*imageContext{}
|
||||||
if isDangling(image) {
|
if isDangling(image) {
|
||||||
images = append(images, &imageContext{
|
formatted = append(formatted, &imageContext{
|
||||||
trunc: ctx.Trunc,
|
trunc: ctx.Trunc,
|
||||||
i: image,
|
i: image,
|
||||||
repo: "<none>",
|
repo: "<none>",
|
||||||
|
@ -96,8 +96,21 @@ func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subC
|
||||||
digest: "<none>",
|
digest: "<none>",
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
formatted = imageFormatTaggedAndDigest(ctx, image)
|
||||||
|
}
|
||||||
|
for _, imageCtx := range formatted {
|
||||||
|
if err := format(imageCtx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func imageFormatTaggedAndDigest(ctx ImageContext, image types.ImageSummary) []*imageContext {
|
||||||
repoTags := map[string][]string{}
|
repoTags := map[string][]string{}
|
||||||
repoDigests := map[string][]string{}
|
repoDigests := map[string][]string{}
|
||||||
|
images := []*imageContext{}
|
||||||
|
|
||||||
for _, refString := range image.RepoTags {
|
for _, refString := range image.RepoTags {
|
||||||
ref, err := reference.ParseNormalizedNamed(refString)
|
ref, err := reference.ParseNormalizedNamed(refString)
|
||||||
|
@ -120,6 +133,17 @@ func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
addImage := func(repo, tag, digest string) {
|
||||||
|
image := &imageContext{
|
||||||
|
trunc: ctx.Trunc,
|
||||||
|
i: image,
|
||||||
|
repo: repo,
|
||||||
|
tag: tag,
|
||||||
|
digest: digest,
|
||||||
|
}
|
||||||
|
images = append(images, image)
|
||||||
|
}
|
||||||
|
|
||||||
for repo, tags := range repoTags {
|
for repo, tags := range repoTags {
|
||||||
digests := repoDigests[repo]
|
digests := repoDigests[repo]
|
||||||
|
|
||||||
|
@ -133,24 +157,12 @@ func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subC
|
||||||
|
|
||||||
for _, tag := range tags {
|
for _, tag := range tags {
|
||||||
if len(digests) == 0 {
|
if len(digests) == 0 {
|
||||||
images = append(images, &imageContext{
|
addImage(repo, tag, "<none>")
|
||||||
trunc: ctx.Trunc,
|
|
||||||
i: image,
|
|
||||||
repo: repo,
|
|
||||||
tag: tag,
|
|
||||||
digest: "<none>",
|
|
||||||
})
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Display the digests for each tag
|
// Display the digests for each tag
|
||||||
for _, dgst := range digests {
|
for _, dgst := range digests {
|
||||||
images = append(images, &imageContext{
|
addImage(repo, tag, dgst)
|
||||||
trunc: ctx.Trunc,
|
|
||||||
i: image,
|
|
||||||
repo: repo,
|
|
||||||
tag: tag,
|
|
||||||
digest: dgst,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -161,31 +173,14 @@ func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subC
|
||||||
// If digests are displayed, show row per digest
|
// If digests are displayed, show row per digest
|
||||||
if ctx.Digest {
|
if ctx.Digest {
|
||||||
for _, dgst := range digests {
|
for _, dgst := range digests {
|
||||||
images = append(images, &imageContext{
|
addImage(repo, "<none>", dgst)
|
||||||
trunc: ctx.Trunc,
|
|
||||||
i: image,
|
|
||||||
repo: repo,
|
|
||||||
tag: "<none>",
|
|
||||||
digest: dgst,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
images = append(images, &imageContext{
|
addImage(repo, "<none>", "")
|
||||||
trunc: ctx.Trunc,
|
|
||||||
i: image,
|
|
||||||
repo: repo,
|
|
||||||
tag: "<none>",
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return images
|
||||||
for _, imageCtx := range images {
|
|
||||||
if err := format(imageCtx); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type imageContext struct {
|
type imageContext struct {
|
||||||
|
|
|
@ -55,6 +55,26 @@ func TestImageContext(t *testing.T) {
|
||||||
i: types.ImageSummary{},
|
i: types.ImageSummary{},
|
||||||
digest: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a",
|
digest: "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a",
|
||||||
}, "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a", ctx.Digest},
|
}, "sha256:d149ab53f8718e987c3a3024bb8aa0e2caadf6c0328f1d9d850b2a2a67f2819a", ctx.Digest},
|
||||||
|
{
|
||||||
|
imageContext{
|
||||||
|
i: types.ImageSummary{Containers: 10},
|
||||||
|
}, "10", ctx.Containers,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imageContext{
|
||||||
|
i: types.ImageSummary{VirtualSize: 10000},
|
||||||
|
}, "10kB", ctx.VirtualSize,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imageContext{
|
||||||
|
i: types.ImageSummary{SharedSize: 10000},
|
||||||
|
}, "10kB", ctx.SharedSize,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
imageContext{
|
||||||
|
i: types.ImageSummary{SharedSize: 5000, VirtualSize: 20000},
|
||||||
|
}, "15kB", ctx.UniqueSize,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
|
@ -62,8 +82,8 @@ func TestImageContext(t *testing.T) {
|
||||||
v := c.call()
|
v := c.call()
|
||||||
if strings.Contains(v, ",") {
|
if strings.Contains(v, ",") {
|
||||||
compareMultipleValues(t, v, c.expValue)
|
compareMultipleValues(t, v, c.expValue)
|
||||||
} else if v != c.expValue {
|
} else {
|
||||||
t.Fatalf("Expected %s, was %s\n", c.expValue, v)
|
assert.Equal(t, c.expValue, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,11 +93,7 @@ func Load(configDetails types.ConfigDetails) (*types.Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Configs, err = LoadConfigObjs(config["configs"], configDetails.WorkingDir)
|
cfg.Configs, err = LoadConfigObjs(config["configs"], configDetails.WorkingDir)
|
||||||
if err != nil {
|
return &cfg, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &cfg, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func interpolateConfig(configDict map[string]interface{}, lookupEnv template.Mapping) (map[string]map[string]interface{}, error) {
|
func interpolateConfig(configDict map[string]interface{}, lookupEnv template.Mapping) (map[string]map[string]interface{}, error) {
|
||||||
|
|
Loading…
Reference in New Issue