Browse Source

Remove unwrap in fnm env, make error more readable (#656)

remotes/origin/feat/support-install-latest
Gal Schlezinger 3 years ago committed by GitHub
parent
commit
f83fc48251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      src/commands/env.rs

24
src/commands/env.rs

@ -1,12 +1,12 @@
use super::command::Command; use super::command::Command;
use crate::config::FnmConfig; use crate::config::FnmConfig;
use crate::directories;
use crate::fs::symlink_dir; use crate::fs::symlink_dir;
use crate::outln; use crate::outln;
use crate::path_ext::PathExt; use crate::path_ext::PathExt;
use crate::shell::{infer_shell, Shell, AVAILABLE_SHELLS}; use crate::shell::{infer_shell, Shell, AVAILABLE_SHELLS};
use colored::Colorize; use colored::Colorize;
use std::fmt::Debug; use std::fmt::Debug;
use std::path::PathBuf;
use thiserror::Error; use thiserror::Error;
#[derive(clap::Parser, Debug, Default)] #[derive(clap::Parser, Debug, Default)]
@ -31,20 +31,18 @@ fn generate_symlink_path() -> String {
) )
} }
fn symlink_base_dir() -> PathBuf { fn make_symlink(config: &FnmConfig) -> Result<std::path::PathBuf, Error> {
crate::directories::multishell_storage().ensure_exists_silently() let base_dir = directories::multishell_storage().ensure_exists_silently();
}
fn make_symlink(config: &FnmConfig) -> std::path::PathBuf {
let base_dir = symlink_base_dir();
let mut temp_dir = base_dir.join(generate_symlink_path()); let mut temp_dir = base_dir.join(generate_symlink_path());
while temp_dir.exists() { while temp_dir.exists() {
temp_dir = base_dir.join(generate_symlink_path()); temp_dir = base_dir.join(generate_symlink_path());
} }
symlink_dir(config.default_version_dir(), &temp_dir).expect("Can't create symlink!"); match symlink_dir(config.default_version_dir(), &temp_dir) {
temp_dir Ok(_) => Ok(temp_dir),
Err(source) => Err(Error::CantCreateSymlink { source, temp_dir }),
}
} }
impl Command for Env { impl Command for Env {
@ -65,7 +63,7 @@ impl Command for Env {
.shell .shell
.or_else(&infer_shell) .or_else(&infer_shell)
.ok_or(Error::CantInferShell)?; .ok_or(Error::CantInferShell)?;
let multishell_path = make_symlink(config); let multishell_path = make_symlink(config)?;
let binary_path = if cfg!(windows) { let binary_path = if cfg!(windows) {
multishell_path.clone() multishell_path.clone()
} else { } else {
@ -119,6 +117,12 @@ pub enum Error {
shells_as_string() shells_as_string()
)] )]
CantInferShell, CantInferShell,
#[error("Can't create the symlink for multishells at {temp_dir:?}. Maybe there are some issues with permissions for the directory? {source}")]
CantCreateSymlink {
#[source]
source: std::io::Error,
temp_dir: std::path::PathBuf,
},
} }
fn shells_as_string() -> String { fn shells_as_string() -> String {

Loading…
Cancel
Save