()
| 240 | ) |
| 241 | |
| 242 | func LoadConfig() { |
| 243 | configLock.Lock() |
| 244 | defer configLock.Unlock() |
| 245 | |
| 246 | if config != nil { |
| 247 | logger.Debug().Msg("config already loaded, skipping") |
| 248 | return |
| 249 | } |
| 250 | |
| 251 | // load the default config |
| 252 | defaultConfig := getDefaultConfig() |
| 253 | config = &defaultConfig |
| 254 | |
| 255 | file, err := os.Open(configPath) |
| 256 | if err != nil { |
| 257 | logger.Debug().Msg("default config file doesn't exist, using default") |
| 258 | configSuccess.Set(1.0) |
| 259 | configSuccessTime.SetToCurrentTime() |
| 260 | return |
| 261 | } |
| 262 | defer file.Close() |
| 263 | |
| 264 | // load and merge the default config with the user config |
| 265 | rawConfig, err := io.ReadAll(file) |
| 266 | if err != nil { |
| 267 | logger.Warn().Err(err).Msg("config file reading failed") |
| 268 | configSuccess.Set(0.0) |
| 269 | return |
| 270 | } |
| 271 | |
| 272 | loadedConfig := defaultConfig |
| 273 | if err := json.Unmarshal(rawConfig, &loadedConfig); err != nil { |
| 274 | logger.Warn().Err(err).Msg("config file JSON parsing failed") |
| 275 | configSuccess.Set(0.0) |
| 276 | return |
| 277 | } |
| 278 | |
| 279 | // merge the user config with the default config |
| 280 | if loadedConfig.UsbConfig == nil { |
| 281 | loadedConfig.UsbConfig = getDefaultConfig().UsbConfig |
| 282 | } |
| 283 | |
| 284 | if loadedConfig.UsbDevices == nil { |
| 285 | loadedConfig.UsbDevices = getDefaultConfig().UsbDevices |
| 286 | } else if !usbDevicesConfigHasAudio(rawConfig) { |
| 287 | loadedConfig.UsbDevices.Audio = defaultUsbDevices.Audio |
| 288 | } |
| 289 | |
| 290 | if !loadedConfig.UsbDevices.Audio { |
| 291 | loadedConfig.AudioEnabled = false |
| 292 | } |
| 293 | |
| 294 | if loadedConfig.NetworkConfig == nil { |
| 295 | loadedConfig.NetworkConfig = getDefaultConfig().NetworkConfig |
| 296 | } |
| 297 | |
| 298 | if loadedConfig.JigglerConfig == nil { |
| 299 | loadedConfig.JigglerConfig = getDefaultConfig().JigglerConfig |
no test coverage detected