Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust argument slice size in builder #164

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,19 @@ func ParseFile(path string) (*parser.Node, error) {

// Step creates a new step from the current state.
func (b *Builder) Step() *Step {
dst := make([]string, len(b.Env)+len(b.RunConfig.Env))
copy(dst, makeUserArgs(b))
argsMap := make(map[string]string)
for _, argsVal := range b.Arguments() {
val := strings.Split(argsVal, "=")
if len(val) > 1 {
argsMap[val[0]] = val[1]
}
}

userArgs := makeUserArgs(b.Env, argsMap)
dst := make([]string, len(userArgs)+len(b.RunConfig.Env))
copy(dst, userArgs)
dst = append(dst, b.RunConfig.Env...)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the values from b.Args won't be used if they conflict with environment values, so the size count being used here for allocating dst can be off, and if it is, it can leave a gap with empty strings in the final slice.


return &Step{Env: dst}
}

Expand Down
4 changes: 2 additions & 2 deletions dispatchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func add(b *Builder, args []string, attributes map[string]bool, flagArgs []strin
var chown string
last := len(args) - 1
dest := makeAbsolute(args[last], b.RunConfig.WorkingDir)
userArgs := makeUserArgs(b)
userArgs := makeUserArgs(b.Env, b.Args)
for _, a := range flagArgs {
arg, err := ProcessWord(a, userArgs)
if err != nil {
Expand Down Expand Up @@ -182,7 +182,7 @@ func dispatchCopy(b *Builder, args []string, attributes map[string]bool, flagArg
dest := makeAbsolute(args[last], b.RunConfig.WorkingDir)
var chown string
var from string
userArgs := makeUserArgs(b)
userArgs := makeUserArgs(b.Env, b.Args)
for _, a := range flagArgs {
arg, err := ProcessWord(a, userArgs)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,18 @@ func parseOptInterval(f *flag.Flag) (time.Duration, error) {
// defined by both can later be evaluated when resolving variables
// such as ${MY_USER}. If the variable is defined by both ARG and ENV
// don't include the definition of the ARG variable.
func makeUserArgs(b *Builder) (userArgs []string) {
func makeUserArgs(bEnv []string, bArgs map[string]string) (userArgs []string) {

userArgs = b.Env
userArgs = bEnv
envMap := make(map[string]string)
for _, envVal := range b.Env {
for _, envVal := range bEnv {
val := strings.Split(envVal, "=")
if len(val) > 1 {
envMap[val[0]] = val[1]
}
}

for key, value := range b.Args {
for key, value := range bArgs {
if _, ok := envMap[key]; ok {
continue
}
Expand Down