Skip to content

Commit

Permalink
librad: Force construction of default rad home
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Simmerl <[email protected]>
  • Loading branch information
xla committed Sep 9, 2021
1 parent 3067abb commit 1b60712
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
22 changes: 9 additions & 13 deletions librad/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@ pub enum RadHome {
}

impl Default for RadHome {
/// If `RAD_HOME` is defined then the path supplied there is used and
/// [`RadHome::Root`] is constructed. Otherwise, [`RadHome::ProjectDirs`] is
/// constructed.
fn default() -> Self {
Self::new()
if let Ok(root) = env::var(RAD_HOME) {
Self::Root(Path::new(&root).to_path_buf())
} else {
Self::ProjectDirs
}
}
}

Expand All @@ -59,17 +66,6 @@ impl fmt::Display for RadHome {
}

impl RadHome {
/// If `RAD_HOME` is defined then the path supplied there is used and
/// [`RadHome::Root`] is constructed. Otherwise, [`RadHome::ProjectDirs`] is
/// constructed.
pub fn new() -> Self {
if let Ok(root) = env::var(RAD_HOME) {
Self::Root(Path::new(&root).to_path_buf())
} else {
Self::ProjectDirs
}
}

fn config(&self) -> Result<PathBuf, io::Error> {
Ok(match self {
Self::ProjectDirs => project_dirs()?.config_dir().to_path_buf(),
Expand Down Expand Up @@ -159,7 +155,7 @@ impl Profile {
/// `ProjectDirs_DATA_HOME/radicle-link/<profile-id>`.
pub fn load() -> Result<Self, Error> {
let env_profile_id = ProfileId::from_env()?;
let home = RadHome::new();
let home = RadHome::default();
Self::from_home(&home, env_profile_id)
}

Expand Down
14 changes: 7 additions & 7 deletions rad-profile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
C::Error: fmt::Debug + fmt::Display + Send + Sync + 'static,
C::SecretBox: Serialize + DeserializeOwned,
{
let home = RadHome::new();
let home = RadHome::default();
let profile = Profile::new(&home)?;
Profile::set(&home, profile.id().clone())?;
let key = SecretKey::new();
Expand All @@ -82,7 +82,7 @@ where

/// Get the current active `ProfileId`.
pub fn get(id: Option<ProfileId>) -> Result<Option<Profile>, Error> {
let home = RadHome::new();
let home = RadHome::default();
match id {
Some(id) => Profile::get(&home, id).map_err(Error::from),
None => Profile::active(&home).map_err(Error::from),
Expand All @@ -91,13 +91,13 @@ pub fn get(id: Option<ProfileId>) -> Result<Option<Profile>, Error> {

/// Set the active profile to the given `ProfileId`.
pub fn set(id: ProfileId) -> Result<(), Error> {
let home = RadHome::new();
let home = RadHome::default();
Profile::set(&home, id).map_err(Error::from).map(|_| ())
}

/// List the set of active profiles that exist.
pub fn list() -> Result<Vec<Profile>, Error> {
let home = RadHome::new();
let home = RadHome::default();
Profile::list(&home).map_err(Error::from)
}

Expand All @@ -106,7 +106,7 @@ pub fn peer_id<P>(id: P) -> Result<PeerId, Error>
where
P: Into<Option<ProfileId>>,
{
let home = RadHome::new();
let home = RadHome::default();
let profile = get_or_active(&home, id)?;
let read = ReadOnly::open(profile.paths())?;
Ok(*read.peer_id())
Expand All @@ -116,7 +116,7 @@ pub fn paths<P>(id: P) -> Result<Paths, Error>
where
P: Into<Option<ProfileId>>,
{
let home = RadHome::new();
let home = RadHome::default();
get_or_active(&home, id).map(|p| p.paths().clone())
}

Expand All @@ -133,7 +133,7 @@ where
P: Into<Option<ProfileId>>,
S: ClientStream + Unpin + 'static,
{
let home = RadHome::new();
let home = RadHome::default();
let profile = get_or_active(&home, id)?;
let store = keys::file_storage(&profile, crypto);
let key = store.get_key()?;
Expand Down

0 comments on commit 1b60712

Please sign in to comment.