add uninstall support

This commit is contained in:
abbie 2023-08-10 11:00:13 +01:00
parent c68c0529c7
commit 181dc07fc8
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C
3 changed files with 58 additions and 3 deletions

2
Cargo.lock generated
View file

@ -197,7 +197,7 @@ checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "curze"
version = "0.1.1"
version = "0.2.0"
dependencies = [
"clap",
"home",

View file

@ -1,6 +1,6 @@
[package]
name = "curze"
version = "0.1.1"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -19,6 +19,11 @@ enum Commands {
/// Packages to install.
name: Vec<String>,
},
/// Uninstalls a list of packages.
Uninstall {
/// Packages to uninstall.
name: Vec<String>,
},
}
fn main() {
@ -34,6 +39,15 @@ fn main() {
}
}
}
Some(Commands::Uninstall { name }) => {
if name.is_empty() {
println!("EROR: No packages named for uninstall.");
} else {
for package in name {
uninstall_package(package.to_string());
}
}
}
None => {println!("NOTE: Run curze help for subcommands and options.")}
}
}
@ -81,7 +95,7 @@ fn install_package(package: String) {
.current_dir(format!("{}/.local/share/curze/cache/{}", home, realised_name))
.output();
if result_of_install.is_ok() {
fs::copy(format!("{}/.local/share/curze/cache/{}/.curze/version", home, realised_name), format!("{}/.local/share/curze/{}.version", home, realised_name));
let _ = fs::copy(format!("{}/.local/share/curze/cache/{}/.curze/version", home, realised_name), format!("{}/.local/share/curze/{}.version", home, realised_name));
println!("NOTE: {} successfully installed!", package);
} else {
println!("EROR: Installation failed!");
@ -94,6 +108,47 @@ fn install_package(package: String) {
}
}
fn uninstall_package(package: String) {
println!("NOTE: Preparing to uninstall {}.", package);
let realised_name = package.clone().replace("/",".");
let home_out = fetch_home_dir();
let home;
if home_out.is_some() {
home = home_out.unwrap().clone();
} else {
println!("EROR: Your home directory could not be found. Installation halted.");
return;
}
if Path::new(&format!("{}/.local/share/curze/{}.version", home, realised_name)).exists() {
if Path::new(&format!("{}/.local/share/curze/cache/{}/", home, realised_name)).exists() { }
else {
println!("NOTE: Local copy of package doesn't exist. Pulling now.");
let cache_result = handle_package_cache(package.clone());
if cache_result {}
else {
println!("EROR: Local copy of package cannot be obtained. Uninstallation halted.");
return;
}
}
println!("NOTE: Uninstalling {}.", package);
let result_of_uninstall = Command::new("sh")
.arg("-c")
.arg(".curze/uninstall.sh")
.current_dir(format!("{}/.local/share/curze/cache/{}", home, realised_name))
.output();
if result_of_uninstall.is_ok() {
let _ = fs::remove_file(format!("{}/.local/share/curze/{}.version", home, realised_name));
println!("NOTE: {} successfully uninstalled!", package);
} else {
println!("EROR: Uninstallation failed!");
}
} else {
println!("EROR: {} is not installed! Uninstallation halted!", package);
}
}
fn handle_package_cache(package: String) -> bool {
let realised_name = package.clone().replace("/",".");
let home_out = fetch_home_dir();