Repository: starship/starship
File: src/modules/container.rs (Rust)

Code before:

    pub fn container_name(context: &Context) -> Option<String> {
        use crate::utils::context_path;

        if context_path(context, "/proc/vz").exists() && !context_path(context, "/proc/bc").exists()
        {
            // OpenVZ
            return Some("OpenVZ".into());
        }

        if context_path(context, "/run/host/container-manager").exists() {
            // OCI
            return Some("OCI".into());
        }

        if context_path(context, "/run/systemd/container").exists() {
            // systemd
            return Some("Systemd".into());
        }

        let container_env_path = context_path(context, "/run/.containerenv");

        if container_env_path.exists() {
            // podman and others

            let image_res = std::fs::File::open(container_env_path)
                .map(|f| {
                    let reader = std::io::BufReader::new(f);
                    reader.lines().find_map(|l| {
                        l.ok().and_then(|l| {
                            if l.starts_with("image=\"") {
                                let r = l.split_at(7).1;
                                let name = r.rfind('/').map(|n| r.split_at(n + 1).1);
                                Some(String::from(name.unwrap_or(r).trim_end_matches('"')))
                            } else {
                                None
                            }
                        })
                    })
                })
                .unwrap_or_else(|_| Some("podman".into()))
                .unwrap_or_else(|| "podman".into());

            return Some(image_res);
        }

        if context_path(context, "/.dockerenv").exists() {
            // docker
            return Some("Docker".into());
        }


Review comment (suggestion):
Not a blocker but it is recommended to use  [`utils::read_file`](https://github.com/starship/starship/blob/master/src/utils.rs#L13) for getting the contents of a file unless there is a very specific reason not to.

Code after:

    pub fn container_name(context: &Context) -> Option<String> {
        use crate::utils::context_path;

        if context_path(context, "/proc/vz").exists() && !context_path(context, "/proc/bc").exists()
        {
            // OpenVZ
            return Some("OpenVZ".into());
        }

        if context_path(context, "/run/host/container-manager").exists() {
            // OCI
            return Some("OCI".into());
        }

        if context_path(context, "/run/systemd/container").exists() {
            // systemd
            return Some("Systemd".into());
        }

        let container_env_path = context_path(context, "/run/.containerenv");

        if container_env_path.exists() {
            // podman and others

            let image_res = read_file(container_env_path)
                .map(|s| {
                    s.lines()
                        .find_map(|l| {
                            l.starts_with("image=\"").then(|| {
                                let r = l.split_at(7).1;
                                let name = r.rfind('/').map(|n| r.split_at(n + 1).1);
                                String::from(name.unwrap_or(r).trim_end_matches('"'))
                            })
                        })
                        .unwrap_or_else(|| "podman".into())
                })
                .unwrap_or_else(|_| "podman".into());

            return Some(image_res);
        }

        if context_path(context, "/.dockerenv").exists() {
            // docker
            return Some("Docker".into());
        }

        None
    }

    let mut module = context.new_module("container");