github.com/docker/libcompose@v0.4.1-0.20210616120443-2a046c0bdbf2/lookup/envfile.go (about)

     1  package lookup
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/docker/cli/opts"
     7  	"github.com/docker/libcompose/config"
     8  )
     9  
    10  // EnvfileLookup is a structure that implements the project.EnvironmentLookup interface.
    11  // It holds the path of the file where to lookup environment values.
    12  type EnvfileLookup struct {
    13  	Path string
    14  }
    15  
    16  // Lookup creates a string slice of string containing a "docker-friendly" environment string
    17  // in the form of 'key=value'. It gets environment values using a '.env' file in the specified
    18  // path.
    19  func (l *EnvfileLookup) Lookup(key string, config *config.ServiceConfig) []string {
    20  	envs, err := opts.ParseEnvFile(l.Path)
    21  	if err != nil {
    22  		return []string{}
    23  	}
    24  	for _, env := range envs {
    25  		e := strings.Split(env, "=")
    26  		if e[0] == key {
    27  			return []string{env}
    28  		}
    29  	}
    30  	return []string{}
    31  }