health command works now

This commit is contained in:
2023-03-06 17:33:10 +01:00
parent 791ea08462
commit 98607cc5e2

View File

@@ -1,19 +1,31 @@
use std::{collections::HashMap, env, fs::File, io::Write, process}; use std::{collections::HashMap, process, io::ErrorKind};
use crate::app::fs_util; use colored::Colorize;
use super::traits::Command; use super::traits::Command;
use bytes::Bytes;
use colored::Colorize;
pub struct Health; pub struct Health;
const REVEAL_VERSION: &str ="v5.0.0-rc.1"; const INFO_ASCIIDOC: &str = "
const REVEAL_PATH: &str = "~/.docki/asciidoctor-reveal"; Install the binary with your package manager!
fn reveal_url(os: String) -> String { sudo apt install asciidoctor
return format!("https://github.com/asciidoctor/asciidoctor-reveal.js/releases/download/{}/asciidoctor-revealjs-{}", REVEAL_VERSION, os) brew install asciidoctor
} gem install asciidoctor
sudo pacman -Syu asciidoctor
yay -Syu asciidoctor
";
const INFO_REVEAL: &str = "
There are two options to install it:
Option 1:
- run `docki reveal`
Option 2:
- Install the binary from Github https://github.com/asciidoctor/asciidoctor-reveal.js/releases
- Move the downloaded binary in a folder included in the path
";
impl Command for Health { impl Command for Health {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> { fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
@@ -28,86 +40,51 @@ impl Command for Health {
impl Health { impl Health {
fn health() { fn health() {
println!("checking required softwar ... \n"); Self::check_asciidoc();
let asciidoctor_installed = Self::asciidoctor_is_installed(); Self::check_reveal();
let asciidoctor_reveal_installed = Self::asciidoctor_revealjs_is_installed(); }
if asciidoctor_installed { fn check_reveal() -> () {
println!("- ✔️ {}", "asciidoctor".green()) if Self::reveal_is_installed() {
Self::print_health_ok("asciidoctor-revealjs")
} else { } else {
println!("- ❓{}", "asciidoctor \n".bright_red()); Self::print_health_not_ok("asciidoctor-revealjs", INFO_REVEAL)
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() { fn reveal_is_installed() -> bool {
print!("Do you want to install it ? (y/n)"); return Self::check_command("asciidoctor-revealjs")
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() { fn check_asciidoc() -> () {
println!("you may want to install it with your package manager"); if Self::asciidoc_is_installed() {
println!(""); Self::print_health_ok("asciidoctor")
println!("{}", "sudo apt install asciidoctor".yellow()); } else {
println!("{}", "brew install asciidoctor".yellow()); Self::print_health_not_ok("asciidoctor", INFO_ASCIIDOC)
println!("{}", "sudo pacman -Syu asciidoctor".yellow()); }
println!("{}", "yay -Syu asciidoctor".yellow());
println!("{}", "dnf install asciidoctor".yellow());
} }
fn asciidoctor_is_installed() -> bool { fn asciidoc_is_installed() -> bool {
return process::Command::new("asciidoctor") return Self::check_command("asciidoctor")
.output()
.is_ok()
} }
fn asciidoctor_revealjs_is_installed() -> bool { fn check_command(command: &str) -> bool {
return process::Command::new("asciidoctor-revealjs") return match process::Command::new(command).output() {
.output() Ok(_) => true,
.is_ok() Err(e) => ErrorKind::NotFound != e.kind()
}
} }
fn donwload(url: &str) -> Result<Bytes, ()> { fn print_health_ok(name: &str) {
let Ok(response) = reqwest::blocking::get(url) else { println!("- ✔️ {}", name.bright_green());
return Err(());
};
let Ok(data) = response.bytes() else {
return Err(());
};
return Ok(data)
} }
fn save_to(path: &str, data: Bytes) -> () { fn print_health_not_ok(name: &str, info: &str) {
let segments: &Vec<&str> = &path.split("/").collect(); println!("- ❗{}", name.bright_red());
let parent_dir = &segments[0..segments.len() - 1].join("/"); println!("{}", info.bright_black())
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)] #[cfg(test)]