MCPcopy Index your code
hub / github.com/jetkvm/kvm / LoadConfig

Function LoadConfig

config.go:242–338  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

240)
241
242func 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

Callers 4

rpcGetUsbConfigFunction · 0.85
rpcSetUsbConfigFunction · 0.85
ensureConfigLoadedFunction · 0.85
MainFunction · 0.85

Calls 10

GetRootLoggerFunction · 0.92
getDefaultConfigFunction · 0.85
usbDevicesConfigHasAudioFunction · 0.85
DebugMethod · 0.80
WarnMethod · 0.80
UpdateLogLevelMethod · 0.80
InfoMethod · 0.80
LockMethod · 0.45
UnlockMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected