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
  • Loading branch information
roele committed May 6, 2024
1 parent 40e82be commit 8864d42
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 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 {
Tracker::clean()?;
Trust::clean()?;
info!("pruned configuration links");
} else {
info!("pruned configuration links {}", style("[dryrun]").bold());
}
Ok(())
}

fn prune_tools(&self) -> Result<()> {
let config = Config::try_get()?;
let ts = ToolsetBuilder::new().build(&config)?;
let mut to_delete = ts
Expand Down
12 changes: 12 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,15 @@ impl Trust {
self.trust()
}
}
pub fn clean() -> Result<()> {
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
1 change: 0 additions & 1 deletion 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 Down

0 comments on commit 8864d42

Please sign in to comment.