feat(cli): Added a spinner to the CLI for long running commands like fetching releases
This commit is contained in:
+136
-1
@@ -2,13 +2,25 @@ use std::fs::{self, File};
|
||||
use std::io::{BufRead, BufReader, Seek, SeekFrom};
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use anyhow::Result;
|
||||
use colored::Colorize;
|
||||
use log::LevelFilter;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use log::{error, LevelFilter};
|
||||
use log4rs::append::file::FileAppender;
|
||||
use log4rs::config::{Appender, Root};
|
||||
use log4rs::encode::pattern::PatternEncoder;
|
||||
use regex::Regex;
|
||||
use reqwest::{Certificate, Client};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::app::{log_and_print_error, App, AppConfig};
|
||||
use crate::cli::{self, Command};
|
||||
use crate::network::Network;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "utils_tests.rs"]
|
||||
@@ -122,3 +134,126 @@ fn colorize_log_line(line: &str, re: &Regex) -> String {
|
||||
line.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn load_config(path: &str) -> Result<AppConfig> {
|
||||
let file = File::open(path).map_err(|e| anyhow!(e))?;
|
||||
let reader = BufReader::new(file);
|
||||
let config = serde_yaml::from_reader(reader)?;
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub(super) fn build_network_client(config: &AppConfig) -> Client {
|
||||
let mut client_builder = Client::builder()
|
||||
.pool_max_idle_per_host(10)
|
||||
.http2_keep_alive_interval(Duration::from_secs(5))
|
||||
.tcp_keepalive(Duration::from_secs(5));
|
||||
|
||||
if let Some(radarr_config) = &config.radarr {
|
||||
if let Some(ref cert_path) = &radarr_config.ssl_cert_path {
|
||||
let cert = create_cert(cert_path, "Radarr");
|
||||
client_builder = client_builder.add_root_certificate(cert);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(sonarr_config) = &config.sonarr {
|
||||
if let Some(ref cert_path) = &sonarr_config.ssl_cert_path {
|
||||
let cert = create_cert(cert_path, "Sonarr");
|
||||
client_builder = client_builder.add_root_certificate(cert);
|
||||
}
|
||||
}
|
||||
|
||||
match client_builder.build() {
|
||||
Ok(client) => client,
|
||||
Err(e) => {
|
||||
error!("Unable to create reqwest client: {}", e);
|
||||
eprintln!("error: {}", e.to_string().red());
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn create_cert(cert_path: &String, servarr_name: &str) -> Certificate {
|
||||
match fs::read(cert_path) {
|
||||
Ok(cert) => match Certificate::from_pem(&cert) {
|
||||
Ok(certificate) => certificate,
|
||||
Err(_) => {
|
||||
log_and_print_error(format!(
|
||||
"Unable to read the specified {} SSL certificate",
|
||||
servarr_name
|
||||
));
|
||||
process::exit(1);
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
log_and_print_error(format!(
|
||||
"Unable to open specified {} SSL certificate",
|
||||
servarr_name
|
||||
));
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn render_spinner() -> ProgressBar {
|
||||
let pb = ProgressBar::new_spinner();
|
||||
pb.enable_steady_tick(Duration::from_millis(60));
|
||||
pb.set_style(
|
||||
ProgressStyle::with_template("{spinner:.blue}")
|
||||
.unwrap()
|
||||
.tick_strings(&[
|
||||
"⢀⠀", "⡀⠀", "⠄⠀", "⢂⠀", "⡂⠀", "⠅⠀", "⢃⠀", "⡃⠀", "⠍⠀", "⢋⠀", "⡋⠀", "⠍⠁", "⢋⠁", "⡋⠁", "⠍⠉",
|
||||
"⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⢈⠩", "⡀⢙", "⠄⡙", "⢂⠩", "⡂⢘", "⠅⡘", "⢃⠨", "⡃⢐",
|
||||
"⠍⡐", "⢋⠠", "⡋⢀", "⠍⡁", "⢋⠁", "⡋⠁", "⠍⠉", "⠋⠉", "⠋⠉", "⠉⠙", "⠉⠙", "⠉⠩", "⠈⢙", "⠈⡙", "⠈⠩",
|
||||
"⠀⢙", "⠀⡙", "⠀⠩", "⠀⢘", "⠀⡘", "⠀⠨", "⠀⢐", "⠀⡐", "⠀⠠", "⠀⢀", "⠀⡀",
|
||||
]),
|
||||
);
|
||||
pb.set_message("Querying...");
|
||||
pb
|
||||
}
|
||||
|
||||
pub(super) async fn start_cli_with_spinner(
|
||||
config: AppConfig,
|
||||
reqwest_client: Client,
|
||||
cancellation_token: CancellationToken,
|
||||
app: Arc<Mutex<App<'_>>>,
|
||||
command: Command,
|
||||
) {
|
||||
config.verify_config_present_for_cli(&command);
|
||||
app.lock().await.cli_mode = true;
|
||||
let pb = render_spinner();
|
||||
let app_nw = Arc::clone(&app);
|
||||
let mut network = Network::new(&app_nw, cancellation_token, reqwest_client);
|
||||
match cli::handle_command(&app, command, &mut network).await {
|
||||
Ok(output) => {
|
||||
pb.finish();
|
||||
println!("{}", output);
|
||||
}
|
||||
Err(e) => {
|
||||
pb.finish();
|
||||
eprintln!("error: {}", e.to_string().red());
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn start_cli_no_spinner(
|
||||
config: AppConfig,
|
||||
reqwest_client: Client,
|
||||
cancellation_token: CancellationToken,
|
||||
app: Arc<Mutex<App<'_>>>,
|
||||
command: Command,
|
||||
) {
|
||||
config.verify_config_present_for_cli(&command);
|
||||
app.lock().await.cli_mode = true;
|
||||
let app_nw = Arc::clone(&app);
|
||||
let mut network = Network::new(&app_nw, cancellation_token, reqwest_client);
|
||||
match cli::handle_command(&app, command, &mut network).await {
|
||||
Ok(output) => {
|
||||
println!("{}", output);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("error: {}", e.to_string().red());
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user