Skip to content

Commit

Permalink
Move Cli to bin/, receive parameters as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
lpenz committed Jul 2, 2023
1 parent d096902 commit ec426ce
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
24 changes: 20 additions & 4 deletions src/bin/stdecor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ 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<String>,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
color_eyre::install()?;
tracing_subscriber::fmt()
.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));
}
}
20 changes: 0 additions & 20 deletions src/cli.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
5 changes: 2 additions & 3 deletions src/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
15 changes: 7 additions & 8 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,31 @@ 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());
cmd
}

#[tracing::instrument]
pub async fn run(cli: &Cli) -> Result<ExitStatus> {
let cmd = buildcmd(cli);
pub async fn run(prefix: &str, date: bool, command: &[&str]) -> Result<ExitStatus> {
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?);
Expand Down

0 comments on commit ec426ce

Please sign in to comment.