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

Code before:
        let os_info = BottlerocketRelease::new().context(error::ReleaseVersionSnafu)?;
        for update in updates {
            // If the current running version is greater than the max version ever published,
            // or moves us to a valid version <= the maximum version, update.
            // Updates are listed in descending order (in terms of versions) in the manifest,
            // so the first picked out would be the latest update available.
            if os_info.version_id < update.version || os_info.version_id > update.max_version {
                return Ok(Some(update));
            }
        }
        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: "/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,
                },
            )?;

Review comment (refactor):
It looks like the only setting we need in this program is `version-lock`.

It might be better to just put that one setting in `/etc/thar-be-updates.toml` so it's clear what is actually used.

```
[required-extensions]
updates = "v1"
std = { version = "v1", helpers = ["default"] }
+++
version-lock = {{default "latest" settings.updates.version-lock}}
```


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(),