add cli for server/client (#4)

This PR adds a CLI to main.rs that allows you to run the redis server and eventually the redis client.
This commit is contained in:
Avery Harnish
2020-03-03 11:15:20 -06:00
committed by GitHub
parent 9852de9924
commit 80511f2cb5
12 changed files with 233 additions and 25 deletions

40
src/bin/client.rs Normal file
View File

@@ -0,0 +1,40 @@
use bytes::Bytes;
use clap::Clap;
use mini_redis::{client, DEFAULT_PORT};
use std::{io, str};
#[tokio::main]
async fn main() -> io::Result<()> {
let cli = Cli::parse();
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
let mut client = client::connect(&format!("127.0.0.1:{}", port)).await?;
match cli.command {
Client::Get { key } => {
let result = client.get(&key).await?;
if let Some(result) = result {
println!("\"{}\"", str::from_utf8(&result).unwrap());
} else {
println!("(nil)");
}
Ok(())
}
Client::Set { key, value } => client.set(&key, Bytes::from(value)).await,
}
}
#[derive(Clap, Debug)]
#[clap(name = "mini-redis-client", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "Opens a connection to a Redis server")]
struct Cli {
#[clap(subcommand)]
command: Client,
#[clap(name = "port", long = "--port")]
port: Option<String>,
}
#[derive(Clap, Debug)]
enum Client {
#[clap(about = "Gets a value associated with a key")]
Get { key: String },
#[clap(about = "Associates a value with a key")]
Set { key: String, value: String },
}

17
src/bin/server.rs Normal file
View File

@@ -0,0 +1,17 @@
use clap::Clap;
use mini_redis::{server, DEFAULT_PORT};
use std::io;
#[tokio::main]
pub async fn main() -> io::Result<()> {
let cli = Cli::parse();
let port = cli.port.unwrap_or(DEFAULT_PORT.to_string());
server::run(&port).await
}
#[derive(Clap, Debug)]
#[clap(name = "mini-redis-server", version = env!("CARGO_PKG_VERSION"), author = env!("CARGO_PKG_AUTHORS"), about = "A Redis server")]
struct Cli {
#[clap(name = "port", long = "--port")]
port: Option<String>,
}