Refactor enterprise config functions to return String instead of Option

This commit is contained in:
Thorsten Sommer 2025-05-31 19:07:44 +02:00
parent ea3271bc0c
commit 1fb04a73cf
No known key found for this signature in database
GPG Key ID: B0B7E2FC074BF1F5

View File

@ -48,7 +48,7 @@ pub fn read_user_language(_token: APIToken) -> String {
} }
#[get("/system/enterprise/config/id")] #[get("/system/enterprise/config/id")]
pub fn read_enterprise_env_config_id(_token: APIToken) -> Option<String> { pub fn read_enterprise_env_config_id(_token: APIToken) -> String {
// //
// When we are on a Windows machine, we try to read the enterprise config from // When we are on a Windows machine, we try to read the enterprise config from
// the Windows registry. In case we can't find the registry key, or we are on a // the Windows registry. In case we can't find the registry key, or we are on a
@ -71,7 +71,7 @@ pub fn read_enterprise_env_config_id(_token: APIToken) -> Option<String> {
} }
#[get("/system/enterprise/config/server")] #[get("/system/enterprise/config/server")]
pub fn read_enterprise_env_config_server_url(_token: APIToken) -> Option<String> { pub fn read_enterprise_env_config_server_url(_token: APIToken) -> String {
// //
// When we are on a Windows machine, we try to read the enterprise config from // When we are on a Windows machine, we try to read the enterprise config from
// the Windows registry. In case we can't find the registry key, or we are on a // the Windows registry. In case we can't find the registry key, or we are on a
@ -93,11 +93,11 @@ pub fn read_enterprise_env_config_server_url(_token: APIToken) -> Option<String>
) )
} }
fn get_enterprise_configuration(reg_value: &str, env_name: &str) -> Option<String> { fn get_enterprise_configuration(_reg_value: &str, env_name: &str) -> String {
info!("Trying to read the enterprise environment for some predefined configuration."); info!("Trying to read the enterprise environment for some predefined configuration.");
cfg_if::cfg_if! { cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] { if #[cfg(target_os = "windows")] {
info!(r"Detected a Windows machine, trying to read the registry key 'HKEY_CURRENT_USER\Software\github\MindWork AI Studio\Enterprise IT' or the environment variables"); info!(r"Detected a Windows machine, trying to read the registry key 'HKEY_CURRENT_USER\Software\github\MindWork AI Studio\Enterprise IT' or environment variables.");
use windows_registry::*; use windows_registry::*;
let key_path = r"Software\github\MindWork AI Studio\Enterprise IT"; let key_path = r"Software\github\MindWork AI Studio\Enterprise IT";
let key = match CURRENT_USER.open(key_path) { let key = match CURRENT_USER.open(key_path) {
@ -107,28 +107,28 @@ fn get_enterprise_configuration(reg_value: &str, env_name: &str) -> Option<Strin
return match env::var(env_name) { return match env::var(env_name) {
Ok(val) => { Ok(val) => {
info!("Falling back to the environment variable '{}' was successful.", env_name); info!("Falling back to the environment variable '{}' was successful.", env_name);
Some(val) val
}, },
Err(_) => { Err(_) => {
info!("Falling back to the environment variable '{}' was not successful. It appears that this is not an enterprise environment.", env_name); info!("Falling back to the environment variable '{}' was not successful. It appears that this is not an enterprise environment.", env_name);
None "".to_string()
}, },
} }
}, },
}; };
match key.get_string(reg_value) { match key.get_string(_reg_value) {
Ok(val) => Some(val), Ok(val) => val,
Err(_) => { Err(_) => {
info!(r"We could read the registry key 'HKEY_CURRENT_USER\Software\github\MindWork AI Studio\Enterprise IT', but the value '{}' could not be read. Falling back to environment variables.", reg_value); info!(r"We could read the registry key 'HKEY_CURRENT_USER\Software\github\MindWork AI Studio\Enterprise IT', but the value '{}' could not be read. Falling back to environment variables.", _reg_value);
match env::var(env_name) { match env::var(env_name) {
Ok(val) => { Ok(val) => {
info!("Falling back to the environment variable '{}' was successful.", env_name); info!("Falling back to the environment variable '{}' was successful.", env_name);
Some(val) val
}, },
Err(_) => { Err(_) => {
info!("Falling back to the environment variable '{}' was not successful. It appears that this is not an enterprise environment.", env_name); info!("Falling back to the environment variable '{}' was not successful. It appears that this is not an enterprise environment.", env_name);
None "".to_string()
} }
} }
}, },
@ -136,7 +136,13 @@ fn get_enterprise_configuration(reg_value: &str, env_name: &str) -> Option<Strin
} else { } else {
// In the case of macOS or Linux, we just read the environment variable: // In the case of macOS or Linux, we just read the environment variable:
info!(r"Detected a Unix machine, trying to read the environment variable '{}'.", env_name); info!(r"Detected a Unix machine, trying to read the environment variable '{}'.", env_name);
env::var(env_name).ok() match env::var(env_name) {
Ok(val) => val,
Err(_) => {
info!("The environment variable '{}' was not found. It appears that this is not an enterprise environment.", env_name);
"".to_string()
}
}
} }
} }
} }