Skip to content

Commit

Permalink
Feat(download.rs): Added Download Spinner (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillKirkmanM committed Apr 29, 2023
2 parents 48d527f + 7648f7e commit b2e7cf9
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 44 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
clap = { version = "4.2.4", features = ["derive"] }
dylib = "0.0.3"
flate2 = "1.0.25"
indicatif = "0.17.3"
reqwest = "0.11.16"
serde = { version = "1.0.160", features = ["derive"] }
serde_yaml = "0.9.21"
Expand Down
29 changes: 28 additions & 1 deletion src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ use std::error::Error;
use std::fs::File;
use std::io;
use std::path::Path;
use std::thread;
use std::time::Duration;
use std::{cmp::min, fmt::Write};


use reqwest::Client;
use flate2::read::GzDecoder;
use tar::Archive;
use tokio::fs;
use indicatif::{ProgressBar, ProgressState, ProgressStyle};

use crate::get_path;

Expand All @@ -18,13 +23,33 @@ pub async fn download_environment(id: String) -> Result<(), Box<dyn Error>> {
let download_path = format!("{}{}", base_path, environment_path);

let client = Client::new();
let response = client.get(&download_url).send().await?;

let response = client.get(download_url).send().await?;

if response.status().is_success() {
let language_path = Path::new(&download_path).parent().unwrap().to_str().unwrap();
fs::create_dir_all(language_path).await?;

let mut file = File::create(&download_path)?;

let mut downloaded = 0;
let total_size = response.content_length().unwrap();

let pb = ProgressBar::new(total_size);
pb.set_style(ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.unwrap()
.with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap())
.progress_chars("#>-"));

while downloaded < total_size {
let new = min(downloaded + 223211, total_size);
downloaded = new;
pb.set_position(new);
thread::sleep(Duration::from_millis(12));
}

pb.finish_with_message("downloaded");

let content = response.bytes().await?;

io::copy(&mut content.as_ref(), &mut file)?;
Expand All @@ -37,6 +62,8 @@ pub async fn download_environment(id: String) -> Result<(), Box<dyn Error>> {

archive.unpack(&unzip_path)?;
fs::remove_file(&download_path).await?;

println!("Installed {}! Run it with: sandbox --new {}", id, id)
}
Ok(())
}
Expand Down
8 changes: 3 additions & 5 deletions src/install.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::process::exit;
use std::path::Path;

use crate::{get_path, id_is_valid, download::download_environment};
Expand All @@ -7,18 +6,17 @@ pub async fn install_environment(id: String) {
if id_is_valid(id.clone()) {
if in_system(id.clone()) {
println!("You already have {} installed on your system!", id);
exit(0)
} else {
download_environment(id).await.unwrap()
download_environment(id.clone()).await.unwrap();
}
} else {
println!("The environment {} does not exist!", id)
}
}

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 path = get_path(id.clone());
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);
Expand Down
82 changes: 45 additions & 37 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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 @@ -33,13 +32,6 @@ 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 = fs::read_to_string(sandbox_templates_path).unwrap();
Expand All @@ -50,57 +42,73 @@ pub fn get_templates_mapping() -> Mapping {
}
Mapping::new()
}

pub fn get_title(id: String) -> String {
for (project_id, project_object) in get_projects_list() {
if project_id.as_str().unwrap() == id {
return project_object["title"].as_str().unwrap().to_string();
}
}
for (_language, project_list) in get_templates_mapping() {
if let Some(project) = project_list.as_mapping() {
if let Some(list) = project.get(&id) {
let path = list["title"].as_str().unwrap().to_string();
return path
};
}
}
String::new()
}

pub fn get_description(id: String) -> String {
for (project_id, project_object) in get_projects_list() {
if project_id.as_str().unwrap() == id {
return project_object["description"].as_str().unwrap().to_string();
}
for (_language, project_list) in get_templates_mapping() {
if let Some(project) = project_list.as_mapping() {
if let Some(list) = project.get(&id) {
let path = list["description"].as_str().unwrap().to_string();
return path
};
}
}
String::new()
}

pub fn get_path(id: String) -> String {
for (project_id, project_object) in get_projects_list() {
if project_id.as_str().unwrap() == id {
return project_object["path"].as_str().unwrap().to_string();
}
for (_language, project_list) in get_templates_mapping() {
if let Some(project) = project_list.as_mapping() {
if let Some(list) = project.get(&id) {
let path = list["path"].as_str().unwrap().to_string();
return path
};
}
}
String::new()
}

pub fn get_keywords(id: String) -> String {
for (project_id, project_object) in get_projects_list() {
if project_id.as_str().unwrap() == id {
return project_object["path"].as_str().unwrap().to_string();
}
}
for (_language, project_list) in get_templates_mapping() {
if let Some(project) = project_list.as_mapping() {
if let Some(list) = project.get(&id) {
let path = list["keywords"].as_str().unwrap().to_string();
return path
};
}
}
String::new()
}

pub fn get_project_object(id: String) -> Value {
for (project_id, project_object) in get_projects_list() {
if project_id.as_str().unwrap() == id {
return project_object.to_owned()
}
}
for (_language, project_list) in get_templates_mapping() {
if let Some(project) = project_list.as_mapping() {
if let Some(list) = project.get(&id) {
return list.to_owned()
};
}
}
Value::Null
}

pub fn id_is_valid(id: String) -> bool {
for (project_id, _) in get_projects_list() {
if project_id.as_str().unwrap() == id {
return true
pub fn id_is_valid(id: String) -> bool{
for (_language, project_list) in get_templates_mapping() {
if let Some(project) = project_list.as_mapping() {
if let Some(_) = project.get(&id) {
return true
};
}
}
}
false
}
3 changes: 2 additions & 1 deletion src/search.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{get_projects_list, install::in_system, get_templates_mapping};
use crate::install::in_system;
use crate::get_templates_mapping;

pub fn search(query: String) {
println!("Environments That Match Query {}\n", query);
Expand Down

0 comments on commit b2e7cf9

Please sign in to comment.