Skip to content

Commit

Permalink
feat: add force_use_apt option to CielConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed Aug 7, 2024
1 parent 5a4c4ac commit fe57dea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
25 changes: 19 additions & 6 deletions src/actions/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>());
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(())
}
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
Ok(toml::to_string(self)?)
}
Expand All @@ -58,6 +64,7 @@ impl Default for CielConfig {
extra_options: Vec::new(),
sep_mount: true,
volatile_mount: false,
force_use_apt: false,
}
}
}
Expand Down Expand Up @@ -185,6 +192,10 @@ pub fn ask_for_config(config: Option<CielConfig>) -> Result<CielConfig> {
.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)
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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") {
Expand Down

0 comments on commit fe57dea

Please sign in to comment.