summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDario Vladović <d.vladimyr@gmail.com>2021-04-30 08:19:54 +0200
committerGitHub <noreply@github.com>2021-04-30 08:19:54 +0200
commitff3c893a76268efcd55e629cfa8e7587a9677067 (patch)
treebeb21c3fa658db0d21e6b63db1a2631798cb5514 /src
parentd242d40db61ca225be834b0747adf3f91a81bd03 (diff)
feat(gcloud): introduce separate `account` & `domain` format string variables (#2594)
* feat(gcloud): split gcloud profile into `account` & `domain` format variables * docs(gcloud): update documentation Co-authored-by: Filip Bachul <filip.b@masterborn.com>
Diffstat (limited to 'src')
-rw-r--r--src/configs/gcloud.rs2
-rw-r--r--src/modules/gcloud.rs55
2 files changed, 52 insertions, 5 deletions
diff --git a/src/configs/gcloud.rs b/src/configs/gcloud.rs
index 77b65900f..50aa04a68 100644
--- a/src/configs/gcloud.rs
+++ b/src/configs/gcloud.rs
@@ -15,7 +15,7 @@ pub struct GcloudConfig<'a> {
impl<'a> Default for GcloudConfig<'a> {
fn default() -> Self {
GcloudConfig {
- format: "on [$symbol$account(\\($region\\))]($style) ",
+ format: "on [$symbol$account(@$domain)(\\($region\\))]($style) ",
symbol: "☁️ ",
style: "bold blue",
disabled: false,
diff --git a/src/modules/gcloud.rs b/src/modules/gcloud.rs
index cb16a562f..fb2710b1d 100644
--- a/src/modules/gcloud.rs
+++ b/src/modules/gcloud.rs
@@ -1,4 +1,5 @@
-use once_cell::sync::OnceCell;
+use once_cell::sync::{Lazy, OnceCell};
+use std::ops::Deref;
use std::path::Path;
use std::path::PathBuf;
@@ -8,6 +9,8 @@ use crate::configs::gcloud::GcloudConfig;
use crate::formatter::StringFormatter;
use crate::utils;
+type Account = (String, Option<String>);
+
struct GcloudContext {
config_name: String,
config_path: PathBuf,
@@ -33,7 +36,7 @@ impl GcloudContext {
}
}
- pub fn get_account(&self) -> Option<String> {
+ pub fn get_account(&self) -> Option<Account> {
let config = self.get_config()?;
let account_line = config
.lines()
@@ -42,7 +45,11 @@ impl GcloudContext {
.take_while(|line| !line.starts_with('['))
.find(|line| line.starts_with("account"))?;
let account = account_line.splitn(2, '=').nth(1)?.trim();
- Some(account.to_string())
+ let mut segments = account.splitn(2, '@');
+ Some((
+ segments.next().map(String::from)?,
+ segments.next().map(String::from),
+ ))
}
pub fn get_project(&self) -> Option<String> {
@@ -105,6 +112,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let (config_name, config_path) = get_current_config(context)?;
let gcloud_context = GcloudContext::new(&config_name, &config_path);
+ let account: Lazy<Option<Account>, _> = Lazy::new(|| gcloud_context.get_account());
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
@@ -117,7 +125,16 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None,
})
.map(|variable| match variable {
- "account" => gcloud_context.get_account().map(Ok),
+ "account" => account
+ .deref()
+ .as_ref()
+ .map(|(account, _)| (*account).to_owned())
+ .map(Ok),
+ "domain" => account
+ .deref()
+ .as_ref()
+ .and_then(|(_, domain)| (*domain).to_owned())
+ .map(Ok),
"region" => gcloud_context
.get_region()
.map(|region| {
@@ -188,6 +205,36 @@ account = foo@example.com
}
#[test]
+ fn account_with_custom_format_set() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ let active_config_path = dir.path().join("active_config");
+ let mut active_config_file = File::create(&active_config_path)?;
+ active_config_file.write_all(b"default")?;
+
+ create_dir(dir.path().join("configurations"))?;
+ let config_default_path = dir.path().join("configurations").join("config_default");
+ let mut config_default_file = File::create(&config_default_path)?;
+ config_default_file.write_all(
+ b"\
+[core]
+account = foo@example.com
+",
+ )?;
+
+ let actual = ModuleRenderer::new("gcloud")
+ .env("CLOUDSDK_CONFIG", dir.path().to_string_lossy())
+ .config(toml::toml! {
+ [gcloud]
+ format = "on [$symbol$account(\\($region\\))]($style) "
+ })
+ .collect();
+ let expected = Some(format!("on {} ", Color::Blue.bold().paint("☁️ foo")));
+
+ assert_eq!(actual, expected);
+ dir.close()
+ }
+
+ #[test]
fn account_and_region_set() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let active_config_path = dir.path().join("active_config");