Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
small-rust flush_cache.rs
1.3 kB · 54 lines
Rust
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455#!/usr/bin/env -S cargo +nightly -Zscript
---[package]edition = "2024"[dependencies]argh = "0.1"---
use std::io::{BufRead, BufReader, Write};use std::net::{Shutdown, TcpStream};
use argh::FromArgs;
const FLUSH_COMMAND: &str = "FLUSHALL\r\n";
#[derive(FromArgs)]#[argh(description = "Flush the cache of a redis instance.")]struct App { #[argh( option, description = "host for the redis instance.", default = "\"127.0.0.1\".to_string()" )] host: String,
#[argh(option, description = "port redis is listening on.", default = "6379")] port: u16,}
fn main() { let app: App = argh::from_env();
let address = format!("{}:{}", app.host, app.port); let mut stream = TcpStream::connect(address).expect("should be able to connect to redis");
stream .write(FLUSH_COMMAND.as_bytes()) .expect("should be able to write flush command"); stream .flush() .expect("should be able to send flush command"); stream .shutdown(Shutdown::Write) .expect("should be able to shutdown writer");
let mut stream = BufReader::new(stream);
let mut output = String::new(); let _ = stream .read_line(&mut output) .expect("should be able to read response"); println!("{}", &output);}