Repository: bottlerocket-os/bottlerocket
File: sources/api/thar-be-updates/src/status.rs (Rust)

Code before:
    pub fn update_available_updates(
        &mut self,
        config_path: &str,
        updates: Vec<update_metadata::Update>,
    ) -> Result<bool> {
        // Extract the version to store
        self.available_updates = updates.iter().map(|u| u.version.to_owned()).collect();
        // Check if the 'version-lock'ed update is available as the 'chosen' update
        // Retrieve the 'version-lock' setting
        let settings = get_settings(config_path)?;
        let locked_version: FriendlyVersion =
            settings
                .version_lock
                .context(error::GetSettingOptionSnafu {
                    setting: "/settings/updates/version-lock",
                })?;

        if locked_version == "latest" {
            // Set chosen_update to the latest version available
            if let Some(latest_update) = UpdateStatus::get_latest_update(updates)? {
                self.chosen_update = Some(UpdateImage {
                    arch: latest_update.arch,
                    version: latest_update.version,
                    variant: latest_update.variant,
                });
                return Ok(true);
            }
        } else {
            let chosen_version = FriendlyVersion::try_into(locked_version.to_owned()).context(
                error::SemVerSnafu {
                    version: locked_version,
                },
            )?;
            let os_info = BottlerocketRelease::new().context(error::ReleaseVersionSnafu)?;
            if chosen_version != os_info.version_id {
                for update in &updates {
                    if update.version == chosen_version {
                        self.chosen_update = Some(UpdateImage {
                            arch: update.arch.clone(),
                            version: chosen_version,
                            variant: update.variant.clone(),
                        });
                        return Ok(true);
                    }
                }
            }
        }
        // 'version-lock'ed update is unavailable.
        self.chosen_update = None;
        Ok(false)
    }

Review comment (suggestion):
`/settings/updates/version-lock` is kind of confusing context if we're not making a web call to get that value.

Code after:
            }
        }
        Ok(None)
    }

    /// Checks the list of updates to for an available update.
    /// If the 'version-lock'ed version is available returns true. Otherwise returns false
    pub fn update_available_updates<P>(
        &mut self,
        config_path: P,
        updates: Vec<update_metadata::Update>,
    ) -> Result<bool>
    where
        P: AsRef<Path>,
    {
        // Extract the version to store
        self.available_updates = updates.iter().map(|u| u.version.to_owned()).collect();
        // Check if the 'version-lock'ed update is available as the 'chosen' update
        // Retrieve the 'version-lock' setting
        let settings = get_settings(config_path)?;
        let locked_version: FriendlyVersion =
            settings
                .version_lock
                .context(error::GetSettingOptionSnafu {
                    setting: "version-lock",
                })?;

        if locked_version == "latest" {
            // Set chosen_update to the latest version available
            if let Some(latest_update) = UpdateStatus::get_latest_update(updates)? {
                self.chosen_update = Some(UpdateImage {
                    arch: latest_update.arch,
                    version: latest_update.version,
                    variant: latest_update.variant,
                });
                return Ok(true);
            }
        } else {
            let chosen_version = FriendlyVersion::try_into(locked_version.to_owned()).context(
                error::SemVerSnafu {
                    version: locked_version,
                },
            )?;
            let os_info = BottlerocketRelease::new().context(error::ReleaseVersionSnafu)?;
            if chosen_version != os_info.version_id {
                for update in &updates {
                    if update.version == chosen_version {
                        self.chosen_update = Some(UpdateImage {
                            arch: update.arch.clone(),
                            version: chosen_version,
                            variant: update.variant.clone(),