Skip to content

Commit

Permalink
Feat(lib.rs): Support for Multiple Languages (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillKirkmanM committed Apr 27, 2023
2 parents db71b79 + fefec00 commit 48d527f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct SandboxArgs {
pub beach_type: String,

/// Search for Environment
#[clap(short, long, default_value = "")]
#[clap(short = 'S', long, default_value = "")]
pub search: String,

/// Search for Environment
Expand Down
9 changes: 5 additions & 4 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ use crate::{get_path, id_is_valid, download::download_environment};

pub async fn install_environment(id: String) {
if id_is_valid(id.clone()) {
let environment_path = get_path(id.clone()).split("/").collect::<Vec<&str>>()[0].to_owned() + "/" + &id;
if in_system(environment_path) {
if in_system(id.clone()) {
println!("You already have {} installed on your system!", id);
exit(0)
} else {
Expand All @@ -17,8 +16,10 @@ pub async fn install_environment(id: String) {
}
}

pub fn in_system(path: String) -> bool {
let formatted_path = format!("/usr/share/sandbox/beaches/{}", path);
pub fn in_system(id: String) -> bool {
let environment_path = get_path(id.clone());
let some = environment_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);

Expand Down
26 changes: 16 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ mod search;
mod install;
mod download;

use std::fs;

use args::SandboxArgs;
use clap::Parser;
use search::search;
use serde_yaml::{Value, Mapping};

use environment::setup_environment;
use install::install_environment;

use clap::Parser;
use serde_yaml::{Value, Mapping};

pub async fn run() {
let a = get_path("go-min".to_string());
let args = SandboxArgs::parse();

if !args.search.is_empty() {
Expand All @@ -31,16 +34,19 @@ pub async fn run() {
}

pub fn get_projects_list() -> Mapping {
for (_, projects) in get_templates_mapping() {
return projects.as_mapping().unwrap().to_owned()
}
Mapping::new()
}

pub fn get_templates_mapping() -> Mapping {
let sandbox_templates_path = "../sandbox-templates/sandbox-templates.yml";
let file_contents = std::fs::read_to_string(sandbox_templates_path).unwrap();
let file_contents = fs::read_to_string(sandbox_templates_path).unwrap();
let templates: Value = serde_yaml::from_str(&file_contents).unwrap();

if let Some(languages) = templates["languages"].as_mapping() {
for (_language_name, projects) in languages {
if let Some(projects_list) = projects.as_mapping() {
return projects_list.to_owned()
}
}
return languages.to_owned()
}
Mapping::new()
}
Expand All @@ -50,7 +56,7 @@ pub fn get_title(id: String) -> String {
if project_id.as_str().unwrap() == id {
return project_object["title"].as_str().unwrap().to_string();
}
}
}
String::new()
}

Expand Down
47 changes: 18 additions & 29 deletions src/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::fs;

use serde_yaml::Value;
use crate::{get_projects_list, install::in_system, get_templates_mapping};

pub fn search(query: String) {
println!("Environments That Match Query {}\n", query);
Expand All @@ -12,32 +10,23 @@ pub fn search(query: String) {

pub fn search_environment(query: String) {
let _sandbox_url = "https://raw.githubusercontent.com/the-sandbox-project/sandbox-templates/master/sandbox-templates.yml";
let sandbox_templates_path = "../sandbox-templates/sandbox-templates.yml";

let file_contents = std::fs::read_to_string(sandbox_templates_path).unwrap();
let templates: Value = serde_yaml::from_str(&file_contents).unwrap();

if let Some(languages) = templates["languages"].as_mapping() {
for (_language_name, projects) in languages {
if let Some(projects_list) = projects.as_mapping() {
for (project_id, project) in projects_list {
let keywords = project["keywords"].as_str().unwrap_or("");
let keywords_list: Vec<&str> = keywords.split_whitespace().collect();

for keyword in keywords_list {
if keyword == query {
let title = project["title"].as_str().unwrap_or("Title not Found");
let description = project["description"].as_str().unwrap_or("Description not Found");
let path = format!("/usr/share/sandbox/beaches/{}", project["path"].as_str().unwrap_or("Path not Found"));

let installed = match fs::read(path) {
Ok(_) => "✅",
Err(_) => "❌"
};

println!(" {} {} ({}) - {}", installed, title, project_id.as_str().unwrap(), description)
}
}
for (_language, project) in get_templates_mapping() {
for (project_id, project_object) in project.as_mapping().unwrap() {
let keywords = project_object["keywords"].as_str().unwrap_or("");
let keywords_list: Vec<&str> = keywords.split_whitespace().collect();

for keyword in keywords_list {
if keyword == query {
let title = project_object["title"].as_str().unwrap_or("Title not Found");
let description = project_object["description"].as_str().unwrap_or("Description not Found");
let id = project_id.as_str().unwrap().to_string();

let installed = match in_system(id.clone()) {
true => "✅",
false => "❌"
};

println!(" {} {} ({}) - {}", installed, title, id, description)
}
}
}
Expand Down

0 comments on commit 48d527f

Please sign in to comment.