Skip to content

Commit

Permalink
internal: Rework uninstall command. (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Dec 19, 2024
1 parent 5b456cd commit 7c9fd93
Show file tree
Hide file tree
Showing 10 changed files with 294 additions and 243 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#### 💥 Breaking

- Removed the `--global` option from `proto alias`, `unalias`, `pin`, and `unpin`, use `--to` or `--from` instead.
- Removed the `--purge` option from `proto clean`, use `proto uninstall` instead.

## Unreleased

Expand Down
125 changes: 37 additions & 88 deletions crates/cli/src/commands/clean.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use crate::session::ProtoSession;
use clap::Args;
use iocraft::prelude::element;
use proto_core::{Id, ProtoError, Tool, VersionSpec, PROTO_PLUGIN_KEY};
use proto_core::{ProtoError, Tool, VersionSpec, PROTO_PLUGIN_KEY};
use proto_shim::get_exe_file_name;
use rustc_hash::FxHashSet;
use starbase::AppResult;
use starbase_console::ui::*;
use starbase_styles::color;
use starbase_utils::fs;
use std::io::stdout;
use std::io::IsTerminal;
use std::time::{Duration, SystemTime};
use tracing::debug;

Expand All @@ -21,14 +19,6 @@ pub struct CleanArgs {
)]
pub days: Option<u8>,

#[arg(
long,
help = "Purge and delete the installed tool by ID",
group = "purge-type",
value_name = "TOOL"
)]
pub purge: Option<Id>,

#[arg(
long,
help = "Purge and delete all installed plugins",
Expand Down Expand Up @@ -147,23 +137,25 @@ pub async fn clean_tool(
return Ok(0);
}

session
.console
.render_interactive(element! {
Confirm(
label: format!(
"Found {} versions, remove {}?",
count,
versions_to_clean
.iter()
.map(|v| format!("<hash>{v}</hash>"))
.collect::<Vec<_>>()
.join(", ")
),
value: &mut confirmed,
)
})
.await?;
if !yes {
session
.console
.render_interactive(element! {
Confirm(
label: format!(
"Found {} versions, remove {}?",
count,
versions_to_clean
.iter()
.map(|v| format!("<hash>{v}</hash>"))
.collect::<Vec<_>>()
.join(", ")
),
value: &mut confirmed,
)
})
.await?;
}

if yes || confirmed {
for version in versions_to_clean {
Expand Down Expand Up @@ -243,63 +235,25 @@ pub async fn clean_proto(session: &ProtoSession, days: u64) -> miette::Result<us
Ok(clean_count)
}

#[tracing::instrument(skip(session, yes))]
pub async fn purge_tool(session: &ProtoSession, id: &Id, yes: bool) -> miette::Result<Tool> {
let tool = session.load_tool(id).await?;
let inventory_dir = tool.get_inventory_dir();
let mut confirmed = false;

session
.console
.render_interactive(element! {
Confirm(
label: format!(
"Purge all of {} at <path>{}</path>?",
tool.get_name(),
inventory_dir.display()
),
value: &mut confirmed,
)
})
.await?;

if yes || confirmed {
// Delete inventory
fs::remove_dir_all(inventory_dir)?;

// Delete binaries
for bin in tool.resolve_bin_locations(true).await? {
session.env.store.unlink_bin(&bin.path)?;
}

// Delete shims
for shim in tool.resolve_shim_locations().await? {
session.env.store.remove_shim(&shim.path)?;
}

println!("Purged {}", tool.get_name());
}

Ok(tool.tool)
}

#[tracing::instrument(skip_all)]
pub async fn purge_plugins(session: &ProtoSession, yes: bool) -> AppResult {
let plugins_dir = &session.env.store.plugins_dir;
let mut confirmed = false;

session
.console
.render_interactive(element! {
Confirm(
label: format!(
"Purge all plugins in <path>{}</path>?",
plugins_dir.display()
),
value: &mut confirmed,
)
})
.await?;
if !yes {
session
.console
.render_interactive(element! {
Confirm(
label: format!(
"Purge all plugins in <path>{}</path>?",
plugins_dir.display()
),
value: &mut confirmed,
)
})
.await?;
}

if yes || confirmed {
fs::remove_dir_all(plugins_dir)?;
Expand Down Expand Up @@ -373,19 +327,14 @@ pub async fn internal_clean(

#[tracing::instrument(skip_all)]
pub async fn clean(session: ProtoSession, args: CleanArgs) -> AppResult {
let force_yes = args.yes || !stdout().is_terminal();

if let Some(id) = &args.purge {
purge_tool(&session, id, force_yes).await?;
return Ok(None);
}
let skip_prompts = session.skip_prompts(args.yes);

if args.purge_plugins {
purge_plugins(&session, force_yes).await?;
purge_plugins(&session, skip_prompts).await?;
return Ok(None);
}

internal_clean(&session, args, force_yes, true).await?;
internal_clean(&session, args, skip_prompts, true).await?;

Ok(None)
}
31 changes: 17 additions & 14 deletions crates/cli/src/commands/outdated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,26 @@ pub async fn outdated(session: ProtoSession, args: OutdatedArgs) -> AppResult {
return Ok(None);
}

let skip_prompts = session.skip_prompts(args.yes);
let mut confirmed = false;

session
.console
.render_interactive(element! {
Confirm(
label: if args.latest {
"Update config files with latest versions?"
} else {
"Update config files with newest versions?"
},
value: &mut confirmed,
)
})
.await?;
if !skip_prompts {
session
.console
.render_interactive(element! {
Confirm(
label: if args.latest {
"Update config files with latest versions?"
} else {
"Update config files with newest versions?"
},
value: &mut confirmed,
)
})
.await?;
}

if args.yes || confirmed {
if skip_prompts || confirmed {
let mut updates: BTreeMap<PathBuf, BTreeMap<Id, UnresolvedVersionSpec>> = BTreeMap::new();

for (id, item) in &items {
Expand Down
Loading

0 comments on commit 7c9fd93

Please sign in to comment.