command(bake): Specify local and remote bake files

This adds the ability to source additional local build definition files when
sourcing Bake files via a remote url.
Prefixing a file with 'cwd://' will source a bake file on the local
machine, instead of the remote location.
Local files will be read/have precedence before remote files.

Usage:
```
docker buildx bake https://github.com/example/upstream.git --file cwd://docker-bake.override.hcl --print
```
This will source a default file from the example/upstream repository,
and also source a build definition from the local machine.

Signed-off-by: Cameron Adams <pnzreba@gmail.com>
This commit is contained in:
Cameron Adams
2023-08-12 17:42:14 +02:00
committed by Justin Chadwell
parent 86ae8ea854
commit 78616f2531
2 changed files with 73 additions and 3 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/containerd/console"
"github.com/containerd/containerd/platforms"
@@ -142,11 +143,19 @@ func runBake(dockerCli command.Cli, targets []string, in bakeOptions, cFlags com
}
}
}()
localFilesArg, remoteFilesArg := getFilesArgs(in.files)
if url != "" {
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, in.files, printer)
files, inp, err = bake.ReadRemoteFiles(ctx, nodes, url, remoteFilesArg, printer)
if len(localFilesArg) > 0 {
var localFiles []bake.File
localFiles, err = bake.ReadLocalFiles(localFilesArg, dockerCli.In())
files = append(files, localFiles...)
}
} else {
files, err = bake.ReadLocalFiles(in.files, dockerCli.In())
var filesArg []string
filesArg = append(filesArg, localFilesArg...)
filesArg = append(filesArg, remoteFilesArg...)
files, err = bake.ReadLocalFiles(filesArg, dockerCli.In())
}
if err != nil {
return err
@@ -257,3 +266,16 @@ func bakeCmd(dockerCli command.Cli, rootOpts *rootOptions) *cobra.Command {
return cmd
}
func getFilesArgs(s []string) ([]string, []string) {
var localFiles []string
var remoteFiles []string
for _, v := range s {
if strings.HasPrefix(v, "cwd://") {
localFiles = append(localFiles, strings.TrimPrefix(v, "cwd://"))
} else {
remoteFiles = append(remoteFiles, v)
}
}
return localFiles, remoteFiles
}