MCPcopy Create free account
hub / github.com/compose-spec/compose-go / WithEnvFiles

Function WithEnvFiles

cli/options.go:263–297  ·  view source on GitHub ↗

WithEnvFiles set env file(s) to be loaded to set project environment. defaults to local .env file if no explicit file is selected, until COMPOSE_DISABLE_ENV_FILE is set

(file ...string)

Source from the content-addressed store, hash-verified

261// WithEnvFiles set env file(s) to be loaded to set project environment.
262// defaults to local .env file if no explicit file is selected, until COMPOSE_DISABLE_ENV_FILE is set
263func WithEnvFiles(file ...string) ProjectOptionsFn {
264 return func(o *ProjectOptions) error {
265 if len(file) > 0 {
266 o.EnvFiles = file
267 return nil
268 }
269 if v, ok := os.LookupEnv(consts.ComposeDisableDefaultEnvFile); ok {
270 b, err := strconv.ParseBool(v)
271 if err != nil {
272 return err
273 }
274 if b {
275 return nil
276 }
277 }
278
279 wd, err := o.GetWorkingDir()
280 if err != nil {
281 return err
282 }
283 defaultDotEnv := filepath.Join(wd, ".env")
284
285 s, err := os.Stat(defaultDotEnv)
286 if os.IsNotExist(err) {
287 return nil
288 }
289 if err != nil {
290 return err
291 }
292 if !s.IsDir() {
293 o.EnvFiles = []string{defaultDotEnv}
294 }
295 return nil
296 }
297}
298
299// WithDotEnv imports environment variables from .env file
300func WithDotEnv(o *ProjectOptions) error {

Callers 5

TestProjectNameFunction · 0.85
TestProjectWithDotEnvFunction · 0.85
WithEnvFileFunction · 0.85

Calls 2

LookupEnvMethod · 0.80
GetWorkingDirMethod · 0.80

Tested by 4

TestProjectNameFunction · 0.68
TestProjectWithDotEnvFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…