1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +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::data::config::{conf, Config};
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 {
name: String,
description: 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]
@ -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)
@ -88,3 +99,18 @@ 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::<Vec<String>>().join(" "),
action.description
);
}
Ok(())
}

View File

@ -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
///
@ -35,3 +35,9 @@ pub fn time_to_mysql_date(time: u64) -> String {
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()
}