Skip to content

Commit

Permalink
Merge pull request #180 from guilhas07/master
Browse files Browse the repository at this point in the history
add: uninstall prompt when no version is specified
  • Loading branch information
MordechaiHadad authored Jan 17, 2024
2 parents 8791623 + cad390d commit bd3d169
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ If Config::version_sync_file_location is set, the version in that file will be p

---

- `bob uninstall |nightly|stable|latest|<version-string>|<commit-hash>|`
- `bob uninstall [|nightly|stable|latest|<version-string>|<commit-hash>|]`

Uninstall the specified version.
Uninstall the specified version. If no version is specified a prompt is used to select all the versions
to be uninstalled.

---

Expand Down
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ enum Cli {
/// Uninstall the specified version
#[clap(visible_alias = "rm")]
Uninstall {
/// Version to be uninstalled |nightly|stable|<version-string>|<commit-hash>|
version: String,
/// Optional Version to be uninstalled |nightly|stable|<version-string>|<commit-hash>|
/// If no Version is provided a prompt is used to select the versions to be uninstalled
version: Option<String>,
},

/// Rollback to an existing nightly rollback
Expand Down Expand Up @@ -134,7 +135,7 @@ pub async fn start(config: Config) -> Result<()> {
}
Cli::Uninstall { version } => {
info!("Starting uninstallation process");
uninstall_handler::start(&version, config).await?;
uninstall_handler::start(version.as_deref(), config).await?;
}
Cli::Rollback => rollback_handler::start(config).await?,
Cli::Erase => erase_handler::start(config).await?,
Expand Down
78 changes: 76 additions & 2 deletions src/handlers/uninstall_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use anyhow::{anyhow, Result};
use dialoguer::{
console::{style, Term},
theme::ColorfulTheme,
Confirm, MultiSelect,
};
use reqwest::Client;
use tokio::fs;
use tracing::{info, warn};
Expand All @@ -8,10 +13,15 @@ use crate::{
helpers::{self, directories},
};

pub async fn start(version: &str, config: Config) -> Result<()> {
pub async fn start(version: Option<&str>, config: Config) -> Result<()> {
let client = Client::new();
let version = helpers::version::parse_version_type(&client, version).await?;

let version = match version {
Some(value) => value,
None => return uninstall_selections(&client, &config).await,
};

let version = helpers::version::parse_version_type(&client, version).await?;
if helpers::version::is_version_used(&version.tag_name, &config).await {
warn!("Switch to a different version before proceeding");
return Ok(());
Expand All @@ -28,3 +38,67 @@ pub async fn start(version: &str, config: Config) -> Result<()> {
info!("Successfully uninstalled version: {}", version.tag_name);
Ok(())
}

async fn uninstall_selections(client: &Client, config: &Config) -> Result<()> {
let downloads_dir = directories::get_downloads_directory(config).await?;

let mut paths = fs::read_dir(downloads_dir.clone()).await?;
let mut installed_versions: Vec<String> = Vec::new();

while let Some(path) = paths.next_entry().await? {
let name = path.file_name().to_str().unwrap().to_owned();

let version = match helpers::version::parse_version_type(client, &name).await {
Ok(value) => value,
Err(_) => continue,
};

if helpers::version::is_version_used(&version.tag_name, config).await {
continue;
}
installed_versions.push(version.tag_name);
}

if installed_versions.is_empty() {
info!("You only have one neovim instance installed");
return Ok(());
}

let theme = ColorfulTheme {
checked_item_prefix: style("✓".to_string()).for_stderr().green(),
unchecked_item_prefix: style("✓".to_string()).for_stderr().black(),
..ColorfulTheme::default()
};

let selections = MultiSelect::with_theme(&theme)
.with_prompt("Toogle with space the versions you wish to uninstall:")
.items(&installed_versions)
.interact_on_opt(&Term::stderr())?;

match &selections {
Some(ids) if !ids.is_empty() => {
let confirm = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Do you wish to continue?")
.interact_on_opt(&Term::stderr())?;

match confirm {
Some(true) => {}
None | Some(false) => {
info!("Uninstall aborted...");
return Ok(());
}
}

for &i in ids {
let path = downloads_dir.join(&installed_versions[i]);
fs::remove_dir_all(path).await?;
info!(
"Successfully uninstalled version: {}",
&installed_versions[i]
);
}
}
None | Some(_) => info!("Uninstall aborted..."),
}
Ok(())
}

0 comments on commit bd3d169

Please sign in to comment.