Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
refactor: move global config to meta config
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry committed Jul 16, 2024
1 parent 8d0ab65 commit 4654cf1
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions rivet-toolchain/src/backend/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf, ProjectMeta>,
}
Expand Down Expand Up @@ -93,19 +93,19 @@ pub struct OpenGbEnv {
pub url: Option<String>,
}

static SINGLETON: OnceCell<Mutex<GlobalConfig>> = OnceCell::const_new();
static SINGLETON: OnceCell<Mutex<Meta>> = OnceCell::const_new();

/// Gets the global config instance.
///
/// Use `read` to read properties from the config.
async fn get_or_init() -> GlobalResult<&'static Mutex<GlobalConfig>> {
async fn get_or_init() -> GlobalResult<&'static Mutex<Meta>> {
SINGLETON
.get_or_try_init::<GlobalError, _, _>(|| 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::<GlobalError>::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()),
};

Expand All @@ -117,17 +117,15 @@ async fn get_or_init() -> GlobalResult<&'static Mutex<GlobalConfig>> {
/// 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<F: FnOnce(&GlobalConfig) -> GlobalResult<T>, T>(
cb: F,
) -> GlobalResult<T> {
pub async fn try_read_global<F: FnOnce(&Meta) -> GlobalResult<T>, T>(cb: F) -> GlobalResult<T> {
let singleton = get_or_init().await?;
let mut lock = singleton.lock().await;

Expand Down Expand Up @@ -159,7 +157,7 @@ pub async fn read_project<F: FnOnce(&ProjectMeta) -> T, T>(cb: F) -> GlobalResul
try_read_project(|x| Ok(cb(x))).await
}

pub async fn try_mutate_global<F: FnOnce(&mut GlobalConfig) -> GlobalResult<T>, T>(
pub async fn try_mutate_global<F: FnOnce(&mut Meta) -> GlobalResult<T>, T>(
cb: F,
) -> GlobalResult<T> {
let singleton = get_or_init().await?;
Expand Down
2 changes: 1 addition & 1 deletion rivet-toolchain/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod global;
pub mod meta;
pub mod settings;
2 changes: 1 addition & 1 deletion rivet-toolchain/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct CtxInner {

pub async fn load() -> GlobalResult<Ctx> {
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
Expand Down
8 changes: 4 additions & 4 deletions rivet-toolchain/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ pub fn project_root() -> GlobalResult<PathBuf> {
Ok(env::current_dir()?)
}

pub fn global_config_dir() -> GlobalResult<PathBuf> {
pub fn user_config_dir() -> GlobalResult<PathBuf> {
Ok(unwrap!(dirs::config_dir()).join("rivet"))
}

pub fn global_config_file() -> GlobalResult<PathBuf> {
Ok(global_config_dir()?.join("config.json"))
pub fn meta_config_file() -> GlobalResult<PathBuf> {
Ok(user_config_dir()?.join("meta.json"))
}

pub fn user_settings_config_file() -> GlobalResult<PathBuf> {
Ok(global_config_dir()?.join("settings.json"))
Ok(user_config_dir()?.join("settings.json"))
}

pub fn project_settings_config_file() -> GlobalResult<PathBuf> {
Expand Down
2 changes: 1 addition & 1 deletion rivet-toolchain/src/tasks/check_login_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl super::Task for Task {
}

async fn run(task: TaskCtx, input: Input) -> GlobalResult<Output> {
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");
}
Expand Down
2 changes: 1 addition & 1 deletion rivet-toolchain/src/tasks/deploy/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion rivet-toolchain/src/tasks/start_device_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl super::Task for Task {
}

async fn run(task: TaskCtx, input: Self::Input) -> GlobalResult<Self::Output> {
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?;
Expand Down
4 changes: 2 additions & 2 deletions rivet-toolchain/src/tasks/wait_for_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl super::Task for Task {
}

async fn run(task: TaskCtx, input: Self::Input) -> GlobalResult<Self::Output> {
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?;
Expand Down Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions rivet-toolchain/src/util/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ pub async fn capture_event<F: FnOnce(&mut async_posthog::Event) -> GlobalResult<
name: &str,
mutate: Option<F>,
) -> 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::<Vec<_>>();

let distinct_id = if let Some(game_id) = game_id {
Expand Down

0 comments on commit 4654cf1

Please sign in to comment.