Skip to content

Commit

Permalink
feat(aws): Add profile aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
Anders Eurenius Runvald authored and aes committed Mar 8, 2022
1 parent 966f605 commit 4cb00ec
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/configs/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct AwsConfig<'a> {
pub style: &'a str,
pub disabled: bool,
pub region_aliases: HashMap<String, &'a str>,
pub profile_aliases: HashMap<String, &'a str>,
pub expiration_symbol: &'a str,
}

Expand All @@ -21,6 +22,7 @@ impl<'a> Default for AwsConfig<'a> {
style: "bold yellow",
disabled: false,
region_aliases: HashMap::new(),
profile_aliases: HashMap::new(),
expiration_symbol: "X",
}
}
Expand Down
26 changes: 16 additions & 10 deletions src/modules/aws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ fn get_credentials_duration(context: &Context, aws_profile: Option<&Profile>) ->
Some(expiration_date.timestamp() - chrono::Local::now().timestamp())
}

fn alias_region(region: String, aliases: &HashMap<String, &str>) -> String {
match aliases.get(&region) {
None => region,
fn alias_name(name: String, aliases: &HashMap<String, &str>) -> String {
match aliases.get(&name) {
None => name,
Some(alias) => (*alias).to_string(),
}
}
Expand Down Expand Up @@ -187,12 +187,6 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
return None;
}

let mapped_region = if let Some(aws_region) = aws_region {
Some(alias_region(aws_region, &config.region_aliases))
} else {
None
};

let duration = {
get_credentials_duration(context, aws_profile.as_ref()).map(|duration| {
if duration > 0 {
Expand All @@ -203,6 +197,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
})
};

let mapped_region = if let Some(aws_region) = aws_region {
Some(alias_name(aws_region, &config.region_aliases))
} else {
None
};

let mapped_profile = if let Some(aws_profile) = aws_profile {
Some(alias_name(aws_profile, &config.profile_aliases))
} else {
None
};

let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
Expand All @@ -214,7 +220,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
"profile" => aws_profile.as_ref().map(Ok),
"profile" => mapped_profile.as_ref().map(Ok),
"region" => mapped_region.as_ref().map(Ok),
"duration" => duration.as_ref().map(Ok),
_ => None,
Expand Down

0 comments on commit 4cb00ec

Please sign in to comment.