fn load_daemon_cfg()

in aziotctl/src/internal/check/checks/well_formed_configs.rs [169:219]


fn load_daemon_cfg<T: serde::de::DeserializeOwned>(
    daemon: &str,
    config_path: &Path,
    config_directory_path: Option<&Path>,
    shared: &CheckerShared,
) -> Result<DaemonCfg<T>> {
    let daemon_cfg = match config_common::read_config(config_path, config_directory_path) {
        Ok(daemon_cfg) => daemon_cfg,

        Err(config_common::Error::ReadConfig(path, err)) => match err.downcast_ref::<io::Error>() {
            Some(err) if err.kind() == io::ErrorKind::PermissionDenied => {
                if let Some(path) = path {
                    return Ok(DaemonCfg::PermissionDenied(
                        anyhow::anyhow!("{}", err)
                            .context(format!("error in file {}", path.display()))
                            .context(
                                "Could not open file. You might need to run this command as root.",
                            ),
                    ));
                }

                return Ok(DaemonCfg::PermissionDenied(
                    anyhow::anyhow!("{}", err).context(
                        "Could not open file. You might need to run this command as root.",
                    ),
                ));
            }

            _ => {
                let message = if shared.cfg.verbose {
                    format!(
                        "{daemon}'s configuration is not well-formed.\n\
                        Note: In case of syntax errors, the error may not be exactly at the reported line number and position."
                    )
                } else {
                    format!("{daemon}'s configuration file is not well-formed.")
                };

                if let Some(path) = path {
                    return Err(anyhow::anyhow!("{}", err)
                        .context(format!("error in file {}", path.display()))
                        .context(message));
                }

                return Err(anyhow::anyhow!("{}", err).context(message));
            }
        },
    };

    Ok(DaemonCfg::Cfg(daemon_cfg))
}