(v *viper.Viper)
| 39 | } |
| 40 | |
| 41 | func initConfig(v *viper.Viper) { |
| 42 | if envFile := os.Getenv("ENV_FILE"); envFile != "" { |
| 43 | v.SetConfigFile(envFile) |
| 44 | } else { |
| 45 | v.SetConfigName(getConfigName()) |
| 46 | v.SetConfigType("env") |
| 47 | envPath := getEnvPath() |
| 48 | v.AddConfigPath(envPath) |
| 49 | |
| 50 | paths := []string{ |
| 51 | "./../../../../..", |
| 52 | "./../../../..", |
| 53 | "./../../..", |
| 54 | "./../..", |
| 55 | "./..", |
| 56 | "./", |
| 57 | } |
| 58 | for _, path := range paths { |
| 59 | v.AddConfigPath(path) |
| 60 | } |
| 61 | |
| 62 | for _, path := range paths { |
| 63 | filePath := filepath.Join(path, getConfigName()) |
| 64 | fileInfo, err := os.Stat(filePath) |
| 65 | if err == nil && !fileInfo.IsDir() { |
| 66 | envFile = filePath |
| 67 | break |
| 68 | } |
| 69 | } |
| 70 | v.SetConfigFile(envFile) |
| 71 | } |
| 72 | |
| 73 | if _, err := os.Stat(v.ConfigFileUsed()); err != nil { |
| 74 | if os.IsNotExist(err) { |
| 75 | logrus.Info("no [.env] file, devlake will read configuration from environment, please make sure you have set correct environment variable.") |
| 76 | } else { |
| 77 | panic(fmt.Errorf("failed to get config file info: %v", err)) |
| 78 | } |
| 79 | } else { |
| 80 | if err := v.ReadInConfig(); err != nil { |
| 81 | panic(fmt.Errorf("failed to read configuration file: %v", err)) |
| 82 | } |
| 83 | // This line is essential for reading |
| 84 | v.WatchConfig() |
| 85 | } |
| 86 | |
| 87 | v.AutomaticEnv() |
| 88 | setDefaultValue(v) |
| 89 | } |
| 90 | |
| 91 | func getConfigName() string { |
| 92 | return defaultConfigName |
no test coverage detected