mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-04-07 01:31:50 +00:00
feat: Add configuration to set how much AWS profile info is shown (#556)
This commit is contained in:
parent
26fa4ec1ea
commit
fa1267f12f
@ -123,11 +123,12 @@ The `aws` module shows the current AWS region and profile. This is based on
|
|||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
| ---------- | --------------- | ---------------------------------------------------------- |
|
| ----------------- | --------------- | ----------------------------------------------------------------------------|
|
||||||
| `symbol` | `"☁️ "` | The symbol used before displaying the current AWS profile. |
|
| `symbol` | `"☁️ "` | The symbol used before displaying the current AWS profile. |
|
||||||
| `style` | `"bold yellow"` | The style for the module. |
|
| `style` | `"bold yellow"` | The style for the module. |
|
||||||
| `disabled` | `false` | Disables the `AWS` module. |
|
| `disabled` | `false` | Disables the `AWS` module. |
|
||||||
|
| `displayed_items` | `all` | Choose which item to display. Possible values: [`all`, `profile`, `region`] |
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
@ -137,6 +138,7 @@ The `aws` module shows the current AWS region and profile. This is based on
|
|||||||
[aws]
|
[aws]
|
||||||
style = "bold blue"
|
style = "bold blue"
|
||||||
symbol = "🅰 "
|
symbol = "🅰 "
|
||||||
|
displayed_items = "region"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Battery
|
## Battery
|
||||||
|
@ -3,6 +3,13 @@ use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
|
|||||||
use ansi_term::{Color, Style};
|
use ansi_term::{Color, Style};
|
||||||
use starship_module_config_derive::ModuleConfig;
|
use starship_module_config_derive::ModuleConfig;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
|
pub enum AwsItems {
|
||||||
|
All,
|
||||||
|
Region,
|
||||||
|
Profile,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, ModuleConfig)]
|
#[derive(Clone, ModuleConfig)]
|
||||||
pub struct AwsConfig<'a> {
|
pub struct AwsConfig<'a> {
|
||||||
pub symbol: SegmentConfig<'a>,
|
pub symbol: SegmentConfig<'a>,
|
||||||
@ -10,6 +17,7 @@ pub struct AwsConfig<'a> {
|
|||||||
pub region: SegmentConfig<'a>,
|
pub region: SegmentConfig<'a>,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
pub disabled: bool,
|
pub disabled: bool,
|
||||||
|
pub displayed_items: AwsItems,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
|
impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
|
||||||
@ -20,6 +28,18 @@ impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
|
|||||||
region: SegmentConfig::default(),
|
region: SegmentConfig::default(),
|
||||||
style: Color::Yellow.bold(),
|
style: Color::Yellow.bold(),
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
displayed_items: AwsItems::All,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ModuleConfig<'a> for AwsItems {
|
||||||
|
fn from_config(config: &toml::Value) -> Option<Self> {
|
||||||
|
match config.as_str()? {
|
||||||
|
"all" => Some(AwsItems::All),
|
||||||
|
"region" => Some(AwsItems::Region),
|
||||||
|
"profile" => Some(AwsItems::Profile),
|
||||||
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,12 @@ use dirs::home_dir;
|
|||||||
|
|
||||||
use super::{Context, Module, RootModuleConfig};
|
use super::{Context, Module, RootModuleConfig};
|
||||||
|
|
||||||
use crate::configs::aws::AwsConfig;
|
use crate::configs::aws::{AwsConfig, AwsItems};
|
||||||
|
|
||||||
fn get_aws_region_from_config(aws_profile: &Option<String>) -> Option<String> {
|
type Profile = String;
|
||||||
|
type Region = String;
|
||||||
|
|
||||||
|
fn get_aws_region_from_config(aws_profile: Option<&str>) -> Option<Region> {
|
||||||
let config_location = env::var("AWS_CONFIG_FILE")
|
let config_location = env::var("AWS_CONFIG_FILE")
|
||||||
.ok()
|
.ok()
|
||||||
.and_then(|path| PathBuf::from_str(&path).ok())
|
.and_then(|path| PathBuf::from_str(&path).ok())
|
||||||
@ -44,43 +47,38 @@ fn get_aws_region_from_config(aws_profile: &Option<String>) -> Option<String> {
|
|||||||
Some(region.to_string())
|
Some(region.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_aws_region() -> Option<(String, String)> {
|
fn get_aws_profile_and_region() -> (Option<Profile>, Option<Region>) {
|
||||||
env::var("AWS_DEFAULT_REGION")
|
match (
|
||||||
.ok()
|
env::var("AWS_PROFILE").ok(),
|
||||||
.map(|region| (String::new(), region))
|
env::var("AWS_REGION").ok(),
|
||||||
.or_else(|| {
|
env::var("AWS_DEFAULT_REGION").ok(),
|
||||||
let aws_profile = env::var("AWS_PROFILE").ok();
|
) {
|
||||||
let aws_region = get_aws_region_from_config(&aws_profile);
|
(Some(p), Some(_), Some(dr)) => (Some(p), Some(dr)),
|
||||||
|
(Some(p), Some(r), None) => (Some(p), Some(r)),
|
||||||
|
(None, Some(r), None) => (None, Some(r)),
|
||||||
|
(Some(p), None, Some(dr)) => (Some(p), Some(dr)),
|
||||||
|
(Some(ref p), None, None) => (Some(p.to_owned()), get_aws_region_from_config(Some(p))),
|
||||||
|
(None, None, Some(dr)) => (None, Some(dr)),
|
||||||
|
(None, Some(_), Some(dr)) => (None, Some(dr)),
|
||||||
|
(None, None, None) => (None, get_aws_region_from_config(None)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if aws_profile.is_none() && aws_region.is_none() {
|
fn get_aws_region() -> Option<Region> {
|
||||||
None
|
match (
|
||||||
} else {
|
env::var("AWS_REGION").ok(),
|
||||||
Some((
|
env::var("AWS_DEFAULT_REGION").ok(),
|
||||||
aws_profile.unwrap_or_default(),
|
) {
|
||||||
aws_region.unwrap_or_default(),
|
(Some(r), None) => Some(r),
|
||||||
))
|
(None, Some(dr)) => Some(dr),
|
||||||
}
|
(Some(_), Some(dr)) => Some(dr),
|
||||||
})
|
(None, None) => get_aws_region_from_config(None),
|
||||||
.or_else(|| {
|
}
|
||||||
env::var("AWS_REGION")
|
|
||||||
.ok()
|
|
||||||
.map(|region| (String::new(), region))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
const AWS_PREFIX: &str = "on ";
|
const AWS_PREFIX: &str = "on ";
|
||||||
|
|
||||||
let (aws_profile, aws_region) = get_aws_region()?;
|
|
||||||
if aws_profile.is_empty() && aws_region.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let aws_region = if aws_profile.is_empty() || aws_region.is_empty() {
|
|
||||||
aws_region
|
|
||||||
} else {
|
|
||||||
format!("({})", aws_region)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut module = context.new_module("aws");
|
let mut module = context.new_module("aws");
|
||||||
let config: AwsConfig = AwsConfig::try_load(module.config);
|
let config: AwsConfig = AwsConfig::try_load(module.config);
|
||||||
|
|
||||||
@ -89,8 +87,29 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
module.get_prefix().set_value(AWS_PREFIX);
|
module.get_prefix().set_value(AWS_PREFIX);
|
||||||
|
|
||||||
module.create_segment("symbol", &config.symbol);
|
module.create_segment("symbol", &config.symbol);
|
||||||
module.create_segment("profile", &config.profile.with_value(&aws_profile));
|
match config.displayed_items {
|
||||||
module.create_segment("region", &config.profile.with_value(&aws_region));
|
AwsItems::All => {
|
||||||
|
let (aws_profile, aws_region) = get_aws_profile_and_region();
|
||||||
|
|
||||||
|
let aws_segment = match (&aws_profile, &aws_region) {
|
||||||
|
(None, None) => return None,
|
||||||
|
(Some(p), Some(r)) => format!("{}({})", p, r),
|
||||||
|
(Some(p), None) => p.to_string(),
|
||||||
|
(None, Some(r)) => r.to_string(),
|
||||||
|
};
|
||||||
|
module.create_segment("all", &config.region.with_value(&aws_segment));
|
||||||
|
}
|
||||||
|
AwsItems::Profile => {
|
||||||
|
let aws_profile = env::var("AWS_PROFILE").ok()?;
|
||||||
|
|
||||||
|
module.create_segment("profile", &config.profile.with_value(&aws_profile));
|
||||||
|
}
|
||||||
|
AwsItems::Region => {
|
||||||
|
let aws_region = get_aws_region()?;
|
||||||
|
|
||||||
|
module.create_segment("region", &config.region.with_value(&aws_region));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Some(module)
|
Some(module)
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,11 @@ use std::io::{self, Write};
|
|||||||
use ansi_term::Color;
|
use ansi_term::Color;
|
||||||
use tempfile;
|
use tempfile;
|
||||||
|
|
||||||
use crate::common;
|
use crate::common::{self, TestCommand};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_region_set() -> io::Result<()> {
|
fn no_region_set() -> io::Result<()> {
|
||||||
let output = common::render_module("aws")
|
let output = common::render_module("aws")
|
||||||
.env_clear()
|
|
||||||
.env("PATH", env!("PATH"))
|
.env("PATH", env!("PATH"))
|
||||||
.output()?;
|
.output()?;
|
||||||
let expected = "";
|
let expected = "";
|
||||||
@ -21,7 +20,6 @@ fn no_region_set() -> io::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn region_set() -> io::Result<()> {
|
fn region_set() -> io::Result<()> {
|
||||||
let output = common::render_module("aws")
|
let output = common::render_module("aws")
|
||||||
.env_clear()
|
|
||||||
.env("AWS_REGION", "ap-northeast-2")
|
.env("AWS_REGION", "ap-northeast-2")
|
||||||
.output()?;
|
.output()?;
|
||||||
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ ap-northeast-2"));
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ ap-northeast-2"));
|
||||||
@ -33,7 +31,6 @@ fn region_set() -> io::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn default_region_set() -> io::Result<()> {
|
fn default_region_set() -> io::Result<()> {
|
||||||
let output = common::render_module("aws")
|
let output = common::render_module("aws")
|
||||||
.env_clear()
|
|
||||||
.env("AWS_REGION", "ap-northeast-2")
|
.env("AWS_REGION", "ap-northeast-2")
|
||||||
.env("AWS_DEFAULT_REGION", "ap-northeast-1")
|
.env("AWS_DEFAULT_REGION", "ap-northeast-1")
|
||||||
.output()?;
|
.output()?;
|
||||||
@ -46,7 +43,6 @@ fn default_region_set() -> io::Result<()> {
|
|||||||
#[test]
|
#[test]
|
||||||
fn profile_set() -> io::Result<()> {
|
fn profile_set() -> io::Result<()> {
|
||||||
let output = common::render_module("aws")
|
let output = common::render_module("aws")
|
||||||
.env_clear()
|
|
||||||
.env("AWS_PROFILE", "astronauts")
|
.env("AWS_PROFILE", "astronauts")
|
||||||
.output()?;
|
.output()?;
|
||||||
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ astronauts"));
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ astronauts"));
|
||||||
@ -55,6 +51,21 @@ fn profile_set() -> io::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_and_region_set() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_PROFILE", "astronauts")
|
||||||
|
.env("AWS_REGION", "ap-northeast-2")
|
||||||
|
.output()?;
|
||||||
|
let expected = format!(
|
||||||
|
"on {} ",
|
||||||
|
Color::Yellow.bold().paint("☁️ astronauts(ap-northeast-2)")
|
||||||
|
);
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn default_profile_set() -> io::Result<()> {
|
fn default_profile_set() -> io::Result<()> {
|
||||||
let dir = tempfile::tempdir()?;
|
let dir = tempfile::tempdir()?;
|
||||||
@ -72,7 +83,6 @@ region = us-east-2
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let output = common::render_module("aws")
|
let output = common::render_module("aws")
|
||||||
.env_clear()
|
|
||||||
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
||||||
.output()?;
|
.output()?;
|
||||||
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ us-east-1"));
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ us-east-1"));
|
||||||
@ -98,9 +108,11 @@ region = us-east-2
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let output = common::render_module("aws")
|
let output = common::render_module("aws")
|
||||||
.env_clear()
|
|
||||||
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
.env("AWS_CONFIG_FILE", config_path.to_string_lossy().as_ref())
|
||||||
.env("AWS_PROFILE", "astronauts")
|
.env("AWS_PROFILE", "astronauts")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
})
|
||||||
.output()?;
|
.output()?;
|
||||||
let expected = format!(
|
let expected = format!(
|
||||||
"on {} ",
|
"on {} ",
|
||||||
@ -110,3 +122,113 @@ region = us-east-2
|
|||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_and_region_set_with_display_all() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_PROFILE", "astronauts")
|
||||||
|
.env("AWS_REGION", "ap-northeast-1")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "all"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = format!(
|
||||||
|
"on {} ",
|
||||||
|
Color::Yellow.bold().paint("☁️ astronauts(ap-northeast-1)")
|
||||||
|
);
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_set_with_display_all() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_PROFILE", "astronauts")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "all"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ astronauts"));
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn region_set_with_display_all() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_REGION", "ap-northeast-1")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "all"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ ap-northeast-1"));
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_and_region_set_with_display_region() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_PROFILE", "astronauts")
|
||||||
|
.env("AWS_DEFAULT_REGION", "ap-northeast-1")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "region"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ ap-northeast-1"));
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn profile_and_region_set_with_display_profile() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_PROFILE", "astronauts")
|
||||||
|
.env("AWS_REGION", "ap-northeast-1")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "profile"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ astronauts"));
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn region_set_with_display_profile() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.env("AWS_REGION", "ap-northeast-1")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "profile"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = "";
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn region_not_set_with_display_region() -> io::Result<()> {
|
||||||
|
let output = common::render_module("aws")
|
||||||
|
.use_config(toml::toml! {
|
||||||
|
[aws]
|
||||||
|
displayed_items = "region"
|
||||||
|
})
|
||||||
|
.output()?;
|
||||||
|
let expected = "";
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user