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

Code before:
    let replaced = re.replace(value, replacement.as_str());
    match replaced {
        Cow::Owned(replaced) => Some(replaced),
        // It didn't match...
        _ => None,
    }
}

pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("kubernetes");
    let config: KubernetesConfig = KubernetesConfig::try_load(module.config);

    // As we default to disabled=true, we have to check here after loading our config module,
    // before it was only checking against whatever is in the config starship.toml
    if config.disabled {
        return None;
    };

    // We check that detect_env_vars is not empty here first since calling any on an amptry
    // iternator returns false, since we want to preserve backwards compatibiliTy we default to
    // being enabled.
    let have_env_vars = !config.detect_env_vars.is_empty()
        && config
            .detect_env_vars
            .iter()
            .any(|e| context.get_env(e).is_some());

    // If we have some config for doing the directory scan then we use it but if we don't then we
    // assume we should treat it like the module is enabled to preserve backward compatibility.
    let have_scan_config = [
        &config.detect_files,
        &config.detect_folders,
        &config.detect_extensions,
    ]
    .into_iter()
    .any(|v| !v.is_empty());

    let is_kube_project = have_scan_config.then(|| {
        context.try_begin_scan().map_or(false, |scanner| {
            scanner
                .set_files(&config.detect_files)
                .set_folders(&config.detect_folders)
                .set_extensions(&config.detect_extensions)
                .is_match()
        })
    });

    if !is_kube_project.unwrap_or(true) && !have_env_vars {
        return None;
    }


Review comment (refactor):
This has a helper in `Context` now:
```suggestion
    let have_env_vars = context.detect_env_vars(&config.detect_env_vars);
```

Code after:
    let replaced = re.replace(value, replacement.as_str());
    match replaced {
        Cow::Owned(replaced) => Some(replaced),
        // It didn't match...
        _ => None,
    }
}

pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
    let mut module = context.new_module("kubernetes");
    let config: KubernetesConfig = KubernetesConfig::try_load(module.config);

    // As we default to disabled=true, we have to check here after loading our config module,
    // before it was only checking against whatever is in the config starship.toml
    if config.disabled {
        return None;
    };

    let have_env_vars = context.detect_env_vars(&config.detect_env_vars);

    // If we have some config for doing the directory scan then we use it but if we don't then we
    // assume we should treat it like the module is enabled to preserve backward compatibility.
    let have_scan_config = [
        &config.detect_files,
        &config.detect_folders,
        &config.detect_extensions,
    ]
    .into_iter()
    .any(|v| !v.is_empty());

    let is_kube_project = have_scan_config.then(|| {
        context.try_begin_scan().map_or(false, |scanner| {
            scanner
                .set_files(&config.detect_files)
                .set_folders(&config.detect_folders)
                .set_extensions(&config.detect_extensions)
                .is_match()
        })
    });

    if !is_kube_project.unwrap_or(true) && !have_env_vars {
        return None;
    }

    let default_config_file = context.get_home()?.join(".kube").join("config");

    let kube_cfg = context
        .get_env("KUBECONFIG")
        .unwrap_or(default_config_file.to_str()?.to_string());

    let current_kube_ctx_name =