MCPcopy
hub / github.com/caddyserver/caddy / loadEnvFromFile

Function loadEnvFromFile

cmd/main.go:352–380  ·  view source on GitHub ↗
(envFile string)

Source from the content-addressed store, hash-verified

350}
351
352func loadEnvFromFile(envFile string) error {
353 file, err := os.Open(envFile)
354 if err != nil {
355 return fmt.Errorf("reading environment file: %v", err)
356 }
357 defer file.Close()
358
359 envMap, err := parseEnvFile(file)
360 if err != nil {
361 return fmt.Errorf("parsing environment file: %v", err)
362 }
363
364 for k, v := range envMap {
365 // do not overwrite existing environment variables
366 _, exists := os.LookupEnv(k)
367 if !exists {
368 if err := os.Setenv(k, v); err != nil {
369 return fmt.Errorf("setting environment variables: %v", err)
370 }
371 }
372 }
373
374 // Update the storage paths to ensure they have the proper
375 // value after loading a specified env file.
376 caddy.ConfigAutosavePath = filepath.Join(caddy.AppConfigDir(), "autosave.json")
377 caddy.DefaultStorage = &certmagic.FileStorage{Path: caddy.AppDataDir()}
378
379 return nil
380}
381
382// parseEnvFile parses an env file from KEY=VALUE format.
383// It's pretty naive. Limited value quotation is supported,

Callers 1

handleEnvFileFlagFunction · 0.85

Calls 3

parseEnvFileFunction · 0.85
OpenMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected