perf(pulumi): allow disabling upwards discovery (#4159)

* perf(pulumi): disable upwards discovery by default

* change `search_upwards` default to `true`
This commit is contained in:
David Knaack 2022-07-10 11:14:43 +02:00 committed by GitHub
parent aae1ed04ba
commit af15de93c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 1 deletions

View File

@ -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"
}
}
},

View File

@ -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

View File

@ -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,
}
}
}

View File

@ -31,6 +31,15 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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()
}
}