loadEnvFile reads the file at path using godotenv and sets any variables not already present in the process environment. It returns the number of variables set.
(path string)
| 485 | // not already present in the process environment. It returns the number of |
| 486 | // variables set. |
| 487 | func loadEnvFile(path string) (int, error) { |
| 488 | vars, err := godotenv.Read(path) |
| 489 | if err != nil { |
| 490 | return 0, err |
| 491 | } |
| 492 | var n int |
| 493 | for key, val := range vars { |
| 494 | if _, exists := os.LookupEnv(key); exists { |
| 495 | continue |
| 496 | } |
| 497 | if err := os.Setenv(key, val); err != nil { |
| 498 | return n, err |
| 499 | } |
| 500 | n++ |
| 501 | } |
| 502 | return n, nil |
| 503 | } |
| 504 | |
| 505 | // filterEnv returns env with any variables whose key matches exclude removed. |
| 506 | func filterEnv(env []string, exclude ...string) []string { |