Skip to content

Commit

Permalink
Feat(uninstall.rs): Uninstall Command (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillKirkmanM committed May 1, 2023
2 parents fcbd297 + 3672b6e commit a8dfc78
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sandbox"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["WillKirkmanM <[email protected]>"]

Expand Down
6 changes: 5 additions & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use colored::Colorize;

use crate::get_path;

pub async fn download_environment(id: String) -> Result<(), Box<dyn Error>> {
pub async fn install(id: String) -> Result<(), Box<dyn Error>> {
let base_path = match env::consts::OS {
"windows" => {
let appdata = std::env::var("appdata").unwrap();
Expand Down
21 changes: 4 additions & 17 deletions src/install.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<&str>>()[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
}
}
57 changes: 51 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Mapping, Box<dyn Error>> {
Expand All @@ -47,7 +55,9 @@ pub async fn get_templates_mapping() -> Result<Mapping, Box<dyn Error>> {
}
}

pub async fn get_title(id: String) -> String {
pub async fn get_title(id: impl Into<String>) -> 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) {
Expand All @@ -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>) -> 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) {
Expand All @@ -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>) -> 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) {
Expand All @@ -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>) -> 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) {
Expand All @@ -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<String>) -> 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) {
Expand All @@ -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<String>) -> 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) {
Expand All @@ -116,3 +136,28 @@ pub async fn id_is_valid(id: String) -> bool{
}
false
}

pub async fn in_system(id: impl Into<String>) -> 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::<Vec<&str>>()[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
}
}
4 changes: 2 additions & 2 deletions src/new.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
}
}
2 changes: 1 addition & 1 deletion src/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::install::in_system;
use crate::in_system;
use crate::get_templates_mapping;

use colored::Colorize;
Expand Down
70 changes: 70 additions & 0 deletions src/uninstall.rs
Original file line number Diff line number Diff line change
@@ -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<String>) {
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<String>) {
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::<Vec<&str>>()[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)
}
}
}
}

0 comments on commit a8dfc78

Please sign in to comment.