feat: Implement AWS region aliases (#646)

This commit is contained in:
Mike Sampson
2019-12-21 04:30:47 +11:00
committed by Matan Kushner
parent f3784f5aaa
commit 256a2be949
5 changed files with 53 additions and 6 deletions
+17
View File
@@ -3,6 +3,7 @@ use crate::utils;
use ansi_term::{Color, Style};
use std::clone::Clone;
use std::collections::HashMap;
use std::marker::Sized;
use dirs::home_dir;
@@ -127,6 +128,22 @@ where
}
}
impl<'a, T, S: ::std::hash::BuildHasher + Default> ModuleConfig<'a> for HashMap<String, T, S>
where
T: ModuleConfig<'a>,
S: Clone,
{
fn from_config(config: &'a Value) -> Option<Self> {
let mut hm = HashMap::default();
for (x, y) in config.as_table()?.iter() {
hm.insert(x.clone(), T::from_config(y)?);
}
Some(hm)
}
}
impl<'a, T> ModuleConfig<'a> for Option<T>
where
T: ModuleConfig<'a> + Sized,
+3
View File
@@ -1,4 +1,5 @@
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use std::collections::HashMap;
use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
@@ -18,6 +19,7 @@ pub struct AwsConfig<'a> {
pub style: Style,
pub disabled: bool,
pub displayed_items: AwsItems,
pub region_aliases: HashMap<String, &'a str>,
}
impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
@@ -29,6 +31,7 @@ impl<'a> RootModuleConfig<'a> for AwsConfig<'a> {
style: Color::Yellow.bold(),
disabled: false,
displayed_items: AwsItems::All,
region_aliases: HashMap::new(),
}
}
}
+11 -3
View File
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
@@ -76,6 +77,13 @@ fn get_aws_region() -> Option<Region> {
}
}
fn alias_region(region: &str, aliases: &HashMap<String, &str>) -> String {
match aliases.get(region) {
None => region.to_string(),
Some(alias) => alias.to_string(),
}
}
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
const AWS_PREFIX: &str = "on ";
@@ -93,9 +101,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let aws_segment = match (&aws_profile, &aws_region) {
(None, None) => return None,
(Some(p), Some(r)) => format!("{}({})", p, r),
(Some(p), Some(r)) => format!("{}({})", p, alias_region(r, &config.region_aliases)),
(Some(p), None) => p.to_string(),
(None, Some(r)) => r.to_string(),
(None, Some(r)) => alias_region(r, &config.region_aliases),
};
module.create_segment("all", &config.region.with_value(&aws_segment));
}
@@ -105,7 +113,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
module.create_segment("profile", &config.profile.with_value(&aws_profile));
}
AwsItems::Region => {
let aws_region = get_aws_region()?;
let aws_region = alias_region(&get_aws_region()?, &config.region_aliases);
module.create_segment("region", &config.region.with_value(&aws_region));
}