Skip to content

Commit

Permalink
bake: fix print output
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Nov 23, 2021
1 parent bcfd434 commit 9ebe9fd
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 22 deletions.
27 changes: 27 additions & 0 deletions bake/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,33 @@ func TargetsToBuildOpt(m map[string]*Target, inp *Input) (map[string]build.Optio
return m2, nil
}

type DefaultConfig struct {
Group map[string]*Group `json:"group,omitempty"`
Target map[string]*Target `json:"target"`
}

func GetDefaultConfig(inp []string, noTarget bool, targets map[string]*Target, groups []*Group) (cfg DefaultConfig) {
cfg.Target = targets
cfg.Group = map[string]*Group{
"default": {
Targets: inp,
},
}
if noTarget {
for _, group := range groups {
if group.Name != "default" {
continue
}
cfg.Group = map[string]*Group{
"default": {
Targets: group.Targets,
},
}
}
}
return
}

func updateContext(t *build.Inputs, inp *Input) {
if inp == nil || inp.State == nil {
return
Expand Down
132 changes: 132 additions & 0 deletions bake/bake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,135 @@ func TestOverrideMerge(t *testing.T) {
require.Equal(t, 1, len(m["app"].Outputs))
require.Equal(t, "type=registry", m["app"].Outputs[0])
}

func TestDefaultConfig(t *testing.T) {
t.Parallel()

fpHCL := File{
Name: "docker-bake.hcl",
Data: []byte(`
group "default" {
targets = ["image"]
}
target "nocache" {
no-cache = true
}
target "image" {
inherits = ["nocache"]
output = ["type=docker"]
}`)}

fpYML := File{
Name: "docker-compose.yml",
Data: []byte(`
services:
addon:
build:
context: .
dockerfile: ./Dockerfile
args:
CT_ECR: foo
CT_TAG: bar
image: ct-addon:bar
environment:
- NODE_ENV=test
- AWS_ACCESS_KEY_ID=dummy
- AWS_SECRET_ACCESS_KEY=dummy
aws:
build:
dockerfile: ./aws.Dockerfile
args:
CT_ECR: foo
CT_TAG: bar
image: ct-fake-aws:bar`)}

fpJSON := File{
Name: "docker-bake.json",
Data: []byte(`{
"group": {
"default": {
"targets": [
"image"
]
}
},
"target": {
"image": {
"context": ".",
"dockerfile": "Dockerfile",
"output": [
"type=docker"
]
}
}
}`)}

ctx := context.TODO()

defaultTargetCheck := func(t *testing.T, cfg DefaultConfig) {
require.Equal(t, 1, len(cfg.Group["default"].Targets))
require.Equal(t, []string{"image"}, cfg.Group["default"].Targets)
require.Equal(t, 1, len(cfg.Target))
require.Equal(t, ".", *cfg.Target["image"].Context)
}

cases := []struct {
name string
fps []File
inp []string
check func(*testing.T, DefaultConfig)
}{
{
name: "hcl with compose and undefined target",
fps: []File{fpYML, fpHCL},
inp: []string{},
check: defaultTargetCheck,
},
{
name: "json and undefined target",
fps: []File{fpJSON},
inp: []string{},
check: defaultTargetCheck,
},
{
name: "hcl with compose and addon, aws targets",
fps: []File{fpYML, fpHCL},
inp: []string{"addon", "aws"},
check: func(t *testing.T, cfg DefaultConfig) {
require.Equal(t, 2, len(cfg.Group["default"].Targets))
require.Equal(t, []string{"addon", "aws"}, cfg.Group["default"].Targets)
require.Equal(t, 2, len(cfg.Target))
require.Equal(t, "./Dockerfile", *cfg.Target["addon"].Dockerfile)
require.Equal(t, "./aws.Dockerfile", *cfg.Target["aws"].Dockerfile)
},
},
{
name: "hcl with compose and addon, aws, image targets",
fps: []File{fpYML, fpHCL},
inp: []string{"addon", "aws", "image"},
check: func(t *testing.T, cfg DefaultConfig) {
require.Equal(t, 3, len(cfg.Group["default"].Targets))
require.Equal(t, []string{"addon", "aws", "image"}, cfg.Group["default"].Targets)
require.Equal(t, 3, len(cfg.Target))
require.Equal(t, ".", *cfg.Target["image"].Context)
require.Equal(t, "./Dockerfile", *cfg.Target["addon"].Dockerfile)
require.Equal(t, "./aws.Dockerfile", *cfg.Target["aws"].Dockerfile)
},
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
var noTarget bool
if len(tt.inp) == 0 {
tt.inp = []string{"default"}
noTarget = true
}
targets, groups, err := ReadTargets(ctx, tt.fps, tt.inp, nil, nil)
require.NoError(t, err)
tt.check(t, GetDefaultConfig(tt.inp, noTarget, targets, groups))
})
}
}
24 changes: 2 additions & 22 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
if bake.IsRemoteURL(targets[0]) {
cmdContext = targets[0]
targets = targets[1:]

}
}
}
Expand Down Expand Up @@ -124,26 +123,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
}

if in.printOnly {
defGroup := map[string][]string{
"default": targets,
}
if noTarget {
for _, group := range g {
if group.Name != "default" {
continue
}
defGroup = map[string][]string{
"default": group.Targets,
}
}
}
dt, err := json.MarshalIndent(struct {
Group map[string][]string `json:"group,omitempty"`
Target map[string]*bake.Target `json:"target"`
}{
defGroup,
t,
}, "", " ")
dt, err := json.MarshalIndent(bake.GetDefaultConfig(targets, noTarget, t, g), "", " ")
if err != nil {
return err
}
Expand All @@ -152,7 +132,7 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions) (err error
if err != nil {
return err
}
fmt.Fprintln(dockerCli.Out(), string(dt))
_, _ = fmt.Fprintln(dockerCli.Out(), string(dt))
return nil
}

Expand Down

0 comments on commit 9ebe9fd

Please sign in to comment.