Skip to content

Commit

Permalink
feat: cleanup invalid symlinks in .local/state/mise/(tracked|trusted)…
Browse files Browse the repository at this point in the history
…-configs (#2036)
  • Loading branch information
roele committed May 6, 2024
1 parent 40e82be commit 6c557cf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 6 deletions.
32 changes: 32 additions & 0 deletions src/cli/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use console::style;
use eyre::Result;

use crate::cli::args::ForgeArg;
use crate::config::tracking::Tracker;
use crate::config::{Config, Settings};
use crate::forge::Forge;
use crate::toolset::{ToolVersion, ToolsetBuilder};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::prompt;

use super::trust::Trust;

/// Delete unused versions of tools
///
/// mise tracks which config files have been used in ~/.local/share/mise/tracked_config_files
Expand All @@ -27,10 +30,39 @@ pub struct Prune {
/// Do not actually delete anything
#[clap(long, short = 'n')]
pub dry_run: bool,

/// Prune only tracked and trusted configuration links that point to non-existent configurations
#[clap(long)]
pub configs: bool,

/// Prune only unused versions of tools
#[clap(long)]
pub tools: bool,
}

impl Prune {
pub fn run(self) -> Result<()> {
if self.configs || !self.tools {
self.prune_configs()?;
}
if self.tools || !self.configs {
self.prune_tools()?;
}
Ok(())
}

fn prune_configs(&self) -> Result<()> {
if self.dry_run {
info!("pruned configuration links {}", style("[dryrun]").bold());
} else {
Tracker::clean()?;
Trust::clean()?;
info!("pruned configuration links");
}
Ok(())
}

fn prune_tools(&self) -> Result<()> {
let config = Config::try_get()?;
let ts = ToolsetBuilder::new().build(&config)?;
let mut to_delete = ts
Expand Down
14 changes: 14 additions & 0 deletions src/cli/trust.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::fs::read_dir;
use std::path::PathBuf;

use clap::ValueHint;
use eyre::Result;

use crate::config;
use crate::config::{config_file, DEFAULT_CONFIG_FILENAMES};
use crate::dirs::TRUSTED_CONFIGS;
use crate::file::remove_file;

/// Marks a config file as trusted
///
Expand Down Expand Up @@ -44,6 +47,17 @@ impl Trust {
self.trust()
}
}
pub fn clean() -> Result<()> {
if TRUSTED_CONFIGS.is_dir() {
for path in read_dir(&*TRUSTED_CONFIGS)? {
let path = path?.path();
if !path.exists() {
remove_file(&path)?;
}
}
}
Ok(())
}
fn untrust(&self) -> Result<()> {
let path = match &self.config_file {
Some(filename) => PathBuf::from(filename),
Expand Down
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::{dirs, env, file, forge};
pub mod config_file;
mod env_directive;
pub mod settings;
mod tracking;
pub mod tracking;

type AliasMap = BTreeMap<ForgeArg, BTreeMap<String, String>>;
type ConfigMap = IndexMap<PathBuf, Box<dyn ConfigFile>>;
Expand Down
11 changes: 6 additions & 5 deletions src/config/tracking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ impl Tracker {
}

pub fn list_all() -> Result<Vec<PathBuf>> {
Self::clean()?;
let mut output = vec![];
for path in read_dir(&*TRACKED_CONFIGS)? {
let path = path?.path();
Expand All @@ -37,10 +36,12 @@ impl Tracker {
}

pub fn clean() -> Result<()> {
for path in read_dir(&*TRACKED_CONFIGS)? {
let path = path?.path();
if !path.exists() {
remove_file(&path)?;
if TRACKED_CONFIGS.is_dir() {
for path in read_dir(&*TRACKED_CONFIGS)? {
let path = path?.path();
if !path.exists() {
remove_file(&path)?;
}
}
}
Ok(())
Expand Down

0 comments on commit 6c557cf

Please sign in to comment.