1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 07:46:28 +00:00

feat(aws): add option to force AWS display (#3720)

* add option to force AWS display

Even if no credentials or credential_process have been setup

* change README wording

* Include sso_start_url in the description

* Change option name to force_display
This commit is contained in:
Alex Douze 2022-03-25 22:30:36 +01:00 committed by GitHub
parent 538329d9b4
commit e04f126a10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 11 deletions

View File

@ -274,7 +274,9 @@ format = "$all$directory$character"
## AWS
The `aws` module shows the current AWS region and profile when
credentials, a `credential_process` or a `sso_start_url` have been setup. This is based on
credentials, a `credential_process` or a `sso_start_url` have been setup. Alternatively, you can force this
module to show the region and profile event when the credentials have not been setup
with the `force_display` option. This is based on
`AWS_REGION`, `AWS_DEFAULT_REGION`, and `AWS_PROFILE` env var with
`~/.aws/config` file. This module also shows an expiration timer when using temporary
credentials.
@ -284,6 +286,8 @@ The module will display a profile only if its credentials are present in
`~/.aws/config`. Alternatively, having any of the `AWS_ACCESS_KEY_ID`,
`AWS_SECRET_ACCESS_KEY`, or `AWS_SESSION_TOKEN` env vars defined will
also suffice.
If the option `force_display` is set to `true`, all available information will be
displayed even if the conditions above are not respected.
When using [aws-vault](https://github.com/99designs/aws-vault) the profile
is read from the `AWS_VAULT` env var and the credentials expiration date
@ -299,7 +303,7 @@ date is read from the `AWSUME_EXPIRATION` env var.
### Options
| Option | Default | Description |
| ------------------- | ---------------------------------------------------------------- | ----------------------------------------------------------------- |
| ------------------- | ---------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| `format` | `'on [$symbol($profile )(\($region\) )(\[$duration\])]($style)'` | The format for the module. |
| `symbol` | `"☁️ "` | The symbol used before displaying the current AWS profile. |
| `region_aliases` | | Table of region aliases to display in addition to the AWS name. |
@ -307,6 +311,7 @@ date is read from the `AWSUME_EXPIRATION` env var.
| `style` | `"bold yellow"` | The style for the module. |
| `expiration_symbol` | `X` | The symbol displayed when the temporary credentials have expired. |
| `disabled` | `false` | Disables the `AWS` module. |
| `force_display` | `false` | If true displays info even if `credentials`, `credential_process` or `sso_start_url` have not been setup. |
### Variables

View File

@ -12,6 +12,7 @@ pub struct AwsConfig<'a> {
pub region_aliases: HashMap<String, &'a str>,
pub profile_aliases: HashMap<String, &'a str>,
pub expiration_symbol: &'a str,
pub force_display: bool,
}
impl<'a> Default for AwsConfig<'a> {
@ -24,6 +25,7 @@ impl<'a> Default for AwsConfig<'a> {
region_aliases: HashMap::new(),
profile_aliases: HashMap::new(),
expiration_symbol: "X",
force_display: false,
}
}
}

View File

@ -186,7 +186,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}
// only display if credential_process is defined or has valid credentials
if !has_credential_process_or_sso(context, aws_profile.as_ref())
if !config.force_display
&& !has_credential_process_or_sso(context, aws_profile.as_ref())
&& get_defined_credentials(context, aws_profile.as_ref()).is_none()
{
return None;
@ -787,6 +788,36 @@ region = us-east-2
dir.close()
}
#[test]
fn missing_any_credentials_but_display_empty() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let config_path = dir.path().join("config");
let mut file = File::create(&config_path)?;
file.write_all(
"[profile astronauts]
region = us-east-2
"
.as_bytes(),
)?;
let actual = ModuleRenderer::new("aws")
.config(toml::toml! {
[aws]
force_display = true
})
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
.env("AWS_PROFILE", "astronauts")
.collect();
let expected = Some(format!(
"on {}",
Color::Yellow.bold().paint("☁️ astronauts (us-east-2) ")
));
assert_eq!(expected, actual);
dir.close()
}
#[test]
fn access_key_credential_set() -> io::Result<()> {
let dir = tempfile::tempdir()?;