diff --git a/examples/debian.yml b/examples/debian.yml index b01ba9d..cd89727 100644 --- a/examples/debian.yml +++ b/examples/debian.yml @@ -12,6 +12,7 @@ systems: params: - apt - upgrade + yes: -y current_dir: null env: {} - install: diff --git a/src/command.rs b/src/command.rs index da48fbf..7cfeff5 100644 --- a/src/command.rs +++ b/src/command.rs @@ -69,19 +69,18 @@ impl Updater { .unwrap(); } - pub fn parse_config(opt: &Opt) -> Result { + pub fn from_config(opt: &Opt) -> Result { // let u = Updater::new(); // u.write_config(opt); let file = std::fs::read_to_string(&opt.config_file).unwrap(); Ok(serde_yaml::from_str(&file).unwrap()) - // TODO: } - pub fn update_all(&self) -> Result<()> { + pub fn update_all(&self, opt: &Opt) -> Result<()> { let mut errors = vec![]; for sys in &self.systems { - if let Err(err) = self.update(sys) { + if let Err(err) = self.update(sys, opt) { eprintln!("Error catched {}", err); errors.push(err); } @@ -98,15 +97,15 @@ impl Updater { } } - fn update(&self, sys: &System) -> Result<()> { + fn update(&self, sys: &System, opt: &Opt) -> Result<()> { if self.steps == UpdateSteps::Fetch { - sys.fetch()?; + sys.fetch(opt)?; } if self.steps == UpdateSteps::Compile { - sys.compile()?; + sys.compile(opt)?; } if self.steps == UpdateSteps::Install { - sys.install()?; + sys.install(opt)?; } Ok(()) @@ -114,22 +113,22 @@ impl Updater { } impl System { - pub fn fetch(&self) -> Result<()> { + pub fn fetch(&self, opt: &Opt) -> Result<()> { if let Some(fetch) = &self.fetch { - fetch.execute()?; + fetch.execute(opt)?; } Ok(()) } - pub fn compile(&self) -> Result<()> { + pub fn compile(&self, opt: &Opt) -> Result<()> { if let Some(compile) = &self.compile { - compile.execute()?; + compile.execute(opt)?; } Ok(()) } - pub fn install(&self) -> Result<()> { - self.install.execute()?; + pub fn install(&self, opt: &Opt) -> Result<()> { + self.install.execute(opt)?; Ok(()) } } @@ -144,7 +143,7 @@ impl Cmd { } } - fn execute(&self) -> Result<()> { + fn execute(&self, opt: &Opt) -> Result<()> { let mut cmd = Command::new(&self.exe); cmd.args(&self.params) .env_clear() @@ -158,15 +157,18 @@ impl Cmd { cmd.current_dir(std::fs::canonicalize(cdir).unwrap()); } - let confirm = "[Y/n]? "; + let confirm = if opt.yes { "\n" } else { "[Y/n]? " }; print!("Will execute {}{}", self, confirm); stdout().flush().unwrap(); let mut yes_no = String::new(); - std::io::stdin() - .read_line(&mut yes_no) - .expect("Unable to read user’s input"); + + if !opt.yes { + std::io::stdin() + .read_line(&mut yes_no) + .expect("Unable to read user’s input"); + } if !yes_no.to_lowercase().starts_with('n') { if cmd.status().unwrap().success() { diff --git a/src/lib.rs b/src/lib.rs index 8bdb826..0665b34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,10 +22,14 @@ pub struct Opt { // //#[arg(default_value_t = PathBuf::from("examples/debian.yml"))] pub config_file: PathBuf, + + #[arg(short)] + pub yes: bool, + //pub quiet: bool, // imply yes } pub fn run(opt: &Opt) { - let updater = Updater::parse_config(opt).unwrap(); + let updater = Updater::from_config(opt).unwrap(); - dbg!(updater).update_all().unwrap(); + updater.update_all(opt).unwrap(); }