()
| 192 | } |
| 193 | |
| 194 | func initConfig() { |
| 195 | var defaultConfig = &Config{} |
| 196 | |
| 197 | // Find home directory. |
| 198 | home, err := homedir.Dir() |
| 199 | if err != nil { |
| 200 | fmt.Println(err) |
| 201 | os.Exit(1) |
| 202 | } |
| 203 | |
| 204 | viper.AddConfigPath(home) |
| 205 | viper.AddConfigPath("$HOME/.target") |
| 206 | viper.SetConfigName("profiles") |
| 207 | viper.SetConfigType("json") |
| 208 | |
| 209 | // Attempt to read the config file |
| 210 | if err := viper.ReadInConfig(); err != nil { |
| 211 | var configFileNotFoundError viper.ConfigFileNotFoundError |
| 212 | if errors.As(err, &configFileNotFoundError) { |
| 213 | // Config file not found, use default configuration |
| 214 | c = defaultConfig |
| 215 | } |
| 216 | } else { |
| 217 | // Config file found and successfully loaded |
| 218 | configOption := viper.DecodeHook(mapstructure.ComposeDecodeHookFunc( |
| 219 | sliceOfMapsToMapHookFunc(), |
| 220 | mapstructure.StringToTimeDurationHookFunc(), |
| 221 | mapstructure.StringToSliceHookFunc(","), |
| 222 | )) |
| 223 | if err := viper.Unmarshal(&c, configOption); err != nil { |
| 224 | fmt.Println("Error unmarshaling config:", err) |
| 225 | os.Exit(1) |
| 226 | } |
| 227 | |
| 228 | if c.Vault == nil { |
| 229 | c.Vault = map[string]*Vault{} |
| 230 | } |
| 231 | if c.Nomad == nil { |
| 232 | c.Nomad = map[string]*Nomad{} |
| 233 | } |
| 234 | if c.Consul == nil { |
| 235 | c.Consul = map[string]*Consul{} |
| 236 | } |
| 237 | if c.Boundary == nil { |
| 238 | c.Boundary = map[string]*Boundary{} |
| 239 | } |
| 240 | if c.Terraform == nil { |
| 241 | c.Terraform = map[string]*Terraform{} |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Automatically bind environment variables |
| 246 | viper.AutomaticEnv() |
| 247 | |
| 248 | } |
nothing calls this directly
no test coverage detected