Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Enable PATH variable to be expanded as default value, if not set by prior ENV command #98

Merged
merged 2 commits into from
Apr 26, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ var (

// ExportsPath is the path within EXPORT volume containers
ExportsPath = "/.rocker_exports"

// DefaultPathEnv is a system PATH variable to be used in Env substitutions
// if path is not set by ENV command
DefaultPathEnv = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
)

// Config used specify parameters for the builder in New()
Expand Down
7 changes: 6 additions & 1 deletion src/build/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,13 @@ func (c *CommandOnbuildWrap) ReplaceEnv(env []string) error {
////////// Private stuff //////////

func replaceEnv(args []string, env []string) (err error) {

defaultEnv := []string{"PATH=" + DefaultPathEnv}

env1 := replaceOrAppendEnvValues(defaultEnv, env)

for i, v := range args {
if args[i], err = shellparser.ProcessWord(v, env); err != nil {
if args[i], err = shellparser.ProcessWord(v, env1); err != nil {
return err
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/md5"
"fmt"
"io"
"strings"

"github.com/fsouza/go-dockerclient"
)
Expand Down Expand Up @@ -85,3 +86,25 @@ func (r readerVoidCloser) Read(p []byte) (int, error) {
func (r readerVoidCloser) Close() error {
return nil
}

func replaceOrAppendEnvValues(defaults, overrides []string) []string {

cache := make(map[string]int, len(defaults))
for i, e := range defaults {
parts := strings.SplitN(e, "=", 2)
cache[parts[0]] = i
}

for _, value := range overrides {
parts := strings.SplitN(value, "=", 2)

if i, exists := cache[parts[0]]; exists {
defaults[i] = value
} else {
cache[parts[0]] = len(defaults)
defaults = append(defaults, value)
}
}

return defaults
}
11 changes: 11 additions & 0 deletions test/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ RUN ls -l /replaced-onbuild-bar`, "--no-cache")

assert.Nil(t, err, "should expand variable in ONBUILD command")
}

func TestEnvExpansionPath(t *testing.T) {
err := runRockerBuildWithOptions(`
FROM alpine
ENV foo=/opt/foo/bin:$PATH
RUN echo $foo
RUN test $foo == /opt/foo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
`, "--no-cache")
assert.Nil(t, err, "should use PATH from the default PATH setting, if PATH is not set "+
"with ENV command in any of the parent containers")
}