diff --git a/Cargo.toml b/Cargo.toml index ca500dd..3d3e189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sandbox" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["WillKirkmanM "] diff --git a/src/args.rs b/src/args.rs index 0bb12d9..b6b2df9 100644 --- a/src/args.rs +++ b/src/args.rs @@ -4,9 +4,13 @@ use clap::Parser; #[clap(author, version, about)] pub struct SandboxArgs { /// Create a New Environment - #[clap(short = 'n', long = "new", default_value = "")] + #[clap(short, long, default_value = "")] pub new: String, + /// Create a New Environment + #[clap(short = 'U', long, default_value = "")] + pub uninstall: String, + /// Search for Environment #[clap(short = 'S', long, default_value = "")] pub search: String, diff --git a/src/download.rs b/src/download.rs index cb702d6..0bb081f 100644 --- a/src/download.rs +++ b/src/download.rs @@ -17,7 +17,7 @@ use colored::Colorize; use crate::get_path; -pub async fn download_environment(id: String) -> Result<(), Box> { +pub async fn install(id: String) -> Result<(), Box> { let base_path = match env::consts::OS { "windows" => { let appdata = std::env::var("appdata").unwrap(); diff --git a/src/install.rs b/src/install.rs index 7a57053..734c946 100644 --- a/src/install.rs +++ b/src/install.rs @@ -1,31 +1,18 @@ -use std::path::Path; - use colored::Colorize; -use crate::{get_path, id_is_valid, download::download_environment}; +use crate::id_is_valid; +use crate::download::install; +use crate::in_system; pub async fn install_environment(id: String) { if id_is_valid(id.clone()).await { if in_system(id.clone()).await { println!("You already have {} installed on your system!", id.bright_green()); } else { - download_environment(id.clone()).await.unwrap(); + install(id.clone()).await.unwrap(); } } else { println!("The environment ({}) does {} exist!", id.bright_green(), "not".red()) } } -pub async fn in_system(id: String) -> bool { - let path = get_path(id.clone()).await; - let environment_path = path.split("/").collect::>()[0].to_owned() + "/" + &id; - let formatted_path = format!("/usr/share/sandbox/beaches/{}", environment_path); - - let path = Path::new(&formatted_path); - - if path.exists() && path.is_dir() { - true - } else { - false - } -} diff --git a/src/lib.rs b/src/lib.rs index fa8b17b..9b060fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,14 +4,18 @@ mod args; mod environment; mod search; mod install; +mod uninstall; mod download; mod new; use std::error::Error; +use std::path::Path; +use std::env; use args::SandboxArgs; use search::search; use install::install_environment; +use uninstall::uninstall_environment; use new::create_new_environment; use clap::Parser; @@ -32,6 +36,10 @@ pub async fn run() { if !args.install.is_empty() { install_environment(args.install).await; } + + if !args.uninstall.is_empty() { + uninstall_environment(args.uninstall).await; + } } pub async fn get_templates_mapping() -> Result> { @@ -47,7 +55,9 @@ pub async fn get_templates_mapping() -> Result> { } } -pub async fn get_title(id: String) -> String { +pub async fn get_title(id: impl Into) -> String { + let id = id.into(); + for (_language, project_list) in get_templates_mapping().await.unwrap() { if let Some(project) = project_list.as_mapping() { if let Some(list) = project.get(&id) { @@ -59,7 +69,9 @@ pub async fn get_title(id: String) -> String { String::new() } -pub async fn get_description(id: String) -> String { +pub async fn get_description(id: impl Into) -> String { + let id = id.into(); + for (_language, project_list) in get_templates_mapping().await.unwrap() { if let Some(project) = project_list.as_mapping() { if let Some(list) = project.get(&id) { @@ -71,7 +83,9 @@ pub async fn get_description(id: String) -> String { String::new() } -pub async fn get_path(id: String) -> String { +pub async fn get_path(id: impl Into) -> String { + let id = id.into(); + for (_language, project_list) in get_templates_mapping().await.unwrap() { if let Some(project) = project_list.as_mapping() { if let Some(list) = project.get(&id) { @@ -83,7 +97,9 @@ pub async fn get_path(id: String) -> String { String::new() } -pub async fn get_keywords(id: String) -> String { +pub async fn get_keywords(id: impl Into) -> String { + let id = id.into(); + for (_language, project_list) in get_templates_mapping().await.unwrap() { if let Some(project) = project_list.as_mapping() { if let Some(list) = project.get(&id) { @@ -95,7 +111,9 @@ pub async fn get_keywords(id: String) -> String { String::new() } -pub async fn get_project_object(id: String) -> Value { +pub async fn get_project_object(id: impl Into) -> Value { + let id = id.into(); + for (_language, project_list) in get_templates_mapping().await.unwrap() { if let Some(project) = project_list.as_mapping() { if let Some(list) = project.get(&id) { @@ -106,7 +124,9 @@ pub async fn get_project_object(id: String) -> Value { Value::Null } -pub async fn id_is_valid(id: String) -> bool{ +pub async fn id_is_valid(id: impl Into) -> bool{ + let id = id.into(); + for (_language, project_list) in get_templates_mapping().await.unwrap() { if let Some(project) = project_list.as_mapping() { if let Some(_) = project.get(&id) { @@ -116,3 +136,28 @@ pub async fn id_is_valid(id: String) -> bool{ } false } + +pub async fn in_system(id: impl Into) -> bool { + let id = id.into(); + + let base_path = match env::consts::OS { + "windows" => { + let appdata = std::env::var("appdata").unwrap(); + let beaches_path = format!("{}/sandbox/beaches/", appdata); + beaches_path + } + _ => "/usr/share/sandbox/beaches/".to_string(), + }; + + let path = get_path(id.clone()).await; + let environment_path = path.split("/").collect::>()[0].to_owned() + "/" + &id; + let formatted_path = format!("{}{}", base_path, environment_path); + + let path = Path::new(&formatted_path); + + if path.exists() && path.is_dir() { + true + } else { + false + } +} diff --git a/src/new.rs b/src/new.rs index a10e477..3b8962f 100644 --- a/src/new.rs +++ b/src/new.rs @@ -1,6 +1,6 @@ use crate::id_is_valid; use crate::environment::open_environment; -use crate::install::in_system; +use crate::in_system; use colored::Colorize; @@ -12,6 +12,6 @@ pub async fn create_new_environment(environment: String) { println!("You do {} have {} installed! Install it with:\nsandbox --install {}", "not".red(), environment.bright_green(), environment.bright_green()) } } else { - println!("The environment {} does {} exist... You can search for an environment with\nsandbox --search {}", environment.bright_green(), "not".red(), environment.bright_green()) + println!("The environment ({}) does {} exist! You can search for an environment with\nsandbox --search {}", environment.bright_green(), "not".red(), environment.bright_green()); } } diff --git a/src/search.rs b/src/search.rs index ac0a529..3871af6 100644 --- a/src/search.rs +++ b/src/search.rs @@ -1,4 +1,4 @@ -use crate::install::in_system; +use crate::in_system; use crate::get_templates_mapping; use colored::Colorize; diff --git a/src/uninstall.rs b/src/uninstall.rs new file mode 100644 index 0000000..a0ca21f --- /dev/null +++ b/src/uninstall.rs @@ -0,0 +1,70 @@ +use std::env; +use std::io::{stdin, stdout, Write}; + +use crate::{get_path, id_is_valid, in_system}; + +use colored::Colorize; +use tokio::fs; + +pub async fn uninstall_environment(environment: impl Into) { + let environment = environment.into(); + + if id_is_valid(&environment).await { + if in_system(&environment).await { + uninstall(environment).await + } else { + println!( + "You do {} have {} installed! Install it with:\nsandbox --install {}", + "not".red(), + environment.bright_green(), + environment.bright_green() + ) + } + } else { + println!("The environment ({}) does {} exist! You can search for an environment with\nsandbox --search {}", environment.bright_green(), "not".red(), environment.bright_green()); + } +} + +pub async fn uninstall(environment: impl Into) { + let environment = environment.into(); + + let base_path = match env::consts::OS { + "windows" => { + let appdata = std::env::var("appdata").unwrap(); + let beaches_path = format!("{}/sandbox/beaches/", appdata); + beaches_path + } + _ => "/usr/share/sandbox/beaches/".to_string(), + }; + + let path = get_path(environment.clone()).await; + let environment_path = path.split("/").collect::>()[0].to_owned() + "/" + &environment; + + let formatted_path = format!("{}{}", base_path, environment_path); + + let mut answer = String::new(); + + print!( + "Are you sure you want to delete {}? [{}/{}] ", + &environment.blue(), + "Y".bright_green(), + "n".red() + ); + + stdout().lock().flush().unwrap(); + + stdin().read_line(&mut answer).unwrap(); + + answer = answer.trim().into(); + + if answer == "Y" { + match fs::remove_dir_all(formatted_path).await { + Ok(_) => { + println!("Successfully Deleted {} from System!", environment.bright_green()) + }, + Err(err) => { + println!("There was an {} Deleting {}!\n{}", "Erorr".red(), environment.blue(), err) + } + } + } +}