Skip to content

env_file option of docker-compose supported #9950

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

Merged
merged 1 commit into from
Aug 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package project
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

log "github.com/golang/glog"
)

// Lookup creates a string slice of string containing a "docker-friendly" environment string
Expand Down Expand Up @@ -99,3 +104,58 @@ func ParseEnvFile(filename string) ([]string, error) {
}
return lines, scanner.Err()
}

// relativePath returns the proper relative path for the given file path. If
// the relativeTo string equals "-", then it means that it's from the stdin,
// and the returned path will be the current working directory. Otherwise, if
// file is really an absolute path, then it will be returned without any
// changes. Otherwise, the returned path will be a combination of relativeTo
// and file.
func relativePath(file, relativeTo string) string {
// stdin: return the current working directory if possible.
if relativeTo == "-" {
if cwd, err := os.Getwd(); err == nil {
return filepath.Join(cwd, file)
}
}

// If the given file is already an absolute path, just return it.
// Otherwise, the returned path will be relative to the given relativeTo
// path.
if filepath.IsAbs(file) {
return file
}

abs, err := filepath.Abs(filepath.Join(path.Dir(relativeTo), file))
if err != nil {
log.V(4).Infof("Failed to get absolute directory: %s", err)
return file
}
return abs
}

// FileResourceLookup is a "bare" structure that implements the project.ResourceLookup interface
type FileResourceLookup struct {
}

// Lookup returns the content and the actual filename of the file that is "built" using the
// specified file and relativeTo string. file and relativeTo are supposed to be file path.
// If file starts with a slash ('/'), it tries to load it, otherwise it will build a
// filename using the folder part of relativeTo joined with file.
func (f *FileResourceLookup) Lookup(file, relativeTo string) ([]byte, string, error) {
file = relativePath(file, relativeTo)
log.V(4).Infof("Reading file %s", file)
bytes, err := ioutil.ReadFile(file)
return bytes, file, err
}

// ResolvePath returns the path to be used for the given path volume. This
// function already takes care of relative paths.
func (f *FileResourceLookup) ResolvePath(path, relativeTo string) string {
vs := strings.SplitN(path, ":", 2)
if len(vs) != 2 || filepath.IsAbs(vs[0]) {
return path
}
vs[0] = relativePath(vs[0], relativeTo)
return strings.Join(vs, ":")
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Event struct {
// AddEnvironmentLookUp adds mechanism for extracting environment
// variables, from operating system or .env file
func AddEnvironmentLookUp(context *Context) error {
if context.ResourceLookup == nil {
context.ResourceLookup = &FileResourceLookup{}
}

if context.EnvironmentLookup == nil {
cwd, err := os.Getwd()
if err != nil {
Expand Down