From e73ebe9c1227177e1d0c5f42e877b96356584c16 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 4 May 2021 19:36:31 +0200 Subject: [PATCH] Can show command line help --- src/main.rs | 86 +++++++++++++++++++++++++++-------------- src/utils/date_utils.rs | 8 +++- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/src/main.rs b/src/main.rs index 858d22d..c5494e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,35 @@ -use std::future::Future; - use comunic_server::{cleanup_thread, server}; use comunic_server::data::config::{conf, Config}; use comunic_server::helpers::database; +use comunic_server::utils::date_utils::current_year; -type MainActionFunction = Box>>; +type MainActionFunction = std::io::Result<()>; struct Action { name: String, description: String, arguments: Vec, - function: Box MainActionFunction>, + function: Box MainActionFunction>, +} + +fn get_actions() -> Vec { + vec![ + // Start server + Action { + name: "serve".to_string(), + description: "Start the Comunic Server (default action)".to_string(), + arguments: vec![], + function: Box::new(serve), + }, + + // Show help + Action { + name: "help".to_string(), + description: "Show this help".to_string(), + arguments: vec![], + function: Box::new(help), + } + ] } #[actix_rt::main] @@ -34,36 +53,28 @@ async fn main() -> std::io::Result<()> { .unwrap_or("serve") .to_string(); - /* let actions = vec![ - Action { - name: "serve".to_string(), - description: "Start the Comunic Server (default action)".to_string(), - arguments: vec![], - function: Box::new(serve), - } - ]; + let actions = get_actions(); - let selected_action = actions - .iter() - .filter(|p|p.name.eq(&action)) - .next(); + let selected_action = actions + .iter() + .filter(|p| p.name.eq(&action)) + .next(); - let selected_action = match selected_action { - None => { - eprintln!("Action {} invalid! For more information try 'help'!", action); - std::process::exit(-1); - } - Some(a) => a - }; + let selected_action = match selected_action { + None => { + eprintln!("Action {} invalid! For more information try 'help'!", action); + std::process::exit(-1); + } + Some(a) => a + }; - if selected_action.arguments.len() != args.len() - 2 { - eprintln!("Invalid number of arguments!"); - std::process::exit(-2); - } + if !selected_action.arguments.is_empty() && selected_action.arguments.len() + 2 > args.len() { + eprintln!("Invalid number of arguments!"); + std::process::exit(-2); + } - let func = (selected_action.function)(); - func.await*/ - serve() + (selected_action.function)()?; + Ok(()) } /// Start Comunic Server (main action) @@ -86,5 +97,20 @@ fn serve() -> std::io::Result<()> { }); t.join().unwrap(); + Ok(()) +} + +fn help() -> std::io::Result<()> { + println!("Comunic API v3 Server - (c) Pierre HUBERT 2012 - {}", current_year()); + + println!("Available actions:"); + for action in get_actions() { + println!("\t{}\t{} - {}", + action.name, + action.arguments.iter().map(|s| format!("[{}]", s)).collect::>().join(" "), + action.description + ); + } + Ok(()) } \ No newline at end of file diff --git a/src/utils/date_utils.rs b/src/utils/date_utils.rs index 12abd00..e5f2e5c 100644 --- a/src/utils/date_utils.rs +++ b/src/utils/date_utils.rs @@ -4,7 +4,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; -use chrono::{TimeZone, Utc}; +use chrono::{TimeZone, Utc, Datelike}; /// Get the current time since epoch /// @@ -34,4 +34,10 @@ pub fn time_to_mysql_date(time: u64) -> String { /// Get current Mysql formatted date pub fn mysql_date() -> String { time_to_mysql_date(time()) +} + +/// Get current year +pub fn current_year() -> i32 { + let utc = Utc.timestamp(time() as i64, 0); + utc.year() } \ No newline at end of file