Update futriix-cli/src/main.rs
This commit is contained in:
parent
273939c2f9
commit
5954774afd
@ -1,9 +1,7 @@
|
||||
use colored::Colorize;
|
||||
use rustyline::Editor;
|
||||
use rustyline::error::ReadlineError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::TcpStream;
|
||||
@ -25,12 +23,22 @@ mod server {
|
||||
CreateIndex { field: String },
|
||||
DropIndex { field: String },
|
||||
SysExec { script_name: String },
|
||||
ReplicationEnable,
|
||||
ReplicationDisable,
|
||||
ReplicationAddPeer { addr: String },
|
||||
ReplicationRemovePeer { addr: String },
|
||||
ReplicationStatus,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum Response {
|
||||
Success(Option<serde_json::Value>),
|
||||
Error(String),
|
||||
ReplicationStatus {
|
||||
enabled: bool,
|
||||
peers: Vec<String>,
|
||||
last_sync: u128,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,8 +72,9 @@ async fn send_command(cmd: Command) -> Result<Response, Box<dyn std::error::Erro
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
println!("{}", "Futriix CLI Client".bright_cyan());
|
||||
println!("{}", "Futriix CLI Client".bold().bright_cyan());
|
||||
println!("Type 'help' for available commands");
|
||||
println!();
|
||||
|
||||
let mut rl = Editor::<()>::new()?;
|
||||
if rl.load_history("futriix-history.txt").is_err() {
|
||||
@ -74,7 +83,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
loop {
|
||||
let readline = rl.readline("futriix> ");
|
||||
let readline = rl.readline(&"futriix:~> ".bright_cyan());
|
||||
match readline {
|
||||
Ok(line) => {
|
||||
rl.add_history_entry(&line);
|
||||
@ -99,6 +108,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
},
|
||||
Err(e) => println!("{}: {}", "Invalid JSON".red(), e),
|
||||
@ -120,6 +130,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -137,6 +148,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
},
|
||||
Err(e) => println!("{}: {}", "Invalid JSON".red(), e),
|
||||
@ -154,6 +166,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -164,6 +177,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -174,6 +188,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -184,6 +199,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -193,12 +209,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
continue;
|
||||
}
|
||||
let field = parts[1].to_string();
|
||||
match send_command(Command::CreateIndex { field }).await {
|
||||
match send_command(Command::CreateIndex { field: field.clone() }).await {
|
||||
Ok(Response::Success(_)) => {
|
||||
println!("{}", "Index created".bright_green());
|
||||
println!("{}", format!("Index created on field '{}'", field).bright_green());
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -214,6 +231,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
@ -234,11 +252,75 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!();
|
||||
},
|
||||
Ok(Response::Error(e)) => println!("{}", e.bold().red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
},
|
||||
"replication" => {
|
||||
if parts.len() < 2 {
|
||||
println!("{}", "Usage: replication <on|off|status|add-peer|remove-peer>".red());
|
||||
continue;
|
||||
}
|
||||
match parts[1].to_lowercase().as_str() {
|
||||
"on" => {
|
||||
match send_command(Command::ReplicationEnable).await {
|
||||
Ok(Response::Success(_)) => println!("{}", "Replication enabled".bright_green()),
|
||||
Ok(Response::Error(e)) => println!("{}", e.red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
}
|
||||
"off" => {
|
||||
match send_command(Command::ReplicationDisable).await {
|
||||
Ok(Response::Success(_)) => println!("{}", "Replication disabled".bright_green()),
|
||||
Ok(Response::Error(e)) => println!("{}", e.red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
}
|
||||
"status" => {
|
||||
match send_command(Command::ReplicationStatus).await {
|
||||
Ok(Response::ReplicationStatus { enabled, peers, last_sync }) => {
|
||||
println!("Status:");
|
||||
println!(" Enabled: {}", enabled);
|
||||
println!(" Peers: {:?}", peers);
|
||||
println!(" Last sync: {}", last_sync);
|
||||
}
|
||||
Ok(Response::Success(_)) => println!("{}", "Unexpected success response".red()),
|
||||
Ok(Response::Error(e)) => println!("{}", e.red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
}
|
||||
"add-peer" => {
|
||||
if parts.len() < 3 {
|
||||
println!("{}", "Usage: replication add-peer <addr>".red());
|
||||
continue;
|
||||
}
|
||||
match send_command(Command::ReplicationAddPeer { addr: parts[2].to_string() }).await {
|
||||
Ok(Response::Success(_)) => println!("{}", "Peer added".bright_green()),
|
||||
Ok(Response::Error(e)) => println!("{}", e.red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
}
|
||||
"remove-peer" => {
|
||||
if parts.len() < 3 {
|
||||
println!("{}", "Usage: replication remove-peer <addr>".red());
|
||||
continue;
|
||||
}
|
||||
match send_command(Command::ReplicationRemovePeer { addr: parts[2].to_string() }).await {
|
||||
Ok(Response::Success(_)) => println!("{}", "Peer removed".bright_green()),
|
||||
Ok(Response::Error(e)) => println!("{}", e.red()),
|
||||
Ok(Response::ReplicationStatus { .. }) => println!("{}", "Unexpected response type".red()),
|
||||
Err(e) => println!("{}: {}", "Connection error".red(), e),
|
||||
}
|
||||
}
|
||||
_ => println!("{}", "Unknown replication command".red()),
|
||||
}
|
||||
},
|
||||
"help" => {
|
||||
println!("Available commands:");
|
||||
println!();
|
||||
println!("{}", "Available commands:".bold());
|
||||
println!(" insert <key> <json_value> (or i) - Insert data");
|
||||
println!(" get <key> (or g) - Get data");
|
||||
println!(" update <key> <json_value> (or u) - Update data");
|
||||
@ -249,6 +331,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!(" createindex <field> - Create index");
|
||||
println!(" dropindex <field> - Drop index");
|
||||
println!(" sysexec <script_name> - Execute system script");
|
||||
println!(" replication on/off - Enable/disable replication");
|
||||
println!(" replication status - Show replication status");
|
||||
println!(" replication add-peer <addr> - Add replication peer");
|
||||
println!(" replication remove-peer <addr> - Remove replication peer");
|
||||
println!(" exit (or quit) - Exit client");
|
||||
println!(" help - Show this help");
|
||||
println!();
|
||||
|
Loading…
x
Reference in New Issue
Block a user