specify multiple additional file targets + cli interface #2
cargo devel CI / cargo CI (push) Successful in 31s Details

This commit is contained in:
Christoph J. Scherr 2024-02-21 22:50:52 +01:00
parent 15d6a1d2e1
commit e80f2cd2d0
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
2 changed files with 31 additions and 2 deletions

View File

@ -5,10 +5,11 @@ edition = "2021"
publish = true publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"] authors = ["Christoph J. Scherr <software@cscherr.de>"]
license = "MIT" 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" readme = "README.md"
homepage = "https://git.cscherr.de/PlexSheep/seep" homepage = "https://git.cscherr.de/PlexSheep/seep"
repository = "https://git.cscherr.de/PlexSheep/seep" repository = "https://git.cscherr.de/PlexSheep/seep"
keywords = ["cli"] keywords = ["cli"]
[dependencies] [dependencies]
clap = { version = "4.5.1", features = ["derive"] }

View File

@ -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 TTY: &str = "/dev/tty";
const BUFSIZ: usize = 2 << 8; const BUFSIZ: usize = 2 << 8;
#[derive(Parser)]
#[command(version, about)]
struct Cli {
targets: Vec<PathBuf>,
}
fn main() -> Result<(), io::Error> { 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 // /dev/tty points the currently opened terminal. If we write to it directly, we bypass piping
// to the next process // to the next process
let tty = std::fs::File::options() 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 // 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. // that we can write to it.
let mut targets: Vec<Box<dyn Write>> = vec![Box::new(tty), Box::new(io::stdout())]; let mut targets: Vec<Box<dyn Write>> = 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 // fill the buffer with data from stdin, then write the read data to all targets
let mut buf = [0; BUFSIZ]; let mut buf = [0; BUFSIZ];