Skip to content

Commit d44ffb4

Browse files
rrjjvvcrazy-max
andcommitted
Display source of bake definitions when read from environment
While it would make sense to add "from file" to complement "from env," (in the common case of `--file` or using the default), it wouldn't provide any real value. A simpler solution would have been looking for the existence of the variable at the point where printing happens. It felt wrong duplicating the logic. Executing the same logic (if it was extracted) wouldn't be as bad, but still not ideal. A 'correct' solution would be to explicitly track the source of each definition, which would be clearer and more future-proof. It didn't seem like this feature warranted that amount of engineering (with no known features that might make use of it). This implementation seemed like a fair compromise; none of the functions are exported, and all have only one caller. I also considered converting prefixing environment values with `env://` so they could be thought of (and processed like) `cmd://` values. I didn't think it would be viewed as a good solution. Co-authored-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> Signed-off-by: Roberto Villarreal <rrjjvv@yahoo.com>
1 parent cb54ddb commit d44ffb4

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

commands/bake.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type bakeOptions struct {
6767
listVars bool
6868
}
6969

70-
func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags) (err error) {
70+
func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in bakeOptions, cFlags commonFlags, filesFromEnv bool) (err error) {
7171
mp := dockerCli.MeterProvider()
7272

7373
ctx, end, err := tracing.TraceCurrentCommand(ctx, append([]string{"bake"}, targets...),
@@ -185,7 +185,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
185185
return err
186186
}
187187

188-
files, inp, err := readBakeFiles(ctx, nodes, url, in.files, dockerCli.In(), printer)
188+
files, inp, err := readBakeFiles(ctx, nodes, url, in.files, dockerCli.In(), printer, filesFromEnv)
189189
if err != nil {
190190
return err
191191
}
@@ -457,12 +457,14 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
457457
Aliases: []string{"f"},
458458
Short: "Build from a file",
459459
RunE: func(cmd *cobra.Command, args []string) error {
460+
filesFromEnv := false
460461
if len(options.files) == 0 {
461462
envFiles, err := bakeEnvFiles(os.LookupEnv)
462463
if err != nil {
463464
return err
464465
}
465466
options.files = envFiles
467+
filesFromEnv = true
466468
}
467469
// reset to nil to avoid override is unset
468470
if !cmd.Flags().Lookup("no-cache").Changed {
@@ -481,7 +483,7 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
481483
options.builder = rootOpts.builder
482484
options.metadataFile = cFlags.metadataFile
483485
// Other common flags (noCache, pull and progress) are processed in runBake function.
484-
return runBake(cmd.Context(), dockerCli, args, options, cFlags)
486+
return runBake(cmd.Context(), dockerCli, args, options, cFlags, filesFromEnv)
485487
},
486488
ValidArgsFunction: completion.BakeTargets(options.files),
487489
}
@@ -596,7 +598,7 @@ func bakeArgs(args []string) (url, cmdContext string, targets []string) {
596598
return url, cmdContext, targets
597599
}
598600

599-
func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names []string, stdin io.Reader, pw progress.Writer) (files []bake.File, inp *bake.Input, err error) {
601+
func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names []string, stdin io.Reader, pw progress.Writer, filesFromEnv bool) (files []bake.File, inp *bake.Input, err error) {
600602
var lnames []string // local
601603
var rnames []string // remote
602604
var anames []string // both
@@ -621,7 +623,11 @@ func readBakeFiles(ctx context.Context, nodes []builder.Node, url string, names
621623

622624
if len(lnames) > 0 || url == "" {
623625
var lfiles []bake.File
624-
progress.Wrap("[internal] load local bake definitions", pw.Write, func(sub progress.SubLogger) error {
626+
where := ""
627+
if filesFromEnv {
628+
where = " from " + bakeEnvFilePath + " env"
629+
}
630+
progress.Wrap("[internal] load local bake definitions"+where, pw.Write, func(sub progress.SubLogger) error {
625631
if url != "" {
626632
lfiles, err = bake.ReadLocalFiles(lnames, stdin, sub)
627633
} else {

tests/bake.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,7 +2213,7 @@ target "second" {
22132213

22142214
dt, err := cmd.CombinedOutput()
22152215
require.NoError(t, err, string(dt))
2216-
require.Contains(t, string(dt), `#1 [internal] load local bake definitions`)
2216+
require.Contains(t, string(dt), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env`)
22172217
require.Contains(t, string(dt), `#1 reading first.hcl`)
22182218
})
22192219

@@ -2230,7 +2230,7 @@ target "second" {
22302230

22312231
dt, err := cmd.CombinedOutput()
22322232
require.NoError(t, err, string(dt))
2233-
require.Contains(t, string(dt), `#1 [internal] load local bake definitions`)
2233+
require.Contains(t, string(dt), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env`)
22342234
require.Contains(t, string(dt), `#1 reading first.hcl`)
22352235
require.NotContains(t, string(dt), "docker-bake.hcl")
22362236
})
@@ -2249,7 +2249,7 @@ target "second" {
22492249
withEnv("BUILDX_BAKE_FILE=first.hcl"+string(os.PathListSeparator)+"second.hcl"))
22502250
dt, err := cmd.CombinedOutput()
22512251
require.NoError(t, err, string(dt))
2252-
require.Contains(t, string(dt), `#1 [internal] load local bake definitions`)
2252+
require.Contains(t, string(dt), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env`)
22532253
require.Contains(t, string(dt), `#1 reading first.hcl`)
22542254
require.Contains(t, string(dt), `#1 reading second.hcl`)
22552255
})
@@ -2269,7 +2269,7 @@ target "second" {
22692269

22702270
dt, err := cmd.CombinedOutput()
22712271
require.NoError(t, err, string(dt))
2272-
require.Contains(t, string(dt), `#1 [internal] load local bake definitions`)
2272+
require.Contains(t, string(dt), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env`)
22732273
require.Contains(t, string(dt), `#1 reading first.hcl`)
22742274
require.Contains(t, string(dt), `#1 reading second.hcl`)
22752275
})
@@ -2294,7 +2294,7 @@ target "second" {
22942294

22952295
dt, err := cmd.CombinedOutput()
22962296
require.NoError(t, err, string(dt))
2297-
require.Contains(t, string(dt), `#1 [internal] load local bake definitions`)
2297+
require.Contains(t, string(dt), `#1 [internal] load local bake definitions from BUILDX_BAKE_FILE env`)
22982298
require.Contains(t, string(dt), `#1 reading first.hcl`)
22992299
require.Contains(t, string(dt), `#1 reading from stdin`)
23002300
})

0 commit comments

Comments
 (0)