Better cli (added help)
This commit is contained in:
+25
-9
@@ -7,6 +7,21 @@ use crate::{logger, plugin::loader::PluginLoader, server::Server};
|
|||||||
|
|
||||||
logger!(LOGGER "CLI");
|
logger!(LOGGER "CLI");
|
||||||
|
|
||||||
|
macro_rules! commands {
|
||||||
|
(($cmdi:expr) $($cmd:literal $desc:literal => $body:block)*) => {
|
||||||
|
match $cmdi {
|
||||||
|
"help" => {
|
||||||
|
LOGGER.info("List of commands:");
|
||||||
|
$(
|
||||||
|
println!("\x1b[34m{}\x1b[0m: {}", $cmd, format!($desc));
|
||||||
|
)*
|
||||||
|
},
|
||||||
|
$($cmd => $body)*
|
||||||
|
_ => LOGGER.error(format!("Command not found: {}", $cmdi)),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
pub fn parse_args(input: &str) -> Vec<String> {
|
pub fn parse_args(input: &str) -> Vec<String> {
|
||||||
let mut args = Vec::new();
|
let mut args = Vec::new();
|
||||||
let mut current = String::new();
|
let mut current = String::new();
|
||||||
@@ -69,7 +84,7 @@ pub fn start_cli(server: Arc<Server>, plugin_loader: PluginLoader) {
|
|||||||
let mut rl = DefaultEditor::new().unwrap();
|
let mut rl = DefaultEditor::new().unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let line = match rl.readline("> ") {
|
let line = match rl.readline("\n> ") {
|
||||||
Ok(line) => {
|
Ok(line) => {
|
||||||
rl.add_history_entry(line.as_str()).ok();
|
rl.add_history_entry(line.as_str()).ok();
|
||||||
line
|
line
|
||||||
@@ -87,8 +102,9 @@ pub fn start_cli(server: Arc<Server>, plugin_loader: PluginLoader) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
match args[0].as_str() {
|
commands! {
|
||||||
"install" => {
|
(args[0].as_str())
|
||||||
|
"install" "Installs the specified plugin .vxp package" => {
|
||||||
if require_args(&args, &["<path.vxp>"]) {
|
if require_args(&args, &["<path.vxp>"]) {
|
||||||
let path = PathBuf::from_str(&args[1]).unwrap();
|
let path = PathBuf::from_str(&args[1]).unwrap();
|
||||||
if path.extension().and_then(|s| s.to_str()) == Some("vxp") {
|
if path.extension().and_then(|s| s.to_str()) == Some("vxp") {
|
||||||
@@ -105,20 +121,20 @@ pub fn start_cli(server: Arc<Server>, plugin_loader: PluginLoader) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"load" => {
|
"load" "loads a plugin that has been installed" => {
|
||||||
if require_args(&args, &["<plugin-dir>"]) {
|
if require_args(&args, &["<plugin-dir>"]) {
|
||||||
plugin_loader.load(&server.root.join("plugins").join(&args[1]));
|
plugin_loader.load(&server.root.join("plugins").join(&args[1]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
"ping" => {
|
"shutdown" "Softly shuts the server down, may not fully shut everything down" => {
|
||||||
LOGGER.info("pong");
|
|
||||||
}
|
|
||||||
"shutdown" => {
|
|
||||||
server.shutdown();
|
server.shutdown();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => LOGGER.error(format!("Command not found: {}", args[0])),
|
|
||||||
|
"ping" "replies with pong" => {
|
||||||
|
LOGGER.info("pong");
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -118,7 +118,10 @@ impl Server {
|
|||||||
let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port))?;
|
let listener = TcpListener::bind(format!("0.0.0.0:{}", self.config.port))?;
|
||||||
Self::LOGGER.info(format!("Server listening at 0.0.0.0:{}", self.config.port));
|
Self::LOGGER.info(format!("Server listening at 0.0.0.0:{}", self.config.port));
|
||||||
|
|
||||||
println!("{WELCOME}\nversion {}\n", env!("CARGO_PKG_VERSION"));
|
println!(
|
||||||
|
"{WELCOME}\nversion {}\nType 'help' to see available commands.",
|
||||||
|
env!("CARGO_PKG_VERSION")
|
||||||
|
);
|
||||||
|
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
match stream {
|
match stream {
|
||||||
|
|||||||
Reference in New Issue
Block a user