envfile.go 828 B

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