Browse Source

Use local cache directory instead of temp directory for symlinks (#638)

* Use local cache directory instead of temp directory for symlinks

still with no cleanup, probably need to have --cleanup-duration=... for fnm env
or another command for cleaning up old symlinks.

* Introduce directories, and support XDG_ directories for cache directories

* cargo clippy --fix
remotes/origin/feat/support-install-latest
Gal Schlezinger 3 years ago committed by GitHub
parent
commit
ad0ecd7fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/commands/env.rs
  2. 26
      src/directories.rs
  3. 1
      src/main.rs

9
src/commands/env.rs

@ -6,6 +6,7 @@ use crate::path_ext::PathExt; @@ -6,6 +6,7 @@ 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)]
@ -30,10 +31,12 @@ fn generate_symlink_path() -> String { @@ -30,10 +31,12 @@ 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 = std::env::temp_dir()
.join("fnm_multishells")
.ensure_exists_silently();
let base_dir = symlink_base_dir();
let mut temp_dir = base_dir.join(generate_symlink_path());
while temp_dir.exists() {

26
src/directories.rs

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
use std::path::PathBuf;
fn xdg_dir(env: &str) -> Option<PathBuf> {
let env_var = std::env::var(env).ok()?;
Some(PathBuf::from(env_var))
}
fn state_dir() -> Option<PathBuf> {
xdg_dir("XDG_STATE_HOME").or_else(dirs::state_dir)
}
fn cache_dir() -> Option<PathBuf> {
xdg_dir("XDG_CACHE_HOME").or_else(dirs::cache_dir)
}
fn runtime_dir() -> Option<PathBuf> {
xdg_dir("XDG_RUNTIME_DIR").or_else(dirs::runtime_dir)
}
pub fn multishell_storage() -> PathBuf {
runtime_dir()
.or_else(state_dir)
.or_else(cache_dir)
.unwrap_or_else(std::env::temp_dir)
.join("fnm_multishells")
}

1
src/main.rs

@ -34,6 +34,7 @@ mod version_files; @@ -34,6 +34,7 @@ mod version_files;
#[macro_use]
mod log_level;
mod default_version;
mod directories;
fn main() {
env_logger::init();

Loading…
Cancel
Save