Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Feb 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::command::Command;
use crate::config::FnmConfig;
use crate::directories;
use crate::fs::symlink_dir;
use crate::outln;
use crate::path_ext::PathExt;
use crate::shell::{infer_shell, Shell, AVAILABLE_SHELLS};
use colored::Colorize;
use std::fmt::Debug;
use std::path::PathBuf;
use thiserror::Error;

#[derive(clap::Parser, Debug, Default)]
Expand All @@ -31,20 +31,18 @@ fn generate_symlink_path() -> String {
)
}

fn symlink_base_dir() -> PathBuf {
crate::directories::multishell_storage().ensure_exists_silently()
}

fn make_symlink(config: &FnmConfig) -> std::path::PathBuf {
let base_dir = symlink_base_dir();
fn make_symlink(config: &FnmConfig) -> Result<std::path::PathBuf, Error> {
let base_dir = directories::multishell_storage().ensure_exists_silently();
let mut temp_dir = base_dir.join(generate_symlink_path());

while temp_dir.exists() {
temp_dir = base_dir.join(generate_symlink_path());
}

symlink_dir(config.default_version_dir(), &temp_dir).expect("Can't create symlink!");
temp_dir
match symlink_dir(config.default_version_dir(), &temp_dir) {
Ok(_) => Ok(temp_dir),
Err(source) => Err(Error::CantCreateSymlink { source, temp_dir }),
}
}

impl Command for Env {
Expand All @@ -65,7 +63,7 @@ impl Command for Env {
.shell
.or_else(&infer_shell)
.ok_or(Error::CantInferShell)?;
let multishell_path = make_symlink(config);
let multishell_path = make_symlink(config)?;
let binary_path = if cfg!(windows) {
multishell_path.clone()
} else {
Expand Down Expand Up @@ -119,6 +117,12 @@ pub enum Error {
shells_as_string()
)]
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 {
Expand Down