From af15de93c4494bb08d8c2cb3dbf54951f6bc9239 Mon Sep 17 00:00:00 2001 From: David Knaack Date: Sun, 10 Jul 2022 11:14:43 +0200 Subject: [PATCH] perf(pulumi): allow disabling upwards discovery (#4159) * perf(pulumi): disable upwards discovery by default * change `search_upwards` default to `true` --- .github/config-schema.json | 5 ++++ docs/config/README.md | 3 ++- src/configs/pulumi.rs | 2 ++ src/modules/pulumi.rs | 52 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 5631940b..5a3247f7 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -1026,6 +1026,7 @@ "default": { "disabled": false, "format": "via [$symbol($username@)$stack]($style) ", + "search_upwards": true, "style": "bold 5", "symbol": " ", "version_format": "v${raw}" @@ -3831,6 +3832,10 @@ "disabled": { "default": false, "type": "boolean" + }, + "search_upwards": { + "default": true, + "type": "boolean" } } }, diff --git a/docs/config/README.md b/docs/config/README.md index 77c1927c..e8347891 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2700,7 +2700,7 @@ If you still want to enable it, [follow the example shown below](#with-pulumi-ve By default the module will be shown if any of the following conditions are met: - The current directory contains either `Pulumi.yaml` or `Pulumi.yml` -- A parent directory contains either `Pulumi.yaml` or `Pulumi.yml` +- A parent directory contains either `Pulumi.yaml` or `Pulumi.yml` unless `search_upwards` is set to `false` ### Options @@ -2710,6 +2710,7 @@ By default the module will be shown if any of the following conditions are met: | `version_format` | `"v${raw}"` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` | | `symbol` | `" "` | A format string shown before the Pulumi stack. | | `style` | `"bold 5"` | The style for the module. | +| `search_upwards` | `true` | Enable discovery of pulumi config files in parent directories. | | `disabled` | `false` | Disables the `pulumi` module. | ### Variables diff --git a/src/configs/pulumi.rs b/src/configs/pulumi.rs index d372c900..043cdab0 100644 --- a/src/configs/pulumi.rs +++ b/src/configs/pulumi.rs @@ -9,6 +9,7 @@ pub struct PulumiConfig<'a> { pub symbol: &'a str, pub style: &'a str, pub disabled: bool, + pub search_upwards: bool, } impl<'a> Default for PulumiConfig<'a> { @@ -19,6 +20,7 @@ impl<'a> Default for PulumiConfig<'a> { symbol: " ", style: "bold 5", disabled: false, + search_upwards: true, } } } diff --git a/src/modules/pulumi.rs b/src/modules/pulumi.rs index 63ef6352..5b2f975d 100644 --- a/src/modules/pulumi.rs +++ b/src/modules/pulumi.rs @@ -31,6 +31,15 @@ pub fn module<'a>(context: &'a Context) -> Option> { let mut module = context.new_module("pulumi"); let config = PulumiConfig::try_load(module.config); + let project_file_in_cwd = context + .try_begin_scan()? + .set_files(&["Pulumi.yaml", "Pulumi.yml"]) + .is_match(); + + if !project_file_in_cwd && !config.search_upwards { + return None; + } + let project_file = find_package_file(&context.logical_dir)?; let parsed = StringFormatter::new(config.format).and_then(|formatter| { @@ -409,4 +418,47 @@ mod tests { dir.close()?; Ok(()) } + + #[test] + fn do_not_search_upwards() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let child_dir = dir.path().join("child"); + std::fs::create_dir(&child_dir)?; + let yaml = File::create(dir.path().join("Pulumi.yaml"))?; + yaml.sync_all()?; + + let actual = ModuleRenderer::new("pulumi") + .path(&child_dir) + .logical_path(&child_dir) + .config(toml::toml! { + [pulumi] + format = "in [$symbol($stack)]($style) " + search_upwards = false + }) + .collect(); + let expected = None; + assert_eq!(expected, actual); + dir.close() + } + + #[test] + fn search_upwards_default() -> io::Result<()> { + let dir = tempfile::tempdir()?; + let child_dir = dir.path().join("child"); + std::fs::create_dir(&child_dir)?; + let yaml = File::create(dir.path().join("Pulumi.yaml"))?; + yaml.sync_all()?; + + let actual = ModuleRenderer::new("pulumi") + .path(&child_dir) + .logical_path(&child_dir) + .config(toml::toml! { + [pulumi] + format = "in [$symbol($stack)]($style) " + }) + .collect(); + let expected = Some(format!("in {} ", Color::Fixed(5).bold().paint(" "))); + assert_eq!(expected, actual); + dir.close() + } }