Skip to content

Commit

Permalink
spec: Make status always required
Browse files Browse the repository at this point in the history
This avoids unnecessary digging through an `Option`, particularly
some `unwrap()` usage.

Signed-off-by: Colin Walters <[email protected]>
  • Loading branch information
cgwalters committed Oct 21, 2023
1 parent 083c1af commit 670f6ca
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,24 +286,29 @@ async fn upgrade(opts: UpgradeOpts) -> Result<()> {
let repo = &sysroot.repo();
let booted_deployment = &sysroot.require_booted_deployment()?;
let (_deployments, host) = crate::status::get_status(sysroot, Some(booted_deployment))?;
// SAFETY: There must be a status if we have a booted deployment
let status = host.status.unwrap();
let imgref = host.spec.image.as_ref();
// If there's no specified image, let's be nice and check if the booted system is using rpm-ostree
if imgref.is_none() && status.booted.as_ref().map_or(false, |b| b.incompatible) {
if imgref.is_none()
&& host
.status
.booted
.as_ref()
.map_or(false, |b| b.incompatible)
{
return Err(anyhow::anyhow!(
"Booted deployment contains local rpm-ostree modifications; cannot upgrade via bootc"
));
}
let spec = RequiredHostSpec::from_spec(&host.spec)?;
let booted_image = status
let booted_image = host
.status
.booted
.map(|b| b.query_image(repo))
.transpose()?
.flatten();
let imgref = imgref.ok_or_else(|| anyhow::anyhow!("No image source specified"))?;
// Find the currently queued digest, if any before we pull
let staged = status.staged.as_ref();
let staged = host.status.staged.as_ref();
let staged_image = staged.as_ref().and_then(|s| s.image.as_ref());
let mut changed = false;
if opts.check {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/privtests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ pub(crate) fn impl_run_container() -> Result<()> {
assert!(ostree_ext::container_utils::is_ostree_container()?);
let sh = Shell::new()?;
let host: Host = serde_yaml::from_str(&cmd!(sh, "bootc status").read()?)?;
let status = host.status.unwrap();
assert!(status.is_container);
assert!(host.status.is_container);
for c in ["upgrade", "update"] {
let o = Command::new("bootc").arg(c).output()?;
let st = o.status;
Expand Down
5 changes: 3 additions & 2 deletions lib/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ pub struct Host {
#[serde(default)]
pub spec: HostSpec,
/// The status
pub status: Option<HostStatus>,
#[serde(default)]
pub status: HostStatus,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -121,7 +122,7 @@ impl Host {
metadata,
},
spec,
status: None,
status: Default::default(),
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ pub(crate) fn get_status(
})
.unwrap_or_default();
let mut host = Host::new(OBJECT_NAME, spec);
host.status = Some(HostStatus {
host.status = HostStatus {
staged,
booted,
rollback,
is_container,
});
};
Ok((deployments, host))
}

Expand All @@ -244,7 +244,7 @@ pub(crate) async fn status(opts: super::cli::StatusOpts) -> Result<()> {
..Default::default()
};
let mut r = Host::new(OBJECT_NAME, HostSpec { image: None });
r.status = Some(status);
r.status = status;
r
} else {
let sysroot = super::cli::get_locked_sysroot().await?;
Expand Down

0 comments on commit 670f6ca

Please sign in to comment.