mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
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>
This commit is contained in:
@@ -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,
|
||||
|
||||
+51
-4
@@ -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| {
|
||||
@@ -187,6 +204,36 @@ account = foo@example.com
|
||||
dir.close()
|
||||
}
|
||||
|
||||
#[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()?;
|
||||
|
||||
Reference in New Issue
Block a user