cli
This commit is contained in:
123
src/bin/cli.rs
Normal file
123
src/bin/cli.rs
Normal file
@@ -0,0 +1,123 @@
|
||||
use std::io::{Write as _, stdin};
|
||||
|
||||
use archive::{Result, client::Client};
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(version, about, long_about = None)]
|
||||
struct Args {
|
||||
#[arg(long, default_value = "127.0.0.1")]
|
||||
host: String,
|
||||
#[arg(short, long, default_value_t = 6171)]
|
||||
port: u16,
|
||||
}
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(help_template = "{subcommands}")]
|
||||
enum Commands {
|
||||
Get {
|
||||
key: String,
|
||||
},
|
||||
Set {
|
||||
key: String,
|
||||
value: String,
|
||||
expiration: Option<u64>,
|
||||
},
|
||||
Delete {
|
||||
key: String,
|
||||
},
|
||||
Has {
|
||||
key: String,
|
||||
},
|
||||
Ttl {
|
||||
key: String,
|
||||
},
|
||||
Expire {
|
||||
key: String,
|
||||
seconds: u64,
|
||||
},
|
||||
Persist {
|
||||
key: String,
|
||||
},
|
||||
#[command(aliases = &["exit", "q"])]
|
||||
Quit,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let args = Args::parse();
|
||||
|
||||
let mut client = Client::new(&format!("{}:{}", args.host, args.port)).await?;
|
||||
|
||||
print_welcome();
|
||||
|
||||
let stdin = stdin();
|
||||
let mut input = String::new();
|
||||
|
||||
loop {
|
||||
input.clear();
|
||||
print!("> ");
|
||||
std::io::stdout().flush()?;
|
||||
stdin.read_line(&mut input)?;
|
||||
|
||||
let Ok(mut words) = shell_words::split(&input) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
words.insert(0, "".into());
|
||||
|
||||
let command = match Commands::try_parse_from(words.clone()) {
|
||||
Ok(command) => command,
|
||||
Err(e) => {
|
||||
println!("{}", e.render());
|
||||
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
match command {
|
||||
Commands::Get { key } => {
|
||||
let value = client.get(&key).await?;
|
||||
println!("{value:?}");
|
||||
}
|
||||
Commands::Set {
|
||||
key,
|
||||
value,
|
||||
expiration,
|
||||
} => {
|
||||
client.set(&key, value.as_bytes(), expiration).await?;
|
||||
println!("1");
|
||||
}
|
||||
Commands::Delete { key } => {
|
||||
let value = client.delete(&key).await?;
|
||||
println!("{value:?}");
|
||||
}
|
||||
Commands::Has { key } => {
|
||||
let value = client.has(&key).await?;
|
||||
println!("{value:?}");
|
||||
}
|
||||
Commands::Ttl { key } => {
|
||||
let value = client.ttl(&key).await?;
|
||||
println!("{value:?}");
|
||||
}
|
||||
Commands::Expire { key, seconds } => {
|
||||
let value = client.expire(&key, seconds).await?;
|
||||
println!("{value:?}");
|
||||
}
|
||||
Commands::Persist { key } => {
|
||||
let value = client.persist(&key).await?;
|
||||
println!("{value:?}");
|
||||
}
|
||||
Commands::Quit => break,
|
||||
}
|
||||
}
|
||||
|
||||
println!("{input}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn print_welcome() {
|
||||
let version = env!("CARGO_PKG_VERSION");
|
||||
println!("archive-cli {version}");
|
||||
}
|
||||
@@ -1,22 +1,9 @@
|
||||
use config::ServerConfig;
|
||||
use errors::AppError;
|
||||
use server::Server;
|
||||
use archive::Result;
|
||||
use archive::config::ServerConfig;
|
||||
use archive::server::Server;
|
||||
|
||||
use tokio::{signal::ctrl_c, sync::oneshot};
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests;
|
||||
|
||||
pub mod client;
|
||||
pub mod commands;
|
||||
pub mod config;
|
||||
pub mod connection;
|
||||
pub mod database;
|
||||
pub mod errors;
|
||||
pub mod handler;
|
||||
pub mod server;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, AppError>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
env_logger::builder()
|
||||
15
src/lib.rs
Normal file
15
src/lib.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use errors::AppError;
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests;
|
||||
|
||||
pub mod client;
|
||||
pub mod commands;
|
||||
pub mod config;
|
||||
pub mod connection;
|
||||
pub mod database;
|
||||
pub mod errors;
|
||||
pub mod handler;
|
||||
pub mod server;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, AppError>;
|
||||
Reference in New Issue
Block a user