From 088eb42521432d60a443a27eea846e67267ec222 Mon Sep 17 00:00:00 2001 From: Zykino Date: Mon, 16 Jan 2023 22:51:33 +0100 Subject: [PATCH] Add quiet option (which may still be quite noisy) --- src/command.rs | 28 +++++++++++++++------------- src/lib.rs | 6 ++++-- src/main.rs | 5 ++++- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/command.rs b/src/command.rs index e828cea..901ce17 100644 --- a/src/command.rs +++ b/src/command.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use std::fmt; use std::io::{stdout, Write}; use std::path::PathBuf; -use std::process::Command; +use std::process::{Command, Stdio}; #[derive(Debug, Serialize, Deserialize, PartialEq)] enum UpdateSteps { @@ -213,12 +213,7 @@ impl ActualCmd { fn execute(&self, opt: &Opt) -> Result { let mut cmd = Command::new(&self.exe); - cmd.args(&self.params) - .env_clear() - .envs(&self.env) - // .stdout(cfg) - // .stderr(cfg) - ; + cmd.args(&self.params).env_clear().envs(&self.env); if let Some(cdir) = &self.current_dir { cmd.current_dir(std::fs::canonicalize(cdir).unwrap()); @@ -229,19 +224,26 @@ impl ActualCmd { print!("Will execute {}{}", self, confirm); stdout().flush().unwrap(); - let mut yes_no = String::new(); + if opt.quiet { + // Maybe we should only hide with the quiet option and not with yes? + // FIXME: stdin does not work with sudo? + cmd.stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()); + } + let mut user_continue = String::new(); if !opt.yes { std::io::stdin() - .read_line(&mut yes_no) + .read_line(&mut user_continue) .expect("Unable to read user’s input"); } - if yes_no.to_lowercase().starts_with('n') { + if user_continue.to_lowercase().starts_with('n') { eprintln!( - "yes_no? {} {}", - yes_no, - yes_no.to_lowercase().starts_with('n') + "user_continue? {} {}", + user_continue, + user_continue.to_lowercase().starts_with('n') ); return Ok(false); } diff --git a/src/lib.rs b/src/lib.rs index d34d18d..27f76cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,9 +23,11 @@ pub struct Opt { //#[arg(default_value_t = PathBuf::from("examples/debian.yml"))] pub config_file: PathBuf, - #[arg(short)] + #[arg(short, long)] pub yes: bool, - //pub quiet: bool, // imply yes + + #[arg(short, long)] + pub quiet: bool, #[arg(short, long)] pub steps: Option>, } diff --git a/src/main.rs b/src/main.rs index 6150b3b..626c084 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,10 @@ use clap::Parser; use system_updater::*; fn main() { - let opt = Opt::parse(); + let mut opt = Opt::parse(); + if opt.quiet { + opt.yes = true; + } run(&opt); }