Skip to content

Commit 3f9da8e

Browse files
authored
Adapt agama config to return error code when no validates (#3304)
## Problem When running `agama config load`, `agama config generate` or `agama config validate` it always returns `0` even when the profile is invalid. - [*bsc#1256951*](https://bugzilla.suse.com/show_bug.cgi?id=1256951) ## Solution Return Err (`return code 1`) in case of invalid profile. ## Testing - *Tested manually* ### agama config generate ```bash suse@vikingo-laptop:~$ ~/agama_not_fix/agama config generate profile.jsonnet || echo $? { "hostname": { "static": "Agama" }, "product": { "ID": "Tumbleweed" }, "scripts": { "post": [ { "chroot": true, "content": "#!/usr/bin/env bash\necho 'PermitRootLogin yes' > /etc/ssh/sshd_config.d/root.conf\nsystemctl enable sshd\n", "name": "enable root login" } ] } } ✗ The profile is not valid. Please, check the following errors: * Additional properties are not allowed ('ID' was unexpected). /product * "id" is a required property. /product suse@vikingo-laptop:~$ ~/agama_fix/agama config generate profile.jsonnet || echo $? { "hostname": { "static": "Agama" }, "product": { "ID": "Tumbleweed" }, "scripts": { "post": [ { "chroot": true, "content": "#!/usr/bin/env bash\necho 'PermitRootLogin yes' > /etc/ssh/sshd_config.d/root.conf\nsystemctl enable sshd\n", "name": "enable root login" } ] } } ✗ The profile is not valid. Please, check the following errors: * Additional properties are not allowed ('ID' was unexpected). /product * "id" is a required property. /product The profile is not valid 1 ``` ### agama config load ```bash suse@vikingo-laptop:~$ ~/agama_not_fix/agama config load profile.json || echo $? ✗ The profile is not valid. Please, check the following errors: * Additional properties are not allowed ('ID' was unexpected). /product * "id" is a required property. /product suse@vikingo-laptop:~$ ~/agama_fix/agama config load profile.json || echo $? ✗ The profile is not valid. Please, check the following errors: * Additional properties are not allowed ('ID' was unexpected). /product * "id" is a required property. /product The profile is not valid 1 ``` ### agama config validate ```bash suse@vikingo-laptop:~$ ~/agama_not_fix/agama config validate profile.json || echo $? ✗ The profile is not valid. Please, check the following errors: * Additional properties are not allowed ('ID' was unexpected). /product * "id" is a required property. /product suse@vikingo-laptop:~$ ~/agama_fix/agama config validate profile.json || echo $? ✗ The profile is not valid. Please, check the following errors: * Additional properties are not allowed ('ID' was unexpected). /product * "id" is a required property. /product The profile is not valid 1 ```
2 parents de76056 + cb7a925 commit 3f9da8e

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

rust/agama-cli/src/config.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ pub async fn run(subcommand: ConfigCommands, opts: GlobalOpts) -> anyhow::Result
129129
let valid = validate(&http_client, CliInput::Full(contents.clone()), false).await?;
130130

131131
if !matches!(valid, ValidationOutcome::Valid) {
132-
return Ok(());
132+
return Err(anyhow!("The profile is not valid"));
133133
}
134134

135135
let model: api::Config = serde_json::from_str(&contents)?;
@@ -138,12 +138,16 @@ pub async fn run(subcommand: ConfigCommands, opts: GlobalOpts) -> anyhow::Result
138138
monitor_progress(monitor).await?;
139139
}
140140
ConfigCommands::Validate { url_or_path, local } => {
141-
let _ = if !local {
141+
let validity = if !local {
142142
let http_client = build_http_client(api_url, opts.insecure, true).await?;
143-
validate(&http_client, url_or_path, false).await
143+
validate(&http_client, url_or_path, false).await?
144144
} else {
145-
validate_local(url_or_path, opts.insecure)
145+
validate_local(url_or_path, opts.insecure)?
146146
};
147+
148+
if !matches!(validity, ValidationOutcome::Valid) {
149+
return Err(anyhow!("The profile is not valid"));
150+
}
147151
}
148152
ConfigCommands::Generate { url_or_path } => {
149153
let http_client = build_http_client(api_url, opts.insecure, true).await?;
@@ -293,7 +297,7 @@ async fn generate(
293297
println!("{}", &profile_json);
294298
let _ = validation_msg(&validity);
295299

296-
return Ok(());
300+
return Err(anyhow!("The profile is not valid"));
297301
}
298302

299303
let config = api::Config::from_json(&profile_json, &context.source)?;
@@ -303,10 +307,9 @@ async fn generate(
303307
let validity = validate(client, CliInput::Full(config_json.clone()), false).await?;
304308

305309
if matches!(validity, ValidationOutcome::NotValid(_)) {
306-
eprintln!(
307-
"{} Internal error: the profile was made invalid by InstallSettings round trip",
308-
style("\u{2717}").bold().red()
309-
);
310+
return Err(anyhow!(
311+
"Internal error: the profile became invalid during Config round trip"
312+
));
310313
}
311314

312315
Ok(())
@@ -364,10 +367,14 @@ async fn edit(
364367
let status = command.status().context(format!("Running {:?}", command))?;
365368
// TODO: do nothing if the content of the file is unchanged
366369
if status.success() {
367-
// FIXME: invalid profile still gets loaded
368370
let updated =
369371
std::fs::read_to_string(&path).context(format!("Reading from file {:?}", path))?;
370-
validate(http_client, CliInput::Full(updated.clone()), false).await?;
372+
let validity = validate(http_client, CliInput::Full(updated.clone()), false).await?;
373+
374+
if !matches!(validity, ValidationOutcome::Valid) {
375+
return Err(anyhow!("The profile is not valid"));
376+
}
377+
371378
return Ok(serde_json::from_str(&updated)?);
372379
}
373380

rust/package/agama.changes

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
-------------------------------------------------------------------
2+
Fri Mar 20 08:46:19 UTC 2026 - Knut Anderssen <kanderssen@suse.com>
3+
4+
- Return error code when using "agama config generate",
5+
"agama config load" or "agama config validate" does not validate
6+
the given profile (bsc#1256951, gh#agama-project/agama#3304).
7+
18
-------------------------------------------------------------------
29
Fri Mar 20 07:54:46 UTC 2026 - Knut Anderssen <kanderssen@suse.com>
310

0 commit comments

Comments
 (0)