diff --git a/Cargo.toml b/Cargo.toml index 9d1921a..aba419e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,10 +5,11 @@ edition = "2021" publish = true authors = ["Christoph J. Scherr "] license = "MIT" -description = "print stdin to terminal, then to stdout (for piping)" +description = "print the stdin and redirect to stdout and files" readme = "README.md" homepage = "https://git.cscherr.de/PlexSheep/seep" repository = "https://git.cscherr.de/PlexSheep/seep" keywords = ["cli"] [dependencies] +clap = { version = "4.5.1", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index bc47b22..d59002e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,23 @@ -use std::io::{self, prelude::*}; +use std::{ + fs::File, + io::{self, prelude::*}, + path::PathBuf, +}; + +use clap::Parser; + const TTY: &str = "/dev/tty"; const BUFSIZ: usize = 2 << 8; +#[derive(Parser)] +#[command(version, about)] +struct Cli { + targets: Vec, +} + fn main() -> Result<(), io::Error> { + let cli = Cli::parse(); + // /dev/tty points the currently opened terminal. If we write to it directly, we bypass piping // to the next process let tty = std::fs::File::options() @@ -16,6 +31,19 @@ fn main() -> Result<(), io::Error> { // any file or stream that we want our stdin to go to can be a target. The only condition is // that we can write to it. let mut targets: Vec> = vec![Box::new(tty), Box::new(io::stdout())]; + for path in cli.targets { + match File::options() + .write(true) + .read(false) + .create(true) + .open(path) + { + Ok(f) => targets.push(Box::new(f)), + Err(err) => { + eprintln!("error while opening file: {err}"); + } + } + } // fill the buffer with data from stdin, then write the read data to all targets let mut buf = [0; BUFSIZ];