Skip to content

Commit

Permalink
feat: added mise ls --prunable flag (#4062)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx authored Jan 11, 2025
1 parent 18338bc commit 1db6945
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/cli/ls.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ Display missing tool versions

Display versions matching this prefix

### `--prunable`

List only tools that can be pruned with `mise prune`

### `--no-header`

Don't display headers
Expand Down
2 changes: 2 additions & 0 deletions docs/cli/prune.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Versions which are no longer the latest specified in any of those configs are de
Versions installed only with environment variables `MISE_<PLUGIN>_VERSION` will be deleted,
as will versions only referenced on the command line `mise exec <PLUGIN>@<VERSION>`.

You can list prunable tools with `mise ls --prunable`

## Arguments

### `[INSTALLED_TOOL]...`
Expand Down
3 changes: 3 additions & 0 deletions e2e/cli/test_ls
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ mise use cargo-binstall
mise i cargo:usage-cli
assert_contains "mise ls" "cargo:usage-cli"
assert_not_contains "mise ls" "cargo-usage-cli" # if the backend meta file isn't working right these will be displayed

assert "mise ls --prunable" "cargo:usage-cli 2.0.3
tiny 2.0.0"
3 changes: 2 additions & 1 deletion mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ cmd ls help="List installed and active tool versions" {
flag --prefix help="Display versions matching this prefix" {
arg <PREFIX>
}
flag --prunable help="List only tools that can be pruned with `mise prune`"
flag --no-header help="Don't display headers"
arg "[INSTALLED_TOOL]..." help="Only show tool versions from [TOOL]" required=#false var=#true
}
Expand Down Expand Up @@ -544,7 +545,7 @@ cmd plugins help="Manage plugins" {
}
}
cmd prune help="Delete unused versions of tools" {
long_help "Delete unused versions of tools\n\nmise tracks which config files have been used in ~/.local/state/mise/tracked-configs\nVersions which are no longer the latest specified in any of those configs are deleted.\nVersions installed only with environment variables `MISE_<PLUGIN>_VERSION` will be deleted,\nas will versions only referenced on the command line `mise exec <PLUGIN>@<VERSION>`."
long_help "Delete unused versions of tools\n\nmise tracks which config files have been used in ~/.local/state/mise/tracked-configs\nVersions which are no longer the latest specified in any of those configs are deleted.\nVersions installed only with environment variables `MISE_<PLUGIN>_VERSION` will be deleted,\nas will versions only referenced on the command line `mise exec <PLUGIN>@<VERSION>`.\n\nYou can list prunable tools with `mise ls --prunable`"
after_long_help "Examples:\n\n $ mise prune --dry-run\n rm -rf ~/.local/share/mise/versions/node/20.0.0\n rm -rf ~/.local/share/mise/versions/node/20.0.1\n"
flag "-n --dry-run" help="Do not actually delete anything"
flag --configs help="Prune only tracked and trusted configuration links that point to non-existent configurations"
Expand Down
18 changes: 17 additions & 1 deletion src/cli/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use versions::Versioning;

use crate::backend::Backend;
use crate::cli::args::BackendArg;
use crate::cli::prune;
use crate::config;
use crate::config::Config;
use crate::toolset::{ToolSource, ToolVersion, Toolset};
Expand Down Expand Up @@ -65,6 +66,10 @@ pub struct Ls {
#[clap(long, requires = "installed_tool")]
prefix: Option<String>,

/// List only tools that can be pruned with `mise prune`
#[clap(long)]
prunable: bool,

/// Don't display headers
#[clap(long, alias = "no-headers", verbatim_doc_comment, conflicts_with_all = &["json"])]
no_header: bool,
Expand All @@ -78,7 +83,11 @@ impl Ls {
.or_else(|| self.tool_flag.clone().map(|p| vec![p]));
self.verify_plugin()?;

let mut runtimes = self.get_runtime_list(&config)?;
let mut runtimes = if self.prunable {
self.get_prunable_runtime_list()?
} else {
self.get_runtime_list(&config)?
};
if self.current || self.global {
// TODO: global is a little weird: it will show global versions as the active ones even if
// they're overridden locally
Expand Down Expand Up @@ -165,6 +174,13 @@ impl Ls {
table.truncate(true).print()
}

fn get_prunable_runtime_list(&self) -> Result<Vec<RuntimeRow>> {
let installed_tool = self.installed_tool.clone().unwrap_or_default();
Ok(prune::prunable_tools(installed_tool.iter().collect())?
.into_iter()
.map(|(p, tv)| (self, p, tv, ToolSource::Unknown))
.collect())
}
fn get_runtime_list(&self, config: &Config) -> Result<Vec<RuntimeRow>> {
let mut trs = config.get_tool_request_set()?.clone();
if self.global {
Expand Down
11 changes: 9 additions & 2 deletions src/cli/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use super::trust::Trust;
/// Versions which are no longer the latest specified in any of those configs are deleted.
/// Versions installed only with environment variables `MISE_<PLUGIN>_VERSION` will be deleted,
/// as will versions only referenced on the command line `mise exec <PLUGIN>@<VERSION>`.
///
/// You can list prunable tools with `mise ls --prunable`
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Prune {
Expand Down Expand Up @@ -66,7 +68,7 @@ impl Prune {
}
}

pub fn prune(tools: Vec<&BackendArg>, dry_run: bool) -> Result<()> {
pub fn prunable_tools(tools: Vec<&BackendArg>) -> Result<Vec<(Arc<dyn Backend>, ToolVersion)>> {
let config = Config::try_get()?;
let ts = ToolsetBuilder::new().build(&config)?;
let mut to_delete = ts
Expand All @@ -87,7 +89,12 @@ pub fn prune(tools: Vec<&BackendArg>, dry_run: bool) -> Result<()> {
}
}

delete(dry_run, to_delete.into_values().collect())
Ok(to_delete.into_values().collect())
}

pub fn prune(tools: Vec<&BackendArg>, dry_run: bool) -> Result<()> {
let to_delete = prunable_tools(tools)?;
delete(dry_run, to_delete)
}

fn delete(dry_run: bool, to_delete: Vec<(Arc<dyn Backend>, ToolVersion)>) -> Result<()> {
Expand Down
5 changes: 5 additions & 0 deletions xtasks/fig/src/mise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,11 @@ const completionSpec: Fig.Spec = {
debounce: true,
},
},
{
name: "--prunable",
description: "List only tools that can be pruned with `mise prune`",
isRepeatable: false,
},
{
name: "--no-header",
description: "Don't display headers",
Expand Down

0 comments on commit 1db6945

Please sign in to comment.