From f8d97f26b7a662870660415741c89c671be7c21e Mon Sep 17 00:00:00 2001 From: Nick Schuch Date: Tue, 16 Sep 2025 14:02:18 +1000 Subject: [PATCH] Adds --build-args --- cmd/skpr/package/command.go | 1 + internal/buildpack/builder/builder.go | 20 ++++++++++++++------ internal/command/package/command.go | 6 ++++++ internal/slice/slice.go | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/cmd/skpr/package/command.go b/cmd/skpr/package/command.go index 7ef4b18..b615659 100644 --- a/cmd/skpr/package/command.go +++ b/cmd/skpr/package/command.go @@ -44,6 +44,7 @@ func NewCommand() *cobra.Command { cmd.Flags().BoolVar(&command.Params.NoPush, "no-push", command.Params.NoPush, "Do not push the image to the registry.") cmd.Flags().BoolVar(&command.PrintManifest, "print-manifest", command.PrintManifest, "Print the manifest to stdout.") cmd.Flags().StringVar(&command.PackageDir, "dir", ".skpr/package", "The location of the package directory.") + cmd.Flags().StringSliceVar(&command.BuildArgs, "build-arg", []string{}, "Additional build arguments.") cmd.Flags().BoolVar(&command.Debug, "debug", command.Debug, "Enable debug output.") return cmd diff --git a/internal/buildpack/builder/builder.go b/internal/buildpack/builder/builder.go index c78db16..da7c759 100644 --- a/internal/buildpack/builder/builder.go +++ b/internal/buildpack/builder/builder.go @@ -56,12 +56,13 @@ type Image struct { // Params used for building the applications. type Params struct { - Auth docker.AuthConfiguration - Writer io.Writer - Context string - Registry string - NoPush bool - Version string + Auth docker.AuthConfiguration + Writer io.Writer + Context string + Registry string + NoPush bool + Version string + BuildArgs map[string]string } // Dockerfiles the docker build files. @@ -100,6 +101,13 @@ func (b *Builder) Build(dockerfiles Dockerfiles, params Params) (BuildResponse, }, } + for k, v := range params.BuildArgs { + args = append(args, docker.BuildArg{ + Name: k, + Value: v, + }) + } + start := time.Now() // We build the compile image first, as it is the base image for other images. diff --git a/internal/command/package/command.go b/internal/command/package/command.go index 73dcae2..d3e8faa 100644 --- a/internal/command/package/command.go +++ b/internal/command/package/command.go @@ -24,6 +24,7 @@ import ( "github.com/skpr/cli/internal/buildpack/utils/finder" "github.com/skpr/cli/internal/buildpack/utils/notation/utils" "github.com/skpr/cli/internal/client" + "github.com/skpr/cli/internal/slice" ) // Command to package an application. @@ -32,6 +33,7 @@ type Command struct { PackageDir string Params buildpack.Params PrintManifest bool + BuildArgs []string Debug bool } @@ -66,6 +68,10 @@ func (cmd *Command) Run(ctx context.Context) error { Password: client.Credentials.Password, } + // Convert build args from slice to map. + // eg. --build-arg=KEY=VALUE to map[string]string{"KEY": "VALUE"} + cmd.Params.BuildArgs = slice.ToMap(cmd.BuildArgs, "=") + isECR := ecr.IsRegistry(cmd.Params.Registry) // @todo, Consider abstracting this if another registry + credentials pair is required. diff --git a/internal/slice/slice.go b/internal/slice/slice.go index 3009638..8d83640 100644 --- a/internal/slice/slice.go +++ b/internal/slice/slice.go @@ -1,5 +1,7 @@ package slice +import "strings" + // Contains a string within a slice. func Contains(slice []string, s string) bool { for _, item := range slice { @@ -67,3 +69,20 @@ func AppendIfMissing(slice []string, i string) []string { return append(slice, i) } + +// ToMap converts a slice of strings into a map using the provided delimiter. +func ToMap(slice []string, delimiter string) map[string]string { + m := make(map[string]string, len(slice)) + + for _, s := range slice { + sl := strings.Split(s, delimiter) + + if len(sl) != 2 { + continue + } + + m[sl[0]] = sl[1] + } + + return m +}