#![allow(
clippy::needless_pass_by_ref_mut )]
use clap::{builder::ValueParser, Parser};
use common::comp;
use server::persistence::SqlLogMode;
use std::{str::FromStr, sync::mpsc::Sender};
use tracing::error;
fn admin_role_value_parser() -> ValueParser {
ValueParser::new(move |s: &str| -> Result<comp::AdminRole, String> {
comp::AdminRole::from_str(&s.to_lowercase()).map_err(|err| err.to_string())
})
}
#[derive(Clone, Debug, Parser)]
pub enum Admin {
Add {
username: String,
#[arg(value_parser = admin_role_value_parser())]
role: comp::AdminRole,
},
Remove {
username: String,
},
}
#[derive(Clone, Debug, Parser)]
pub enum Shutdown {
Immediate,
Graceful {
seconds: u64,
#[arg(short, long, default_value = "The server is shutting down")]
reason: String,
},
Cancel,
}
#[derive(Clone, Debug, Parser)]
pub enum SharedCommand {
Admin {
#[command(subcommand)]
command: Admin,
},
}
#[derive(Debug, Clone, Parser)]
pub enum Message {
#[command(flatten)]
Shared(SharedCommand),
Shutdown {
#[command(subcommand)]
command: Shutdown,
},
#[cfg(feature = "worldgen")]
LoadArea {
view_distance: u32,
},
SqlLogMode {
#[arg(default_value_t, value_parser = clap::value_parser!(SqlLogMode))]
mode: SqlLogMode,
},
DisconnectAllClients,
ListPlayers,
ListLogs,
SendGlobalMsg {
msg: String,
},
}
#[derive(Debug, Clone)]
pub enum MessageReturn {
Players(Vec<String>),
Logs(Vec<String>),
}
#[derive(Parser)]
#[command(
name = "Veloren server TUI",
version = common::util::DISPLAY_VERSION_LONG.as_str(),
about = "The veloren server tui allows sending commands directly to the running server.",
author = "The veloren devs <https://gitlab.com/veloren/veloren>",
)]
#[clap(no_binary_name = true)]
pub struct TuiApp {
#[command(subcommand)]
command: Message,
}
#[derive(Debug, Clone, Copy, Parser)]
pub struct BenchParams {
#[arg(long)]
pub view_distance: u32,
#[arg(long)]
pub duration: u32,
}
#[derive(Parser)]
pub enum ArgvCommand {
#[command(flatten)]
Shared(SharedCommand),
Bench(BenchParams),
}
#[derive(Parser)]
#[command(
name = "Veloren server CLI",
version = common::util::DISPLAY_VERSION_LONG.as_str(),
about = "The veloren server cli provides an easy to use interface to start a veloren server.",
author = "The veloren devs <https://gitlab.com/veloren/veloren>",
)]
pub struct ArgvApp {
#[arg(long, short)]
pub tui: bool,
#[arg(long, short)]
pub non_interactive: bool,
#[arg(long)]
pub no_auth: bool,
#[arg(default_value_t, long, short, value_parser = clap::value_parser!(SqlLogMode))]
pub sql_log_mode: SqlLogMode,
#[command(subcommand)]
pub command: Option<ArgvCommand>,
}
pub fn parse_command(input: &str, msg_s: &mut Sender<Message>) {
match TuiApp::try_parse_from(shell_words::split(input).unwrap_or_default()) {
Ok(message) => {
msg_s
.send(message.command)
.unwrap_or_else(|e| error!(?e, "Failed to send CLI message"));
},
Err(e) => error!("{}", e),
}
}