Initialize the vector of steps once in main (with default)

This commit is contained in:
Zykino 2023-07-10 22:18:17 +02:00
parent 167a634b4c
commit 87a206357a
4 changed files with 29 additions and 29 deletions

View File

@ -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 packagerss config
* [ ] List
* [ ] Show

View File

@ -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 structs names for the `topgrade`s one. They are much better.
/// Root of the machines dependency graph
#[derive(Debug, Serialize, Deserialize)]
pub struct Updater {
@ -69,8 +57,8 @@ pub struct ActualCmd {
env: BTreeMap<String, String>,
}
impl From<&String> for UpdateSteps {
fn from(value: &String) -> Self {
impl From<String> 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<PathBuf> {
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)?;

View File

@ -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<String>,
pub steps: Vec<UpdateSteps>,
}
enum Status {

View File

@ -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)?;