bake: fix groups print

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
pull/881/head
CrazyMax 3 years ago
parent 202e99695b
commit 4466a24f9e
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7

@ -108,12 +108,41 @@ func ReadTargets(ctx context.Context, files []File, targets, overrides []string,
g = []*Group{{Targets: group.Targets}} g = []*Group{{Targets: group.Targets}}
} }
} else { } else {
g = []*Group{{Targets: targets}} var gt []string
for _, target := range targets {
isGroup := false
for _, group := range c.Groups {
if target == group.Name {
gt = append(gt, group.Targets...)
isGroup = true
break
}
}
if !isGroup {
gt = append(gt, target)
}
}
g = []*Group{{Targets: dedupString(gt)}}
} }
return m, g, nil return m, g, nil
} }
func dedupString(s []string) []string {
if len(s) == 0 {
return s
}
var res []string
seen := make(map[string]struct{})
for _, val := range s {
if _, ok := seen[val]; !ok {
res = append(res, val)
seen[val] = struct{}{}
}
}
return res
}
func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error) { func ParseFiles(files []File, defaults map[string]string) (_ *Config, err error) {
defer func() { defer func() {
err = formatHCLError(err, files) err = formatHCLError(err, files)

@ -203,7 +203,7 @@ func TestPushOverride(t *testing.T) {
t.Parallel() t.Parallel()
fp := File{ fp := File{
Name: "docker-bake.hc", Name: "docker-bake.hcl",
Data: []byte( Data: []byte(
`target "app" { `target "app" {
output = ["type=image,compression=zstd"] output = ["type=image,compression=zstd"]
@ -217,7 +217,7 @@ func TestPushOverride(t *testing.T) {
require.Equal(t, "type=image,compression=zstd,push=true", m["app"].Outputs[0]) require.Equal(t, "type=image,compression=zstd,push=true", m["app"].Outputs[0])
fp = File{ fp = File{
Name: "docker-bake.hc", Name: "docker-bake.hcl",
Data: []byte( Data: []byte(
`target "app" { `target "app" {
output = ["type=image,compression=zstd"] output = ["type=image,compression=zstd"]
@ -231,7 +231,7 @@ func TestPushOverride(t *testing.T) {
require.Equal(t, "type=image,compression=zstd,push=false", m["app"].Outputs[0]) require.Equal(t, "type=image,compression=zstd,push=false", m["app"].Outputs[0])
fp = File{ fp = File{
Name: "docker-bake.hc", Name: "docker-bake.hcl",
Data: []byte( Data: []byte(
`target "app" { `target "app" {
}`), }`),
@ -353,50 +353,127 @@ func TestOverrideMerge(t *testing.T) {
require.Equal(t, "type=registry", m["app"].Outputs[0]) require.Equal(t, "type=registry", m["app"].Outputs[0])
} }
func TestReadTargetsMixed(t *testing.T) { func TestReadTargetsDefault(t *testing.T) {
t.Parallel() t.Parallel()
ctx := context.TODO()
fTargetDefault := File{ f := File{
Name: "docker-bake2.hcl", Name: "docker-bake.hcl",
Data: []byte(` Data: []byte(`
target "default" { target "default" {
dockerfile = "test" dockerfile = "test"
}`)} }`)}
fTargetImage := File{ m, g, err := ReadTargets(ctx, []File{f}, []string{"default"}, nil, nil)
Name: "docker-bake3.hcl", require.NoError(t, err)
require.Equal(t, 0, len(g))
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["default"].Dockerfile)
}
func TestReadTargetsSpecified(t *testing.T) {
t.Parallel()
ctx := context.TODO()
f := File{
Name: "docker-bake.hcl",
Data: []byte(` Data: []byte(`
target "image" { target "image" {
dockerfile = "test" dockerfile = "test"
}`)} }`)}
fpHCL := File{ _, _, err := ReadTargets(ctx, []File{f}, []string{"default"}, nil, nil)
require.Error(t, err)
m, g, err := ReadTargets(ctx, []File{f}, []string{"image"}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["image"].Dockerfile)
}
func TestReadTargetsGroup(t *testing.T) {
t.Parallel()
ctx := context.TODO()
f := File{
Name: "docker-bake.hcl", Name: "docker-bake.hcl",
Data: []byte(` Data: []byte(`
group "default" { group "foo" {
targets = ["image"] targets = ["image"]
} }
target "image" {
dockerfile = "test"
}`)}
m, g, err := ReadTargets(ctx, []File{f}, []string{"foo"}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["image"].Dockerfile)
}
func TestReadTargetsGroupAndTarget(t *testing.T) {
t.Parallel()
ctx := context.TODO()
f := File{
Name: "docker-bake.hcl",
Data: []byte(`
group "foo" {
targets = ["image"]
}
target "foo" {
dockerfile = "bar"
}
target "image" {
dockerfile = "test"
}`)}
m, g, err := ReadTargets(ctx, []File{f}, []string{"foo"}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["image"].Dockerfile)
m, g, err = ReadTargets(ctx, []File{f}, []string{"foo", "foo"}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["image"].Dockerfile)
}
func TestReadTargetsMixed(t *testing.T) {
t.Parallel()
ctx := context.TODO()
fhcl := File{
Name: "docker-bake.hcl",
Data: []byte(`
group "default" {
targets = ["image"]
}
target "nocache" { target "nocache" {
no-cache = true no-cache = true
} }
group "release" { group "release" {
targets = ["image-release"] targets = ["image-release"]
} }
target "image" { target "image" {
inherits = ["nocache"] inherits = ["nocache"]
output = ["type=docker"] output = ["type=docker"]
} }
target "image-release" { target "image-release" {
inherits = ["image"] inherits = ["image"]
output = ["type=image,push=true"] output = ["type=image,push=true"]
tags = ["user/app:latest"] tags = ["user/app:latest"]
}`)} }`)}
fpYML := File{ fyml := File{
Name: "docker-compose.yml", Name: "docker-compose.yml",
Data: []byte(` Data: []byte(`
services: services:
@ -412,7 +489,6 @@ services:
- NODE_ENV=test - NODE_ENV=test
- AWS_ACCESS_KEY_ID=dummy - AWS_ACCESS_KEY_ID=dummy
- AWS_SECRET_ACCESS_KEY=dummy - AWS_SECRET_ACCESS_KEY=dummy
aws: aws:
build: build:
dockerfile: ./aws.Dockerfile dockerfile: ./aws.Dockerfile
@ -421,7 +497,7 @@ services:
CT_TAG: bar CT_TAG: bar
image: ct-fake-aws:bar`)} image: ct-fake-aws:bar`)}
fpJSON := File{ fjson := File{
Name: "docker-bake.json", Name: "docker-bake.json",
Data: []byte(`{ Data: []byte(`{
"group": { "group": {
@ -442,32 +518,15 @@ services:
} }
}`)} }`)}
ctx := context.TODO() m, g, err := ReadTargets(ctx, []File{fhcl}, []string{"default"}, nil, nil)
m, g, err := ReadTargets(ctx, []File{fTargetDefault}, []string{"default"}, nil, nil)
require.NoError(t, err)
require.Equal(t, 0, len(g))
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["default"].Dockerfile)
_, _, err = ReadTargets(ctx, []File{fTargetImage}, []string{"default"}, nil, nil)
require.Error(t, err)
m, g, err = ReadTargets(ctx, []File{fTargetImage}, []string{"image"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets) require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m)) require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["image"].Dockerfile) require.Equal(t, 1, len(m["image"].Outputs))
require.Equal(t, "type=docker", m["image"].Outputs[0])
m, g, err = ReadTargets(ctx, []File{fTargetImage}, []string{"image"}, nil, nil)
require.NoError(t, err)
require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m))
require.Equal(t, "test", *m["image"].Dockerfile)
m, g, err = ReadTargets(ctx, []File{fpHCL}, []string{"image-release"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fhcl}, []string{"image-release"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
require.Equal(t, []string{"image-release"}, g[0].Targets) require.Equal(t, []string{"image-release"}, g[0].Targets)
@ -475,7 +534,7 @@ services:
require.Equal(t, 1, len(m["image-release"].Outputs)) require.Equal(t, 1, len(m["image-release"].Outputs))
require.Equal(t, "type=image,push=true", m["image-release"].Outputs[0]) require.Equal(t, "type=image,push=true", m["image-release"].Outputs[0])
m, g, err = ReadTargets(ctx, []File{fpHCL}, []string{"image", "image-release"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fhcl}, []string{"image", "image-release"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
require.Equal(t, []string{"image", "image-release"}, g[0].Targets) require.Equal(t, []string{"image", "image-release"}, g[0].Targets)
@ -484,21 +543,21 @@ services:
require.Equal(t, 1, len(m["image-release"].Outputs)) require.Equal(t, 1, len(m["image-release"].Outputs))
require.Equal(t, "type=image,push=true", m["image-release"].Outputs[0]) require.Equal(t, "type=image,push=true", m["image-release"].Outputs[0])
m, g, err = ReadTargets(ctx, []File{fpYML, fpHCL}, []string{"default"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fyml, fhcl}, []string{"default"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets) require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m)) require.Equal(t, 1, len(m))
require.Equal(t, ".", *m["image"].Context) require.Equal(t, ".", *m["image"].Context)
m, g, err = ReadTargets(ctx, []File{fpJSON}, []string{"default"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fjson}, []string{"default"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
require.Equal(t, []string{"image"}, g[0].Targets) require.Equal(t, []string{"image"}, g[0].Targets)
require.Equal(t, 1, len(m)) require.Equal(t, 1, len(m))
require.Equal(t, ".", *m["image"].Context) require.Equal(t, ".", *m["image"].Context)
m, g, err = ReadTargets(ctx, []File{fpYML}, []string{"default"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fyml}, []string{"default"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
sort.Strings(g[0].Targets) sort.Strings(g[0].Targets)
@ -507,7 +566,7 @@ services:
require.Equal(t, "./Dockerfile", *m["addon"].Dockerfile) require.Equal(t, "./Dockerfile", *m["addon"].Dockerfile)
require.Equal(t, "./aws.Dockerfile", *m["aws"].Dockerfile) require.Equal(t, "./aws.Dockerfile", *m["aws"].Dockerfile)
m, g, err = ReadTargets(ctx, []File{fpYML, fpHCL}, []string{"addon", "aws"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fyml, fhcl}, []string{"addon", "aws"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
sort.Strings(g[0].Targets) sort.Strings(g[0].Targets)
@ -516,7 +575,7 @@ services:
require.Equal(t, "./Dockerfile", *m["addon"].Dockerfile) require.Equal(t, "./Dockerfile", *m["addon"].Dockerfile)
require.Equal(t, "./aws.Dockerfile", *m["aws"].Dockerfile) require.Equal(t, "./aws.Dockerfile", *m["aws"].Dockerfile)
m, g, err = ReadTargets(ctx, []File{fpYML, fpHCL}, []string{"addon", "aws", "image"}, nil, nil) m, g, err = ReadTargets(ctx, []File{fyml, fhcl}, []string{"addon", "aws", "image"}, nil, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(g)) require.Equal(t, 1, len(g))
sort.Strings(g[0].Targets) sort.Strings(g[0].Targets)

Loading…
Cancel
Save