Repository: bottlerocket-os/bottlerocket
File: sources/api/host-containers/src/main.rs (Rust)

Code before:
        #[snafu(display("Failed to write user-data for host container '{}': {}", name, source))]
        UserDataWrite {
            name: String,
            source: std::io::Error,
        },

        #[snafu(display(
            "Failed to chmod host container '{}' storage directory: {}",
            name,
            source
        ))]
        SetPermissions {
            name: String,
            source: std::io::Error,
        },
    }
}

type Result<T> = std::result::Result<T, error::Error>;

/// Read the currently defined host containers from the config file
fn get_host_containers<P>(config_path: P) -> Result<HashMap<Identifier, config::HostContainer>>
where
    P: AsRef<Path>,
{
    debug!("Reading containers from the config file!");
    let config_path = config_path.as_ref();
    let config = std::fs::read_to_string(config_path).context(error::ReadConfigSnafu {
        config_file: format!("{:?}", config_path),
    })?;
    let config: config::HostContainersConfig =
        toml::from_str(&config).context(error::ConfigTomlSnafu {
            config_file: format!("{:?}", config_path),
        })?;

    // If host containers aren't defined, return an empty map
    Ok(config.host_containers.unwrap_or_default())
}

/// SystemdUnit stores the systemd unit being manipulated
struct SystemdUnit<'a> {
    unit: &'a str,
}

impl<'a> SystemdUnit<'a> {
    fn new(unit: &'a str) -> Self {
        SystemdUnit { unit }
    }

    fn is_active(&self) -> Result<bool> {
        match command(constants::SYSTEMCTL_BIN, ["is-active", self.unit]) {

Review comment (bug):
More information is better when debugging.

```suggestion
    let config_path = config_path.as_ref();
    debug!("Reading containers from the config file: {}", config_path.display());
```

Code after:
        #[snafu(display("Failed to write user-data for host container '{}': {}", name, source))]
        UserDataWrite {
            name: String,
            source: std::io::Error,
        },

        #[snafu(display(
            "Failed to chmod host container '{}' storage directory: {}",
            name,
            source
        ))]
        SetPermissions {
            name: String,
            source: std::io::Error,
        },
    }
}

type Result<T> = std::result::Result<T, error::Error>;

/// Read the currently defined host containers from the config file
fn get_host_containers<P>(config_path: P) -> Result<HashMap<Identifier, config::HostContainer>>
where
    P: AsRef<Path>,
{
    let config_path = config_path.as_ref();
    debug!(
        "Reading containers from the config file: {}",
        config_path.display()
    );
    let config = std::fs::read_to_string(config_path).context(error::ReadConfigSnafu {
        config_file: format!("{:?}", config_path),
    })?;
    let config: config::HostContainersConfig =
        toml::from_str(&config).context(error::ConfigTomlSnafu {
            config_file: format!("{:?}", config_path),
        })?;

    // If host containers aren't defined, return an empty map
    Ok(config.host_containers.unwrap_or_default())
}

/// SystemdUnit stores the systemd unit being manipulated
struct SystemdUnit<'a> {
    unit: &'a str,
}

impl<'a> SystemdUnit<'a> {
    fn new(unit: &'a str) -> Self {
        SystemdUnit { unit }
    }