Skip to content

Commit

Permalink
add: uninstall prompt when no version is specified
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhas07 committed Jan 16, 2024
1 parent 6c2d6d8 commit efc32a0
Show file tree
Hide file tree
Showing 3 changed files with 68 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
63 changes: 61 additions & 2 deletions src/handlers/uninstall_handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Result};
use dialoguer::{console::Term, theme::ColorfulTheme, Confirm, MultiSelect};
use reqwest::Client;
use tokio::fs;
use tracing::{info, warn};
Expand All @@ -8,10 +9,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 +34,56 @@ 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?;

Check failure on line 39 in src/handlers/uninstall_handler.rs

View workflow job for this annotation

GitHub Actions / clippy (ubuntu-latest)

this expression creates a reference which is immediately dereferenced by the compiler

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 {

Check failure on line 47 in src/handlers/uninstall_handler.rs

View workflow job for this annotation

GitHub Actions / clippy (ubuntu-latest)

this expression creates a reference which is immediately dereferenced by the compiler
Ok(value) => value,
Err(_) => continue,
};

if helpers::version::is_version_used(&version.tag_name, &config).await {

Check failure on line 52 in src/handlers/uninstall_handler.rs

View workflow job for this annotation

GitHub Actions / clippy (ubuntu-latest)

this expression creates a reference which is immediately dereferenced by the compiler
continue;
}
installed_versions.push(version.tag_name);
}

let selections = MultiSelect::with_theme(&ColorfulTheme::default())
.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.len() > 0) => {

Check failure on line 64 in src/handlers/uninstall_handler.rs

View workflow job for this annotation

GitHub Actions / clippy (ubuntu-latest)

length comparison to zero
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..."),
}
return Ok(());

Check failure on line 88 in src/handlers/uninstall_handler.rs

View workflow job for this annotation

GitHub Actions / clippy (ubuntu-latest)

unneeded `return` statement
}

0 comments on commit efc32a0

Please sign in to comment.