From 87a206357abc771cdd8dc1266bff5cdb14890113 Mon Sep 17 00:00:00 2001 From: Zykino Date: Mon, 10 Jul 2023 22:18:17 +0200 Subject: [PATCH] Initialize the vector of steps once in main (with default) --- README.md | 2 ++ src/command.rs | 40 +++++++++++++++------------------------- src/lib.rs | 4 ++-- src/main.rs | 12 ++++++++++-- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index b5bc60f..5cafde8 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,8 @@ See also [topgrade](https://github.com/topgrade-rs/topgrade) for a solution that ## Nice to have * [ ] Being usable as a lib (I set the minimum possible in main.rs but the insterface have not been thought out) + * [ ] Being given a directory and/or a list of files + * [ ] Being given an already populated `Updater`’s struct * [ ] Have subcommands to interactively act on packagers’s config * [ ] List * [ ] Show diff --git a/src/command.rs b/src/command.rs index 9d98f72..4c47988 100644 --- a/src/command.rs +++ b/src/command.rs @@ -6,25 +6,13 @@ use std::path::PathBuf; use std::process::{Command, ExitStatus, Stdio}; use std::{fmt, fs, io}; -#[derive(Debug, Serialize, Deserialize, PartialEq)] +#[derive(Debug, Serialize, Deserialize, PartialEq, Copy, Clone)] pub enum UpdateSteps { PreInstall, Install, PostInstall, } -impl Display for UpdateSteps { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - UpdateSteps::PreInstall => write!(f, "PreInstall"), - UpdateSteps::Install => write!(f, "Install"), - UpdateSteps::PostInstall => write!(f, "PostInstall"), - } - } -} - -// TODO: change the struct’s names for the `topgrade`’s one. They are much better. - /// Root of the machine’s dependency graph #[derive(Debug, Serialize, Deserialize)] pub struct Updater { @@ -69,8 +57,8 @@ pub struct ActualCmd { env: BTreeMap, } -impl From<&String> for UpdateSteps { - fn from(value: &String) -> Self { +impl From for UpdateSteps { + fn from(value: String) -> Self { match value.to_lowercase().as_str() { "pre_install" => UpdateSteps::PreInstall, "install" => UpdateSteps::Install, @@ -81,6 +69,16 @@ impl From<&String> for UpdateSteps { } } +impl Display for UpdateSteps { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + UpdateSteps::PreInstall => write!(f, "pre_install"), + UpdateSteps::Install => write!(f, "install"), + UpdateSteps::PostInstall => write!(f, "post_install"), + } + } +} + pub fn get_packages_folder(opt: &Opt) -> io::Result { if let Some(p) = opt.config_folder.clone() { return Ok(p); @@ -229,16 +227,8 @@ impl Updater { } fn update(&self, sys: &Executor, opt: &Opt) -> Result<()> { - // TODO: compute once before calling this function, maybe? - let steps = if opt.steps.is_empty() { - vec![ - UpdateSteps::PreInstall, - UpdateSteps::Install, - UpdateSteps::PostInstall, - ] - } else { - opt.steps.iter().map(|u| u.into()).collect() - }; + let steps = &opt.steps; + assert!(!steps.is_empty()); if steps.contains(&UpdateSteps::PreInstall) { sys.pre_install(opt)?; diff --git a/src/lib.rs b/src/lib.rs index e67a3e4..7df7c60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -mod command; +pub mod command; pub mod errors; use command::*; @@ -18,7 +18,7 @@ pub struct Opt { pub quiet: bool, // TODO: use clap_verbosity_flag instead #[arg(short, long)] - pub steps: Vec, + pub steps: Vec, } enum Status { diff --git a/src/main.rs b/src/main.rs index 8d66e08..bfc6f58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,17 @@ -use system_updater::*; +use system_updater::{command::UpdateSteps, *}; use clap::Parser; fn main() -> std::io::Result<()> { - let opt = Opt::parse(); + let mut opt = Opt::parse(); + + if opt.steps.is_empty() { + opt.steps = vec![ + UpdateSteps::PreInstall, + UpdateSteps::Install, + UpdateSteps::PostInstall, + ] + }; let summary = run(&opt)?;