From d4453cb44a10f9bb558fd2a5561cc5223ba36a23 Mon Sep 17 00:00:00 2001 From: Skia Date: Mon, 30 May 2022 17:14:31 +0200 Subject: Support multiple profiles with an environment variable This introduces the `RBW_PROFILE` environment variable to be able to run multiple instances in parallel, that will use different configuration paths. To use this feature, simply run `rbw` with the environment variable set to any string that is valid for a path. Setting it to an empty string is like not setting it at all. e.g.: * `RBW_PROFILE=perso rbw list` * `RBW_PROFILE=pro rbw list` --- src/bin/rbw-agent/actions.rs | 2 +- src/dirs.rs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs index 87a8276..61eb74b 100644 --- a/src/bin/rbw-agent/actions.rs +++ b/src/bin/rbw-agent/actions.rs @@ -367,7 +367,7 @@ pub async fn unlock( let password = rbw::pinentry::getpin( &config_pinentry().await?, "Master Password", - "Unlock the local database", + &format!("Unlock the local database for '{}'", rbw::dirs::profile()), err.as_deref(), tty, true, diff --git a/src/dirs.rs b/src/dirs.rs index 5ebeaa2..429f8bd 100644 --- a/src/dirs.rs +++ b/src/dirs.rs @@ -79,32 +79,45 @@ pub fn socket_file() -> std::path::PathBuf { #[must_use] fn config_dir() -> std::path::PathBuf { - let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap(); + let project_dirs = + directories::ProjectDirs::from("", "", &profile()).unwrap(); project_dirs.config_dir().to_path_buf() } #[must_use] fn cache_dir() -> std::path::PathBuf { - let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap(); + let project_dirs = + directories::ProjectDirs::from("", "", &profile()).unwrap(); project_dirs.cache_dir().to_path_buf() } #[must_use] fn data_dir() -> std::path::PathBuf { - let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap(); + let project_dirs = + directories::ProjectDirs::from("", "", &profile()).unwrap(); project_dirs.data_dir().to_path_buf() } #[must_use] fn runtime_dir() -> std::path::PathBuf { - let project_dirs = directories::ProjectDirs::from("", "", "rbw").unwrap(); + let project_dirs = + directories::ProjectDirs::from("", "", &profile()).unwrap(); match project_dirs.runtime_dir() { Some(dir) => dir.to_path_buf(), None => format!( - "{}/rbw-{}", + "{}/{}-{}", std::env::temp_dir().to_string_lossy(), + &profile(), nix::unistd::getuid().as_raw() ) .into(), } } + +#[must_use] +pub fn profile() -> String { + match std::env::var("RBW_PROFILE") { + Ok(profile) if !profile.is_empty() => format!("rbw-{}", profile), + _ => "rbw".to_string(), + } +} -- cgit v1.2.3-54-g00ecf