#!/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); }