Refactored the main.rs file to have utils functions under the main, to improve readability and added a way to check and create missing folders and config files

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2021-10-26 22:14:54 +02:00
parent 09388e8a36
commit 636e77ab05
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283

View File

@ -2,7 +2,7 @@ use std::{fs::{File, create_dir_all}, io::{Read, Write}, path::{Path, PathBuf}};
use hyper_tls::HttpsConnector; use hyper_tls::HttpsConnector;
use hyper::{Body, Client, body, client::HttpConnector}; use hyper::{Body, Client, body, client::HttpConnector};
use clap::{Arg, App, SubCommand}; use clap::{App, Arg, SubCommand, crate_version};
use session::logout; use session::logout;
use cookie::Cookie; use cookie::Cookie;
use crate::{albums::get_albums, session::login}; use crate::{albums::get_albums, session::login};
@ -17,36 +17,14 @@ pub struct LycheeClient {
api_key: String api_key: String
} }
pub async fn body_to_str(res: Body) -> String {
return String::from_utf8(body::to_bytes(res).await.unwrap().to_vec()).unwrap();
}
fn get_config_folder() -> PathBuf {
let mut p = dirs::config_dir().unwrap();
p.push("Lychee_CLIent");
return p;
}
fn write_to_file(d: &str, p: &str) {
let mut o = File::create(p).expect("Cannot create file.");
o.write_all(d.as_bytes()).expect("Cannot write to file.");
}
fn read_from_file(p: &str) -> String {
if !Path::new(p).exists() { return "".to_string(); }
let mut d = String::new();
let mut ifile = File::open(p).expect("Cannot open file.");
ifile.read_to_string(&mut d).expect("Cannot read file.");
return d;
}
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
create_dir_all(get_config_folder()).expect("Cannot create folder."); setup_config_data_storage();
let mut app = App::new("Lychee CLIent") let mut app = App::new("Lychee CLIent")
.version("0.1.0") .version(crate_version!())
.author("Louis Vallat <contact@louis-vallat.xyz>") .author("Louis Vallat <contact@louis-vallat.xyz>")
.about("An Lychee CLI client written in Rust.") .about("A Lychee CLI client written in Rust.")
.subcommand( .subcommand(
SubCommand::with_name("login") SubCommand::with_name("login")
.about("Log into a Lychee instance") .about("Log into a Lychee instance")
@ -95,12 +73,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if let Some(matches) = matches.subcommand_matches("login") { if let Some(matches) = matches.subcommand_matches("login") {
let username = matches.value_of("username").unwrap(); let username = matches.value_of("username").unwrap();
let password = matches.value_of("password").unwrap(); let password = matches.value_of("password").unwrap();
let lychee_session = login(&client, username, password).await; write_to_file(login(&client, username, password).await.as_str(), session_path.to_str().unwrap());
write_to_file(lychee_session.as_str(), session_path.to_str().unwrap());
} else if let Some(_) = matches.subcommand_matches("logout") { } else if let Some(_) = matches.subcommand_matches("logout") {
logout(&client, &lychee_session_cookie).await; logout(&client, &lychee_session_cookie).await;
} else if let Some(matches) = matches.subcommand_matches("albums") { } else if let Some(matches) = matches.subcommand_matches("albums") {
if matches.is_present("get") { if matches.is_present("get") {
println!("{}", get_albums(&client, &lychee_session_cookie).await.pretty(4)); println!("{}", get_albums(&client, &lychee_session_cookie).await.pretty(4));
} else { } else {
App::print_long_help(&mut app).unwrap(); App::print_long_help(&mut app).unwrap();
@ -112,6 +89,38 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
Ok(()) Ok(())
} }
pub async fn body_to_str(res: Body) -> String {
return String::from_utf8(body::to_bytes(res).await.unwrap().to_vec()).unwrap();
}
fn setup_config_data_storage() {
create_dir_all(get_config_folder()).expect("Cannot create folder.");
let mut session = get_config_folder();
session.push("session");
if !Path::new(session.to_str().expect("Cannot check path existence.")).exists() {
File::create(session).expect("Cannot create session file.");
}
}
fn get_config_folder() -> PathBuf {
let mut p = dirs::config_dir().unwrap();
p.push("Lychee_CLIent");
return p;
}
fn write_to_file(d: &str, p: &str) {
let mut o = File::create(p).expect("Cannot create file.");
o.write_all(d.as_bytes()).expect("Cannot write to file.");
}
fn read_from_file(p: &str) -> String {
if !Path::new(p).exists() { return "".to_string(); }
let mut d = String::new();
let mut ifile = File::open(p).expect("Cannot open file.");
ifile.read_to_string(&mut d).expect("Cannot read file.");
return d;
}
#[cfg(test)] #[cfg(test)]
mod tests_main { mod tests_main {
use hyper::Body; use hyper::Body;