From ec426cee2937c673d8adfafbec3db766732a9e4c Mon Sep 17 00:00:00 2001 From: Leandro Lisboa Penz Date: Sun, 2 Jul 2023 20:14:53 +0100 Subject: [PATCH] Move Cli to bin/, receive parameters as arguments --- src/bin/stdecor.rs | 24 ++++++++++++++++++++---- src/cli.rs | 20 -------------------- src/lib.rs | 1 - src/pipe.rs | 5 ++--- src/runner.rs | 15 +++++++-------- 5 files changed, 29 insertions(+), 36 deletions(-) delete mode 100644 src/cli.rs diff --git a/src/bin/stdecor.rs b/src/bin/stdecor.rs index 96cf6ff..fdd6fff 100644 --- a/src/bin/stdecor.rs +++ b/src/bin/stdecor.rs @@ -6,6 +6,21 @@ use clap::Parser; use std::error::Error; use std::process; +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +pub struct Cli { + /// Add a default prefix to both stdout and stderr + #[arg(short, long, default_value = "")] + pub prefix: String, + + /// Add the date and time as a prefix to both stdout and stderr + #[arg(short, long, default_value_t = false)] + pub date: bool, + + /// The command to run; use stdin if empty (pipe mode) + pub command: Vec, +} + #[tokio::main] async fn main() -> Result<(), Box> { color_eyre::install()?; @@ -13,12 +28,13 @@ async fn main() -> Result<(), Box> { .with_span_events(tracing_subscriber::fmt::format::FmtSpan::ACTIVE) .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .init(); - let args = stdecor::cli::Cli::parse(); - if args.command.is_empty() { - stdecor::pipe::pipe(&args).await?; + let cli = Cli::parse(); + if cli.command.is_empty() { + stdecor::pipe::pipe(&cli.prefix, cli.date).await?; Ok(()) } else { - let exitstatus = stdecor::runner::run(&args).await?; + let command: Vec<&str> = cli.command.iter().map(String::as_ref).collect(); + let exitstatus = stdecor::runner::run(&cli.prefix, cli.date, &command).await?; process::exit(exitstatus.code().unwrap_or(0)); } } diff --git a/src/cli.rs b/src/cli.rs deleted file mode 100644 index 9cfd042..0000000 --- a/src/cli.rs +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (C) 2023 Leandro Lisboa Penz -// This file is subject to the terms and conditions defined in -// file 'LICENSE', which is part of this source code package. - -use clap::Parser; - -#[derive(Parser, Debug)] -#[command(author, version, about, long_about = None)] -pub struct Cli { - /// Add a default prefix to both stdout and stderr - #[arg(short, long, default_value = "")] - pub prefix: String, - - /// Add the date and time as a prefix to both stdout and stderr - #[arg(short, long, default_value_t = false)] - pub date: bool, - - /// The command to run; use stdin if empty (pipe mode) - pub command: Vec, -} diff --git a/src/lib.rs b/src/lib.rs index f1d4ae2..a68e7f7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,6 @@ // This file is subject to the terms and conditions defined in // file 'LICENSE', which is part of this source code package. -pub mod cli; pub mod decor_async; pub mod pipe; pub mod runner; diff --git a/src/pipe.rs b/src/pipe.rs index 00d4701..52eae18 100644 --- a/src/pipe.rs +++ b/src/pipe.rs @@ -7,15 +7,14 @@ use tokio::io::{self, AsyncBufReadExt, BufReader, BufWriter}; use tokio_stream::wrappers::LinesStream; use tokio_stream::StreamExt; -use crate::cli::Cli; use crate::decor_async; #[tracing::instrument] -pub async fn pipe(cli: &Cli) -> Result<()> { +pub async fn pipe(prefix: &str, date: bool) -> Result<()> { let mut stdin_lines = LinesStream::new(BufReader::new(io::stdin()).lines()); let mut stdout = BufWriter::new(io::stdout()); while let Some(line) = stdin_lines.next().await { - decor_async::decor_write(&cli.prefix, cli.date, &line?, &mut stdout).await?; + decor_async::decor_write(prefix, date, &line?, &mut stdout).await?; } Ok(()) } diff --git a/src/runner.rs b/src/runner.rs index 70bc6e3..f9a13a9 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -11,13 +11,12 @@ use tokio::process::Command; use tokio_process_stream as tps; use tokio_stream::StreamExt; -use crate::cli::Cli; use crate::decor_async; #[tracing::instrument] -pub fn buildcmd(cli: &Cli) -> Command { - let mut cmd = Command::new(&cli.command[0]); - cmd.args(cli.command.iter().skip(1)) +pub fn buildcmd(command: &[&str]) -> Command { + let mut cmd = Command::new(command[0]); + cmd.args(command.iter().skip(1)) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()); @@ -25,18 +24,18 @@ pub fn buildcmd(cli: &Cli) -> Command { } #[tracing::instrument] -pub async fn run(cli: &Cli) -> Result { - let cmd = buildcmd(cli); +pub async fn run(prefix: &str, date: bool, command: &[&str]) -> Result { + let cmd = buildcmd(command); let mut stream = tps::ProcessStream::try_from(cmd)?; let mut stdout = BufWriter::new(io::stdout()); let mut stderr = BufWriter::new(io::stderr()); while let Some(item) = stream.next().await { match item { tps::Item::Stdout(line) => { - decor_async::decor_write(&cli.prefix, cli.date, &line, &mut stdout).await?; + decor_async::decor_write(prefix, date, &line, &mut stdout).await?; } tps::Item::Stderr(line) => { - decor_async::decor_write(&cli.prefix, cli.date, &line, &mut stderr).await?; + decor_async::decor_write(prefix, date, &line, &mut stderr).await?; } tps::Item::Done(s) => { return Ok(s?);