Skip to content

Commit

Permalink
fix(rust): make all scripts types optional
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Nov 27, 2024
1 parent c1d5579 commit db21a62
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
12 changes: 6 additions & 6 deletions rust/agama-lib/src/scripts/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ use super::{Script, ScriptSource};
#[serde(rename_all = "camelCase")]
pub struct ScriptsConfig {
/// User-defined pre-installation scripts
#[serde(skip_serializing_if = "Vec::is_empty")]
pub pre: Vec<ScriptConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub pre: Option<Vec<ScriptConfig>>,
/// User-defined post-installation scripts
#[serde(skip_serializing_if = "Vec::is_empty")]
pub post: Vec<ScriptConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub post: Option<Vec<ScriptConfig>>,
/// User-defined init scripts
#[serde(skip_serializing_if = "Vec::is_empty")]
pub init: Vec<ScriptConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub init: Option<Vec<ScriptConfig>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
38 changes: 27 additions & 11 deletions rust/agama-lib/src/scripts/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,42 @@ impl ScriptsStore {
pub async fn load(&self) -> Result<ScriptsConfig, ServiceError> {
let scripts = self.client.scripts().await?;

let pre = Self::to_script_configs(&scripts, ScriptsGroup::Pre);
let post = Self::to_script_configs(&scripts, ScriptsGroup::Post);
let init = Self::to_script_configs(&scripts, ScriptsGroup::Init);

Ok(ScriptsConfig {
pre: Self::to_script_configs(&scripts, ScriptsGroup::Pre),
post: Self::to_script_configs(&scripts, ScriptsGroup::Post),
init: Self::to_script_configs(&scripts, ScriptsGroup::Init),
pre: if pre.is_empty() { None } else { Some(pre) },
post: if post.is_empty() { None } else { Some(post) },
init: if init.is_empty() { None } else { Some(init) },
})
}

pub async fn store(&self, settings: &ScriptsConfig) -> Result<(), ServiceError> {
self.client.delete_scripts().await?;

for pre in &settings.pre {
self.client
.add_script(&Self::to_script(pre, ScriptsGroup::Pre))
.await?;
if let Some(scripts) = &settings.pre {
for pre in scripts {
self.client
.add_script(&Self::to_script(pre, ScriptsGroup::Pre))
.await?;
}
}

if let Some(scripts) = &settings.post {
for post in scripts {
self.client
.add_script(&Self::to_script(post, ScriptsGroup::Post))
.await?;
}
}

for post in &settings.post {
self.client
.add_script(&Self::to_script(post, ScriptsGroup::Post))
.await?;
if let Some(scripts) = &settings.init {
for init in scripts {
self.client
.add_script(&Self::to_script(init, ScriptsGroup::Init))
.await?;
}
}

// TODO: find a better play to run the scripts (before probing).
Expand Down
6 changes: 3 additions & 3 deletions rust/agama-lib/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ impl Store {
pub async fn store(&self, settings: &InstallSettings) -> Result<(), ServiceError> {
if let Some(scripts) = &settings.scripts {
self.scripts.store(scripts).await?;
}

if settings.scripts.as_ref().is_some_and(|s| !s.pre.is_empty()) {
self.run_pre_scripts().await?;
if scripts.pre.as_ref().is_some_and(|s| !s.is_empty()) {
self.run_pre_scripts().await?;
}
}

if let Some(network) = &settings.network {
Expand Down

0 comments on commit db21a62

Please sign in to comment.