Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: unable to remove nightly version #160

Merged
merged 7 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 129 additions & 110 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ version = "0.2"
optional = false

[target.'cfg(target_os = "macos")'.dependencies]
flate2 = "1.0"
flate2 = "1.0.26"
tar = "0.4"

[target."cfg(windows)".dependencies]
Expand Down
16 changes: 0 additions & 16 deletions bob.norg

This file was deleted.

3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum Cli {
shell: Shell,
},

/// Update existing version
/// Update existing version |nightly|stable|--all|
Update(Update),
}

Expand Down Expand Up @@ -105,6 +105,7 @@ pub async fn start(config: Config) -> Result<()> {
InstallResult::NightlyIsUpdated => {
info!("Nightly up to date!");
}
InstallResult::GivenNightlyRollback => ()
}
}
Cli::Sync => {
Expand Down
21 changes: 11 additions & 10 deletions src/handlers/install_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ pub async fn start(
client: &Client,
config: &Config,
) -> Result<InstallResult> {
if version.version_type == VersionType::NightlyRollback {
return Ok(InstallResult::GivenNightlyRollback)
}

let root = directories::get_downloads_directory(config).await?;

env::set_current_dir(&root)?;
let root = root.as_path();

let is_version_installed =
helpers::version::is_version_installed(&version.tag_name, config).await?;


if is_version_installed && version.version_type != VersionType::Nightly {
return Ok(InstallResult::VersionAlreadyInstalled);
Expand Down Expand Up @@ -73,6 +78,7 @@ pub async fn start(
}
}
VersionType::Hash => handle_building_from_source(version, config).await,
VersionType::NightlyRollback => Ok(PostDownloadVersionType::None),
}?;

if let PostDownloadVersionType::Standard(downloaded_file) = downloaded_file {
Expand Down Expand Up @@ -119,8 +125,6 @@ async fn handle_rollback(config: &Config) -> Result<()> {
fs::remove_dir_all(oldest_path).await?;
}

let id = generate_random_nightly_id();

// handle this for older installations of nightly instead of introducing breaking changes
cfg_if::cfg_if! {
if #[cfg(unix)] {
Expand All @@ -139,11 +143,13 @@ async fn handle_rollback(config: &Config) -> Result<()> {
}
}

let nightly_file = fs::read_to_string("nightly/bob.json").await?;
let mut json_struct: UpstreamVersion = serde_json::from_str(&nightly_file)?;
let id: String = json_struct.target_commitish.as_ref().unwrap().chars().take(7).collect();

info!("Creating rollback: nightly-{id}");
filesystem::copy_dir("nightly", format!("nightly-{id}")).await?;

let nightly_file = fs::read_to_string("nightly/bob.json").await?;
let mut json_struct: UpstreamVersion = serde_json::from_str(&nightly_file)?;
json_struct.tag_name += &format!("-{id}");

let json_file = serde_json::to_string(&json_struct)?;
Expand All @@ -152,12 +158,6 @@ async fn handle_rollback(config: &Config) -> Result<()> {
Ok(())
}

fn generate_random_nightly_id() -> String {
use rand::distributions::{Alphanumeric, DistString};

Alphanumeric.sample_string(&mut rand::thread_rng(), 8)
}

async fn print_commits(
client: &Client,
local: &UpstreamVersion,
Expand Down Expand Up @@ -235,6 +235,7 @@ async fn download_version(
}
}
VersionType::Hash => handle_building_from_source(version, config).await,
VersionType::NightlyRollback => Ok(PostDownloadVersionType::None),
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ pub enum InstallResult {
InstallationSuccess(String),
VersionAlreadyInstalled,
NightlyIsUpdated,
GivenNightlyRollback
}

#[derive(PartialEq)]
pub enum PostDownloadVersionType {
None,
Standard(LocalVersion),
Hash,
}
5 changes: 4 additions & 1 deletion src/handlers/uninstall_handler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use anyhow::{anyhow, Result};
use reqwest::Client;
use tokio::fs;
Expand All @@ -19,7 +20,9 @@ pub async fn start(version: &str, config: Config) -> Result<()> {
Err(error) => return Err(anyhow!(error)),
};

fs::remove_dir_all(&format!("{}/{}", downloads_dir.display(), version.tag_name)).await?;
let path = downloads_dir.join(&version.tag_name);

fs::remove_dir_all(path).await?;
info!("Successfully uninstalled version: {}", version.tag_name);
Ok(())
}
6 changes: 3 additions & 3 deletions src/handlers/update_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub async fn start(data: Update, client: &Client, config: Config) -> Result<()>
if is_version_installed(&stable.tag_name, &config).await? {
match install_handler::start(&mut stable, client, &config).await? {
InstallResult::VersionAlreadyInstalled => info!("Stable is already updated!"),
InstallResult::InstallationSuccess(_) | InstallResult::NightlyIsUpdated => (),
InstallResult::InstallationSuccess(_) | InstallResult::NightlyIsUpdated | InstallResult::GivenNightlyRollback => (),
}
did_update = true;
}
Expand All @@ -23,7 +23,7 @@ pub async fn start(data: Update, client: &Client, config: Config) -> Result<()>
let mut nightly = crate::version::parse_version_type(client, "nightly").await?;
match install_handler::start(&mut nightly, client, &config).await? {
InstallResult::NightlyIsUpdated => info!("Nightly is already updated!"),
InstallResult::InstallationSuccess(_) | InstallResult::VersionAlreadyInstalled => ()
InstallResult::InstallationSuccess(_) | InstallResult::VersionAlreadyInstalled | InstallResult::GivenNightlyRollback => ()
}

did_update = true;
Expand All @@ -45,7 +45,7 @@ pub async fn start(data: Update, client: &Client, config: Config) -> Result<()>
match install_handler::start(&mut version, client, &config).await? {
InstallResult::NightlyIsUpdated => info!("Nightly is already updated!"),
InstallResult::VersionAlreadyInstalled => info!("Stable is already updated!"),
InstallResult::InstallationSuccess(_) => (),
InstallResult::InstallationSuccess(_) | InstallResult::GivenNightlyRollback => (),
}
Ok(())
}
18 changes: 16 additions & 2 deletions src/helpers/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,27 @@ pub async fn parse_version_type(client: &Client, version: &str) -> Result<Parsed
non_parsed_string: version.to_string(),
});
} else if hash_regex.is_match(version) {

return Ok(ParsedVersion {
tag_name: version.to_string().chars().take(7).collect(),
version_type: VersionType::Hash,
non_parsed_string: version.to_string(),
});
}

let alphanumeric_regex = Regex::new(r"^[a-zA-Z0-9]{8}$")?;
let separated_version: Vec<&str> = version.split('-').collect();

if separated_version[0] == "nightly"
&& (hash_regex.is_match(separated_version[1])
|| alphanumeric_regex.is_match(separated_version[1]))
{
return Ok(ParsedVersion {
tag_name: version.to_string(),
version_type: VersionType::NightlyRollback,
non_parsed_string: version.to_string(),
});
}

Err(anyhow!("Please provide a proper version string"))
}
}
Expand Down Expand Up @@ -78,7 +92,7 @@ pub async fn is_version_installed(version: &str, config: &Config) -> Result<bool

while let Some(directory) = dir.next_entry().await? {
let name = directory.file_name().to_str().unwrap().to_owned();
if !version.contains(&name) {
if !version.eq(&name) {
continue;
} else {
return Ok(true);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/version/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum VersionType {
Latest,
Nightly,
Hash,
NightlyRollback
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -30,7 +31,7 @@ pub struct LocalNightly {
pub path: PathBuf,
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub struct LocalVersion {
pub file_name: String,
pub file_format: String,
Expand Down
Loading