1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Can show command line help

This commit is contained in:
Pierre HUBERT 2021-05-04 19:36:31 +02:00
parent 37a7efb5b3
commit e73ebe9c12
2 changed files with 63 additions and 31 deletions

View File

@ -1,16 +1,35 @@
use std::future::Future;
use comunic_server::{cleanup_thread, server}; use comunic_server::{cleanup_thread, server};
use comunic_server::data::config::{conf, Config}; use comunic_server::data::config::{conf, Config};
use comunic_server::helpers::database; use comunic_server::helpers::database;
use comunic_server::utils::date_utils::current_year;
type MainActionFunction = Box<dyn Future<Output=std::io::Result<()>>>; type MainActionFunction = std::io::Result<()>;
struct Action { struct Action {
name: String, name: String,
description: String, description: String,
arguments: Vec<String>, arguments: Vec<String>,
function: Box<dyn FnOnce() -> MainActionFunction>, function: Box<dyn Fn() -> MainActionFunction>,
}
fn get_actions() -> Vec<Action> {
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] #[actix_rt::main]
@ -34,14 +53,7 @@ async fn main() -> std::io::Result<()> {
.unwrap_or("serve") .unwrap_or("serve")
.to_string(); .to_string();
/* let actions = vec![ let actions = get_actions();
Action {
name: "serve".to_string(),
description: "Start the Comunic Server (default action)".to_string(),
arguments: vec![],
function: Box::new(serve),
}
];
let selected_action = actions let selected_action = actions
.iter() .iter()
@ -56,14 +68,13 @@ async fn main() -> std::io::Result<()> {
Some(a) => a Some(a) => a
}; };
if selected_action.arguments.len() != args.len() - 2 { if !selected_action.arguments.is_empty() && selected_action.arguments.len() + 2 > args.len() {
eprintln!("Invalid number of arguments!"); eprintln!("Invalid number of arguments!");
std::process::exit(-2); std::process::exit(-2);
} }
let func = (selected_action.function)(); (selected_action.function)()?;
func.await*/ Ok(())
serve()
} }
/// Start Comunic Server (main action) /// Start Comunic Server (main action)
@ -88,3 +99,18 @@ fn serve() -> std::io::Result<()> {
t.join().unwrap(); t.join().unwrap();
Ok(()) 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::<Vec<String>>().join(" "),
action.description
);
}
Ok(())
}

View File

@ -4,7 +4,7 @@
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use chrono::{TimeZone, Utc}; use chrono::{TimeZone, Utc, Datelike};
/// Get the current time since epoch /// Get the current time since epoch
/// ///
@ -35,3 +35,9 @@ pub fn time_to_mysql_date(time: u64) -> String {
pub fn mysql_date() -> String { pub fn mysql_date() -> String {
time_to_mysql_date(time()) time_to_mysql_date(time())
} }
/// Get current year
pub fn current_year() -> i32 {
let utc = Utc.timestamp(time() as i64, 0);
utc.year()
}