diff --git a/src/commands/use.rs b/src/commands/use.rs index 5f91eed..3c44b17 100644 --- a/src/commands/use.rs +++ b/src/commands/use.rs @@ -3,7 +3,6 @@ use super::install::Install; use crate::fs; use crate::installed_versions; use crate::outln; -use crate::symlink_path::DefaultMultishellPathExt; use crate::system_version; use crate::user_version::UserVersion; use crate::version::Version; @@ -24,10 +23,8 @@ impl Command for Use { type Error = Error; fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> { - let multishell_path = config - .multishell_path_or_default() - .context(FnmEnvWasNotSourced)?; - warn_if_multishell_path_not_in_path_env_var(&multishell_path, config); + let multishell_path = config.multishell_path().context(FnmEnvWasNotSourced)?; + warn_if_multishell_path_not_in_path_env_var(multishell_path, config); let all_versions = installed_versions::list(config.installations_dir()).context(VersionListingError)?; @@ -81,7 +78,7 @@ impl Command for Use { } }; - replace_symlink(&version_path, &multishell_path).context(SymlinkingCreationIssue)?; + replace_symlink(&version_path, multishell_path).context(SymlinkingCreationIssue)?; Ok(()) } @@ -200,5 +197,5 @@ pub enum Error { "You should setup your shell profile to evaluate `fnm env`, see https://github.com/Schniz/fnm#shell-setup on how to do this", "Check out our documentation for more information: https://fnm.vercel.app" ))] - FnmEnvWasNotSourced { source: Box }, + FnmEnvWasNotSourced, } diff --git a/src/current_version.rs b/src/current_version.rs index c2be3e8..6f7fab3 100644 --- a/src/current_version.rs +++ b/src/current_version.rs @@ -1,12 +1,10 @@ +use crate::config::FnmConfig; use crate::system_version; use crate::version::Version; -use crate::{config::FnmConfig, symlink_path::DefaultMultishellPathExt}; -use snafu::{ResultExt, Snafu}; +use snafu::{OptionExt, ResultExt, Snafu}; pub fn current_version(config: &FnmConfig) -> Result, Error> { - let multishell_path = config - .multishell_path_or_default() - .context(CantCreateMultishellPath)?; + let multishell_path = config.multishell_path().context(EnvNotApplied)?; if multishell_path.read_link().ok() == Some(system_version::path()) { return Ok(Some(Version::Bypassed)); @@ -33,7 +31,7 @@ pub enum Error { #[snafu(display( "`fnm env` was not applied in this context.\nCan't find fnm's environment variables" ))] - CantCreateMultishellPath { source: Box }, + EnvNotApplied, #[snafu(display("Can't read the version as a valid semver"))] VersionError { source: semver::Error, diff --git a/src/main.rs b/src/main.rs index 8ba1f8e..53003e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,6 @@ mod version_files; #[macro_use] mod log_level; -mod symlink_path; fn main() { env_logger::init(); diff --git a/src/symlink_path.rs b/src/symlink_path.rs deleted file mode 100644 index a6d45c3..0000000 --- a/src/symlink_path.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::{fs::create_dir_all, path::PathBuf}; - -use log::info; -use sysinfo::{get_current_pid, ProcessExt, System, SystemExt}; - -use crate::config::FnmConfig; - -pub fn generate_symlink_token_based_on_pid() -> Result { - let mut system = System::new(); - let current_pid = get_current_pid()?; - - system.refresh_process(current_pid); - let current_process = system - .process(current_pid) - .ok_or("Couldn't find the current process in the tree")?; - - let parent_pid = current_process - .parent() - .ok_or("Couldn't find the parent pid in the process tree")?; - - Ok(format!("pid_{}", parent_pid)) -} - -pub(crate) trait DefaultMultishellPathExt { - fn multishell_path_or_default(&self) -> Result>; -} - -impl DefaultMultishellPathExt for FnmConfig { - fn multishell_path_or_default(&self) -> Result> { - if let Some(given_path) = self.multishell_path() { - return Ok(given_path.to_path_buf()); - } - - let symlink_token = generate_symlink_token_based_on_pid()?; - let multishells_path = std::env::temp_dir().join("fnm_multishells"); - let new_path = multishells_path.join(symlink_token); - - info!( - "No multishell path given, generating a new one at {}", - new_path.display() - ); - - create_dir_all(&multishells_path)?; - - Ok(new_path) - } -}