diff options
| author | Fuwn <[email protected]> | 2021-05-03 11:13:32 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-03 11:13:32 -0700 |
| commit | 82fbdc9651acfce184e2fdb34dc35fdd2e80746e (patch) | |
| tree | d085c111ca8a9eb1fb7b17fdaf54fb9b8011e8ba | |
| parent | revert(deps): remove dev-dependencies (diff) | |
| download | whirl-82fbdc9651acfce184e2fdb34dc35fdd2e80746e.tar.xz whirl-82fbdc9651acfce184e2fdb34dc35fdd2e80746e.zip | |
refactor(global): remove all the fluff from main file
| -rw-r--r-- | src/cli.rs | 104 | ||||
| -rw-r--r-- | src/lib.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 83 | ||||
| -rw-r--r-- | src/subs.rs | 44 |
4 files changed, 124 insertions, 108 deletions
@@ -1,32 +1,80 @@ // Copyleft (ɔ) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only -use structopt::clap::{App, AppSettings, Arg, SubCommand}; - -pub fn cli<'b, 'a>() -> App<'a, 'b> { - App::new(env!("CARGO_PKG_NAME")) - .about(env!("CARGO_PKG_DESCRIPTION")) - .version(env!("CARGO_PKG_VERSION")) - .author(env!("CARGO_PKG_AUTHORS")) - .settings(&[AppSettings::SubcommandRequiredElseHelp]) - .subcommands(vec![ - SubCommand::with_name("run").about("Start the WorldServer"), - SubCommand::with_name("config") - .setting(AppSettings::SubcommandRequiredElseHelp) - .subcommands(vec![SubCommand::with_name("show")]), - SubCommand::with_name("completions") - .setting(AppSettings::SubcommandRequiredElseHelp) - .about("Generate shell completions") - .subcommands(vec![ - SubCommand::with_name("powershell"), - SubCommand::with_name("bash"), - SubCommand::with_name("elvish"), - SubCommand::with_name("zsh"), - SubCommand::with_name("fish"), - ]), - ]) - .args(&[ - Arg::with_name("debug").short("d").long("debug"), - Arg::with_name("trace").short("t").long("trace"), - ]) +use structopt::clap::{App, AppSettings, Arg, ArgMatches, Shell, SubCommand}; + +use crate::{config::Config, subs::run}; + +pub struct CLI; +impl CLI { + pub fn setup() -> ArgMatches<'static> { + let matches = Self::cli().get_matches(); + + Self::calc_log_level(&matches); + std::env::set_var("DATABASE_URL", "sqlite:whirl.db"); + + matches + } + + pub async fn execute(matches: ArgMatches<'_>) { + if matches.is_present("run") { + run().await.unwrap(); + } else if let Some(cmd) = matches.subcommand_matches("config") { + if cmd.is_present("show") { + println!("{:#?}", Config::get().unwrap()); + } + } else if let Some(shell) = matches.subcommand_matches("completions") { + if shell.is_present("powershell") { + Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::PowerShell, "."); + } else if shell.is_present("bash") { + Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Bash, "."); + } else if shell.is_present("elvish") { + Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Elvish, "."); + } else if shell.is_present("zsh") { + Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Zsh, "."); + } else if shell.is_present("fish") { + Self::cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Fish, "."); + } + debug!("generated shell completions"); + } + } + + fn calc_log_level(matches: &ArgMatches<'_>) { + let mut log_level = "whirl=error,whirl=warn,whirl=trace".to_string(); + if matches.is_present("debug") || Config::get().unwrap().whirlsplash.log_level >= 2 { + log_level += ",whirl=debug"; + } + if matches.is_present("trace") || Config::get().unwrap().whirlsplash.log_level >= 3 { + log_level += ",whirl=trace"; + } + std::env::set_var("RUST_LOG", log_level); + } + + fn cli<'a, 'b>() -> App<'a, 'b> { + App::new(env!("CARGO_PKG_NAME")) + .about(env!("CARGO_PKG_DESCRIPTION")) + .version(env!("CARGO_PKG_VERSION")) + .author(env!("CARGO_PKG_AUTHORS")) + .settings(&[AppSettings::SubcommandRequiredElseHelp]) + .subcommands(vec![ + SubCommand::with_name("run").about("Start the WorldServer"), + SubCommand::with_name("config") + .setting(AppSettings::SubcommandRequiredElseHelp) + .subcommands(vec![SubCommand::with_name("show")]), + SubCommand::with_name("completions") + .setting(AppSettings::SubcommandRequiredElseHelp) + .about("Generate shell completions") + .subcommands(vec![ + SubCommand::with_name("powershell"), + SubCommand::with_name("bash"), + SubCommand::with_name("elvish"), + SubCommand::with_name("zsh"), + SubCommand::with_name("fish"), + ]), + ]) + .args(&[ + Arg::with_name("debug").short("d").long("debug"), + Arg::with_name("trace").short("t").long("trace"), + ]) + } } @@ -17,6 +17,7 @@ extern crate rocket; pub mod cli; pub mod config; +pub mod subs; pub mod api; pub mod db; diff --git a/src/main.rs b/src/main.rs index 4c44afa..c4b6c49 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,96 +1,19 @@ // Copyleft (ɔ) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only -#[macro_use] -extern crate log; - use std::error::Error; -use structopt::clap::Shell; -use whirl::{ - api::API, - cli::cli, - config::Config, - server::{ - distributor::Distributor, - hub::Hub, - server::{ - Server, - ServerType::{AutoServer, RoomServer}, - }, - }, -}; +use whirl::cli::CLI; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { - // Setup CLI - let matches = cli().get_matches(); - - // Set logging level - let mut log_level = "whirl=error,whirl=warn,whirl=trace".to_string(); - if matches.is_present("debug") || Config::get()?.whirlsplash.log_level >= 2 { - log_level += ",whirl=debug"; - } - if matches.is_present("trace") || Config::get()?.whirlsplash.log_level >= 3 { - log_level += ",whirl=trace"; - } - std::env::set_var("RUST_LOG", log_level); - - // Set database URL - std::env::set_var("DATABASE_URL", "sqlite:whirl.db"); + let matches = CLI::setup(); // Setup logging dotenv::dotenv().ok(); pretty_env_logger::init(); - // Handle CLI command - if matches.is_present("run") { - run().await.unwrap(); - } else if let Some(cmd) = matches.subcommand_matches("config") { - if cmd.is_present("show") { - println!("{:#?}", Config::get()?); - } - } else if let Some(shell) = matches.subcommand_matches("completions") { - if shell.is_present("powershell") { - cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::PowerShell, "."); - } else if shell.is_present("bash") { - cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Bash, "."); - } else if shell.is_present("elvish") { - cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Elvish, "."); - } else if shell.is_present("zsh") { - cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Zsh, "."); - } else if shell.is_present("fish") { - cli().gen_completions(env!("CARGO_PKG_NAME"), Shell::Fish, "."); - } - debug!("generated shell completions"); - } - - Ok(()) -} - -async fn run() -> Result<(), Box<dyn Error>> { - let threads = vec![ - tokio::spawn(async move { - let _ = Distributor::listen( - &*format!("0.0.0.0:{}", Config::get().unwrap().distributor.port), - AutoServer, - ) - .await; - }), - tokio::spawn(async move { - let _ = Hub::listen( - &*format!("0.0.0.0:{}", Config::get().unwrap().hub.port), - RoomServer, - ) - .await; - }), - tokio::spawn(async move { - let _ = API::listen(); - }), - ]; - for thread in threads { - let _ = thread.await; - } + CLI::execute(matches).await; Ok(()) } diff --git a/src/subs.rs b/src/subs.rs new file mode 100644 index 0000000..a8bde65 --- /dev/null +++ b/src/subs.rs @@ -0,0 +1,44 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use std::error::Error; + +use crate::{ + api::API, + config::Config, + server::{ + distributor::Distributor, + hub::Hub, + server::{ + Server, + ServerType::{AutoServer, RoomServer}, + }, + }, +}; + +pub async fn run() -> Result<(), Box<dyn Error>> { + let threads = vec![ + tokio::spawn(async move { + let _ = Distributor::listen( + &*format!("0.0.0.0:{}", Config::get().unwrap().distributor.port), + AutoServer, + ) + .await; + }), + tokio::spawn(async move { + let _ = Hub::listen( + &*format!("0.0.0.0:{}", Config::get().unwrap().hub.port), + RoomServer, + ) + .await; + }), + tokio::spawn(async move { + let _ = API::listen(); + }), + ]; + for thread in threads { + let _ = thread.await; + } + + Ok(()) +} |