renamed command setup to health

This commit is contained in:
2023-03-06 16:55:19 +01:00
parent 48f0a44eaa
commit 791ea08462
7 changed files with 194 additions and 59 deletions

41
Cargo.lock generated
View File

@@ -17,6 +17,17 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@@ -86,6 +97,17 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]] [[package]]
name = "core-foundation" name = "core-foundation"
version = "0.9.3" version = "0.9.3"
@@ -116,8 +138,10 @@ name = "docki"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"bytes", "bytes",
"colored",
"regex", "regex",
"reqwest", "reqwest",
"text_io",
"zip-extract", "zip-extract",
] ]
@@ -252,6 +276,15 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
version = "0.2.6" version = "0.2.6"
@@ -460,7 +493,7 @@ version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b" checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi 0.2.6",
"libc", "libc",
] ]
@@ -740,6 +773,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "text_io"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f0c8eb2ad70c12a6a69508f499b3051c924f4b1cfeae85bfad96e6bc5bba46"
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.38" version = "1.0.38"

View File

@@ -9,6 +9,8 @@ license-file = "LICENSE.txt"
[dependencies] [dependencies]
bytes = "1.4.0" bytes = "1.4.0"
colored = "2.0.0"
regex = "1.7.1" regex = "1.7.1"
reqwest = { version = "0.11.14", features = ["blocking"] } reqwest = { version = "0.11.14", features = ["blocking"] }
text_io = "0.1.12"
zip-extract = "0.1.1" zip-extract = "0.1.1"

123
src/app/commands/health.rs Normal file
View File

@@ -0,0 +1,123 @@
use std::{collections::HashMap, env, fs::File, io::Write, process};
use crate::app::fs_util;
use super::traits::Command;
use bytes::Bytes;
use colored::Colorize;
pub struct Health;
const REVEAL_VERSION: &str ="v5.0.0-rc.1";
const REVEAL_PATH: &str = "~/.docki/asciidoctor-reveal";
fn reveal_url(os: String) -> String {
return format!("https://github.com/asciidoctor/asciidoctor-reveal.js/releases/download/{}/asciidoctor-revealjs-{}", REVEAL_VERSION, os)
}
impl Command for Health {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
Self::health();
return Ok(())
}
fn new() -> Self where Self: Sized {
return Self {}
}
}
impl Health {
fn health() {
println!("checking required softwar ... \n");
let asciidoctor_installed = Self::asciidoctor_is_installed();
let asciidoctor_reveal_installed = Self::asciidoctor_revealjs_is_installed();
if asciidoctor_installed {
println!("- ✔️ {}", "asciidoctor".green())
} else {
println!("- ❓{}", "asciidoctor \n".bright_red());
Self::print_asciidoctor_install_help();
println!("");
}
if asciidoctor_reveal_installed {
println!("- ✔️ {}", "asciidoctor-revealjs".green())
} else {
println!("- ❓{}", "asciidoctor-revealjs \n".bright_red());
Self::ask_to_install_reveal()
}
}
fn ask_to_install_reveal() {
print!("Do you want to install it ? (y/n)");
let user_input: String = text_io::read!("{}\n");
if user_input.to_lowercase() == "y" {
let os = env::consts::OS;
let url = reveal_url(os.to_string());
println!("installing");
let data = Self::donwload(&url).expect("failed installing");
Self::save_to("~/.docki/asciidoctor-revealjs", data);
} else if user_input.to_lowercase() == "y"{
println!("not installing")
} else {
println!("not a valid option (not installing)")
}
}
fn print_asciidoctor_install_help() {
println!("you may want to install it with your package manager");
println!("");
println!("{}", "sudo apt install asciidoctor".yellow());
println!("{}", "brew install asciidoctor".yellow());
println!("{}", "sudo pacman -Syu asciidoctor".yellow());
println!("{}", "yay -Syu asciidoctor".yellow());
println!("{}", "dnf install asciidoctor".yellow());
}
fn asciidoctor_is_installed() -> bool {
return process::Command::new("asciidoctor")
.output()
.is_ok()
}
fn asciidoctor_revealjs_is_installed() -> bool {
return process::Command::new("asciidoctor-revealjs")
.output()
.is_ok()
}
fn donwload(url: &str) -> Result<Bytes, ()> {
let Ok(response) = reqwest::blocking::get(url) else {
return Err(());
};
let Ok(data) = response.bytes() else {
return Err(());
};
return Ok(data)
}
fn save_to(path: &str, data: Bytes) -> () {
let segments: &Vec<&str> = &path.split("/").collect();
let parent_dir = &segments[0..segments.len() - 1].join("/");
fs_util::create_dir_recursive(parent_dir);
let mut file = File::create(path).expect("failed to create file");
file.write_all(&data).expect("failed saving file")
}
}
#[cfg(test)]
mod test {
use std::env;
#[test]
fn download() {
println!("{}", env::consts::OS);
assert!(false)
}
}

View File

@@ -2,12 +2,12 @@ use std::collections::HashMap;
use traits::Command; use traits::Command;
use self::{build::Build, setup::Setup}; use self::{build::Build, health::Health};
pub mod traits; pub mod traits;
pub mod executions; pub mod executions;
mod build; mod build;
mod setup; mod health;
pub struct CommandRegistry { pub struct CommandRegistry {
commands: HashMap<String, Box<dyn Command>> commands: HashMap<String, Box<dyn Command>>
@@ -18,7 +18,7 @@ impl CommandRegistry {
pub fn register_all(&mut self) { pub fn register_all(&mut self) {
let registry = self; let registry = self;
registry.register("/build".to_string(), Box::new(Build::new()), true); registry.register("/build".to_string(), Box::new(Build::new()), true);
registry.register("/setup".to_string(), Box::new(Setup::new()), true); registry.register("/health".to_string(), Box::new(Health::new()), true);
} }

View File

@@ -1,54 +0,0 @@
use std::{collections::HashMap, env, fs::File, io::Write};
use super::traits::Command;
use bytes::Bytes;
pub struct Setup;
impl Command for Setup {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
Self::setup();
return Ok(())
}
fn new() -> Self where Self: Sized {
return Self {}
}
}
impl Setup {
fn setup() {
let os = env::consts::OS;
let reveal_bin_url = format!("https://github.com/asciidoctor/asciidoctor-reveal.js/releases/download/v5.0.0-rc.1/asciidoctor-revealjs-{os}");
let reveal_bin = Self::donwload(&reveal_bin_url).expect("could not download asciidoctor binary");
let mut reveal_file = File::create("/usr/local/bin/asciidoctor").expect("could not save asciidoctor binary");
reveal_file.write_all(&reveal_bin).expect("could not save asciidoctor binary");
}
fn donwload(url: &str) -> Result<Bytes, ()> {
let Ok(response) = reqwest::blocking::get(url) else {
return Err(());
};
let Ok(data) = response.bytes() else {
return Err(());
};
return Ok(data)
}
}
#[cfg(test)]
mod test {
use std::env;
#[test]
fn download() {
println!("{}", env::consts::OS);
assert!(false)
}
}

View File

@@ -1,4 +1,4 @@
use std::fs; use std::{fs, path::Path, env};
struct RecursivePathFetch { struct RecursivePathFetch {
paths: Vec<String>, paths: Vec<String>,
@@ -58,3 +58,26 @@ pub fn fetch_paths_recursive(path: &str) -> Result<Vec<String>, String> {
return path_fetch.fetch(); return path_fetch.fetch();
} }
pub fn create_dir_recursive(path: &str) {
let mut validated_path = "".to_string();
for segment in path.split("/") {
validated_path.push_str(format!("{segment}/").as_str());
if !directory_exists(&validated_path) {
fs::create_dir(&validated_path).unwrap()
}
}
}
pub fn directory_exists(path: &String) -> bool {
Path::new(path).is_dir()
}
pub fn expand_path(path: String) -> String {
let home_dir = env::var("HOME")
.expect("could not find home dir");
return path.replace("~", &home_dir)
}

View File

@@ -1,3 +1,5 @@
use std::{fs, path::Path};
use crate::app::fs_util; use crate::app::fs_util;
#[test] #[test]