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 9644286
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cli/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use eyre::Result;

mod generate;
mod ls;
mod prune;

/// [experimental] Manage config files
#[derive(Debug, clap::Args)]
Expand All @@ -20,13 +21,15 @@ pub struct Config {
enum Commands {
Ls(ls::ConfigLs),
Generate(generate::ConfigGenerate),
Prune(prune::ConfigPrune),
}

impl Commands {
pub fn run(self) -> Result<()> {
match self {
Self::Ls(cmd) => cmd.run(),
Self::Generate(cmd) => cmd.run(),
Self::Prune(cmd) => cmd.run(),
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/cli/config/prune.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::{
cli::trust::Trust,
config::{tracking::Tracker, Settings},
};
use eyre::Result;

/// [experimental] Prune tracked and trusted config file references
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct ConfigPrune {}

impl ConfigPrune {
pub fn run(self) -> Result<()> {
let settings = Settings::try_get()?;
settings.ensure_experimental("`mise config prune`")?;
debug!("pruning tracked configs");
Tracker::clean()?;

debug!("pruning trusted configs");
Trust::clean()?;

info!("pruned config file references");
Ok(())
}
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise config prune</bold>
"#
);
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

0 comments on commit 9644286

Please sign in to comment.