From 4654cf1781d50f155259ba65eeb1556eebc7eff8 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Tue, 16 Jul 2024 05:35:13 -0700 Subject: [PATCH] refactor: move global config to meta config --- rivet-toolchain/src/backend/database.rs | 4 ++-- .../src/config/{global.rs => meta.rs} | 22 +++++++++---------- rivet-toolchain/src/config/mod.rs | 2 +- rivet-toolchain/src/ctx.rs | 2 +- rivet-toolchain/src/paths.rs | 8 +++---- .../src/tasks/check_login_state.rs | 2 +- rivet-toolchain/src/tasks/deploy/backend.rs | 2 +- .../src/tasks/start_device_link.rs | 2 +- rivet-toolchain/src/tasks/wait_for_login.rs | 4 ++-- rivet-toolchain/src/util/telemetry.rs | 4 ++-- 10 files changed, 25 insertions(+), 27 deletions(-) rename rivet-toolchain/src/config/{global.rs => meta.rs} (86%) diff --git a/rivet-toolchain/src/backend/database.rs b/rivet-toolchain/src/backend/database.rs index b7fe10ca..761bcb29 100644 --- a/rivet-toolchain/src/backend/database.rs +++ b/rivet-toolchain/src/backend/database.rs @@ -20,7 +20,7 @@ pub async fn provision_database( .await?; // Fetch remote DB URL - let mut global_project_config = config::global::mutate_project(|config| { + let mut global_project_config = config::meta::mutate_project(|config| { config .opengb .projects @@ -49,7 +49,7 @@ pub async fn provision_database( env_config.url = db_url_res.url; // Update cache - config::global::try_mutate_project(|config| { + config::meta::try_mutate_project(|config| { // Was inserted in last `mutate_project` call let project = unwrap!(config.opengb.projects.get_mut(&project_id)); diff --git a/rivet-toolchain/src/config/global.rs b/rivet-toolchain/src/config/meta.rs similarity index 86% rename from rivet-toolchain/src/config/global.rs rename to rivet-toolchain/src/config/meta.rs index 882ab93a..e78f386c 100644 --- a/rivet-toolchain/src/config/global.rs +++ b/rivet-toolchain/src/config/meta.rs @@ -13,7 +13,7 @@ use crate::paths; /// Configuration stored globally on the file system. #[derive(Default, Serialize, Deserialize)] -pub struct GlobalConfig { +pub struct Meta { /// Store project meta by the absolute path to the project. pub project_roots: HashMap, } @@ -93,19 +93,19 @@ pub struct OpenGbEnv { pub url: Option, } -static SINGLETON: OnceCell> = OnceCell::const_new(); +static SINGLETON: OnceCell> = OnceCell::const_new(); /// Gets the global config instance. /// /// Use `read` to read properties from the config. -async fn get_or_init() -> GlobalResult<&'static Mutex> { +async fn get_or_init() -> GlobalResult<&'static Mutex> { SINGLETON .get_or_try_init::(|| async { - let path = paths::global_config_file()?; + let path = paths::meta_config_file()?; let config = match fs::read_to_string(&path).await { Ok(config) => serde_json::from_str(&config).map_err(Into::::into)?, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => GlobalConfig::default(), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Meta::default(), Err(err) => return Err(err.into()), }; @@ -117,17 +117,15 @@ async fn get_or_init() -> GlobalResult<&'static Mutex> { /// Writes the config to the file system. /// /// Use `mutate` to make changes to the config publicly. -async fn write(config: &GlobalConfig) -> GlobalResult<()> { - fs::create_dir_all(paths::global_config_dir()?).await?; - fs::write(paths::global_config_file()?, serde_json::to_string(config)?).await?; +async fn write(config: &Meta) -> GlobalResult<()> { + fs::create_dir_all(paths::user_config_dir()?).await?; + fs::write(paths::meta_config_file()?, serde_json::to_string(config)?).await?; Ok(()) } /// Reads from the global config. -pub async fn try_read_global GlobalResult, T>( - cb: F, -) -> GlobalResult { +pub async fn try_read_global GlobalResult, T>(cb: F) -> GlobalResult { let singleton = get_or_init().await?; let mut lock = singleton.lock().await; @@ -159,7 +157,7 @@ pub async fn read_project T, T>(cb: F) -> GlobalResul try_read_project(|x| Ok(cb(x))).await } -pub async fn try_mutate_global GlobalResult, T>( +pub async fn try_mutate_global GlobalResult, T>( cb: F, ) -> GlobalResult { let singleton = get_or_init().await?; diff --git a/rivet-toolchain/src/config/mod.rs b/rivet-toolchain/src/config/mod.rs index 0d32cb63..02e8d173 100644 --- a/rivet-toolchain/src/config/mod.rs +++ b/rivet-toolchain/src/config/mod.rs @@ -1,2 +1,2 @@ -pub mod global; +pub mod meta; pub mod settings; diff --git a/rivet-toolchain/src/ctx.rs b/rivet-toolchain/src/ctx.rs index d98c0598..73b1d15c 100644 --- a/rivet-toolchain/src/ctx.rs +++ b/rivet-toolchain/src/ctx.rs @@ -32,7 +32,7 @@ pub struct CtxInner { pub async fn load() -> GlobalResult { let (api_endpoint, token) = - config::global::read_project(|x| (x.cluster.api_endpoint.clone(), x.tokens.cloud.clone())) + config::meta::read_project(|x| (x.cluster.api_endpoint.clone(), x.tokens.cloud.clone())) .await?; let token = unwrap!(token); init(api_endpoint, token).await diff --git a/rivet-toolchain/src/paths.rs b/rivet-toolchain/src/paths.rs index 06b192a1..f245f58d 100644 --- a/rivet-toolchain/src/paths.rs +++ b/rivet-toolchain/src/paths.rs @@ -5,16 +5,16 @@ pub fn project_root() -> GlobalResult { Ok(env::current_dir()?) } -pub fn global_config_dir() -> GlobalResult { +pub fn user_config_dir() -> GlobalResult { Ok(unwrap!(dirs::config_dir()).join("rivet")) } -pub fn global_config_file() -> GlobalResult { - Ok(global_config_dir()?.join("config.json")) +pub fn meta_config_file() -> GlobalResult { + Ok(user_config_dir()?.join("meta.json")) } pub fn user_settings_config_file() -> GlobalResult { - Ok(global_config_dir()?.join("settings.json")) + Ok(user_config_dir()?.join("settings.json")) } pub fn project_settings_config_file() -> GlobalResult { diff --git a/rivet-toolchain/src/tasks/check_login_state.rs b/rivet-toolchain/src/tasks/check_login_state.rs index 0f8d44b1..288a757d 100644 --- a/rivet-toolchain/src/tasks/check_login_state.rs +++ b/rivet-toolchain/src/tasks/check_login_state.rs @@ -20,7 +20,7 @@ impl super::Task for Task { } async fn run(task: TaskCtx, input: Input) -> GlobalResult { - let has_token = config::global::read_project(|x| x.tokens.cloud.is_some()).await?; + let has_token = config::meta::read_project(|x| x.tokens.cloud.is_some()).await?; if !has_token { bail!("No Rivet token found, please do the sign in process"); } diff --git a/rivet-toolchain/src/tasks/deploy/backend.rs b/rivet-toolchain/src/tasks/deploy/backend.rs index 0008cce5..0c74f6ee 100644 --- a/rivet-toolchain/src/tasks/deploy/backend.rs +++ b/rivet-toolchain/src/tasks/deploy/backend.rs @@ -75,7 +75,7 @@ pub async fn deploy(ctx: &Ctx, task: TaskCtx, opts: DeployOpts) -> GlobalResult< ) .await?; - let db_url = config::global::try_read_project(|config| { + let db_url = config::meta::try_read_project(|config| { let project = unwrap!(config.opengb.projects.get(&project.project_id)); let env = unwrap!(project.environments.get(&env.environment_id)); diff --git a/rivet-toolchain/src/tasks/start_device_link.rs b/rivet-toolchain/src/tasks/start_device_link.rs index fbd59c2d..51b1c772 100644 --- a/rivet-toolchain/src/tasks/start_device_link.rs +++ b/rivet-toolchain/src/tasks/start_device_link.rs @@ -24,7 +24,7 @@ impl super::Task for Task { } async fn run(task: TaskCtx, input: Self::Input) -> GlobalResult { - let (api_endpoint, _token) = config::global::read_project(|x| { + let (api_endpoint, _token) = config::meta::read_project(|x| { (x.cluster.api_endpoint.clone(), x.tokens.cloud.clone()) }) .await?; diff --git a/rivet-toolchain/src/tasks/wait_for_login.rs b/rivet-toolchain/src/tasks/wait_for_login.rs index f855384f..1a0f3d43 100644 --- a/rivet-toolchain/src/tasks/wait_for_login.rs +++ b/rivet-toolchain/src/tasks/wait_for_login.rs @@ -25,7 +25,7 @@ impl super::Task for Task { } async fn run(task: TaskCtx, input: Self::Input) -> GlobalResult { - let (api_endpoint, _token) = config::global::read_project(|x| { + let (api_endpoint, _token) = config::meta::read_project(|x| { (x.cluster.api_endpoint.clone(), x.tokens.cloud.clone()) }) .await?; @@ -68,7 +68,7 @@ impl super::Task for Task { ) .await?; - config::global::mutate_project(|x| x.tokens.cloud = Some(token)).await?; + config::meta::mutate_project(|x| x.tokens.cloud = Some(token)).await?; Ok(Output { output: "Token saved".to_string(), diff --git a/rivet-toolchain/src/util/telemetry.rs b/rivet-toolchain/src/util/telemetry.rs index e963d692..5cdc46f2 100644 --- a/rivet-toolchain/src/util/telemetry.rs +++ b/rivet-toolchain/src/util/telemetry.rs @@ -49,10 +49,10 @@ pub async fn capture_event GlobalResult< name: &str, mutate: Option, ) -> GlobalResult<()> { - let api_endpoint = config::global::read_project(|x| x.cluster.api_endpoint.clone()) + let api_endpoint = config::meta::read_project(|x| x.cluster.api_endpoint.clone()) .await? .unwrap_or_else(|| ctx::DEFAULT_API_ENDPOINT.to_string()); - let telemetry_disabled = config::global::read_project(|x| x.telemetry.disabled).await?; + let telemetry_disabled = config::meta::read_project(|x| x.telemetry.disabled).await?; let args = std::env::args().collect::>(); let distinct_id = if let Some(game_id) = game_id {