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

Commit

Permalink
support relative paths for volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuriy Bogdanov committed Jul 3, 2015
1 parent d2a642d commit 03ef8ad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/compose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"math"
"os"
"path"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -370,7 +372,38 @@ func ReadConfigFile(filename string, vars map[string]interface{}) (*Config, erro
}
defer fd.Close()

return ReadConfig(filename, fd, vars)
config, err := ReadConfig(filename, fd, vars)
if err != nil {
return nil, err
}

// Process relative paths in volumes
dir := filepath.Dir(filename)
if !path.IsAbs(dir) {
wd, err := os.Getwd()
if err != nil {
return nil, err
}
dir = path.Join(wd, dir)
}

for _, container := range config.Containers {
for i, volume := range container.Volumes {
split := strings.SplitN(volume, ":", 2)
if len(split) == 1 {
continue
}
if strings.HasPrefix(split[0], "~") {
split[0] = strings.Replace(split[0], "~", os.Getenv("HOME"), 1)
}
if !path.IsAbs(split[0]) {
split[0] = path.Join(dir, split[0])
}
container.Volumes[i] = strings.Join(split, ":")
}
}

return config, nil
}

func ReadConfig(name string, reader io.Reader, vars map[string]interface{}) (*Config, error) {
Expand Down
3 changes: 3 additions & 0 deletions src/compose/testdata/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ containers:
config:
image: dockerhub.grammarly.io/patterns-config:{{ .version.config | default "latest" }}
state: created
volumes:
- ~/.ssh/id_rsa:/root/.ssh/id_rsa
- ./log:/var/log:ro

extdata:
image: dockerhub.grammarly.io/patterns-extdata:{{ .version.extdata | default "latest" }}
Expand Down

0 comments on commit 03ef8ad

Please sign in to comment.