From fe57deac0b4fa3113edd01e8b949f2159d225938 Mon Sep 17 00:00:00 2001 From: eatradish Date: Wed, 7 Aug 2024 16:29:11 +0800 Subject: [PATCH] feat: add `force_use_apt` option to `CielConfig` --- src/actions/container.rs | 25 +++++++++++++++++++------ src/config.rs | 11 +++++++++++ src/main.rs | 3 ++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/actions/container.rs b/src/actions/container.rs index 18356c5..05375af 100644 --- a/src/actions/container.rs +++ b/src/actions/container.rs @@ -376,19 +376,32 @@ pub fn remove_instance(instance: &str) -> Result<()> { } /// Update AOSC OS in the container/instance -pub fn update_os() -> Result<()> { +pub fn update_os(force_use_apt: bool) -> Result<()> { info!("Updating base OS..."); let instance = format!("update-{:x}", random::()); add_instance(&instance)?; - let mut status = run_in_container(&instance, &["/bin/bash", "-ec", OMA_UPDATE_SCRIPT])?; + + if force_use_apt { + return apt_update_os(&instance); + } + + let status = run_in_container(&instance, &["/bin/bash", "-ec", OMA_UPDATE_SCRIPT])?; if status != 0 { - status = run_in_container(&instance, &["/bin/bash", "-ec", APT_UPDATE_SCRIPT])?; - if status != 0 { - return Err(anyhow!("Failed to update OS: {}", status)); - } + return apt_update_os(&instance); } + commit_container(&instance)?; remove_instance(&instance)?; Ok(()) } + +fn apt_update_os(instance: &str) -> Result<()> { + let status = run_in_container(instance, &["/bin/bash", "-ec", APT_UPDATE_SCRIPT])?; + + if status != 0 { + return Err(anyhow!("Failed to update OS: {}", status)); + } + + Ok(()) +} diff --git a/src/config.rs b/src/config.rs index 0de52ee..a44df37 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,9 +34,15 @@ pub struct CielConfig { pub sep_mount: bool, #[serde(rename = "volatile-mount", default)] pub volatile_mount: bool, + #[serde(default = "CielConfig::default_force_use_apt")] + pub force_use_apt: bool, } impl CielConfig { + const fn default_force_use_apt() -> bool { + false + } + pub fn save_config(&self) -> Result { Ok(toml::to_string(self)?) } @@ -58,6 +64,7 @@ impl Default for CielConfig { extra_options: Vec::new(), sep_mount: true, volatile_mount: false, + force_use_apt: false, } } } @@ -185,6 +192,10 @@ pub fn ask_for_config(config: Option) -> Result { .with_prompt("Use volatile mode for filesystem operations") .default(config.volatile_mount) .interact()?; + config.force_use_apt = Confirm::with_theme(&theme) + .with_prompt("Use apt as package manager") + .default(config.force_use_apt) + .interact()?; Ok(config) } diff --git a/src/main.rs b/src/main.rs index f9f8685..b46119d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ mod repo; use anyhow::{anyhow, bail, Context, Result}; use clap::ArgMatches; +use config::read_config; use console::{style, user_attended}; use dotenvy::dotenv; use std::process; @@ -218,7 +219,7 @@ fn main() -> Result<()> { }); } ("update-os", _) => { - print_error!({ actions::update_os() }); + print_error!({ actions::update_os(read_config().is_ok_and(|x| x.force_use_apt)) }); } ("config", args) => { if args.get_flag("g") {