mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 09:34:04 +00:00 
			
		
		
		
	Can show command line help
This commit is contained in:
		
							
								
								
									
										86
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										86
									
								
								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<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)
 | 
			
		||||
@@ -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::<Vec<String>>().join(" "),
 | 
			
		||||
                 action.description
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
@@ -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()
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user