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:
Dario Vladović 2021-04-30 08:19:54 +02:00 committed by GitHub
parent d242d40db6
commit ff3c893a76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 21 deletions

View File

@ -1045,24 +1045,25 @@ This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gc
### Options
| Option | Default | Description |
| ---------------- | ---------------------------------------------- | --------------------------------------------------------------- |
| `format` | `'on [$symbol$account(\($region\))]($style) '` | The format for the module. |
| `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. |
| `region_aliases` | | Table of region aliases to display in addition to the GCP name. |
| `style` | `"bold blue"` | The style for the module. |
| `disabled` | `false` | Disables the `gcloud` module. |
| Option | Default | Description |
| ---------------- | -------------------------------------------------------- | --------------------------------------------------------------- |
| `format` | `'on [$symbol$account(@$domain)(\($region\))]($style) '` | The format for the module. |
| `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. |
| `region_aliases` | | Table of region aliases to display in addition to the GCP name. |
| `style` | `"bold blue"` | The style for the module. |
| `disabled` | `false` | Disables the `gcloud` module. |
### Variables
| Variable | Example | Description |
| -------- | ----------------- | ------------------------------------------------------------------ |
| region | `us-central1` | The current GCP region |
| account | `foo@example.com` | The current GCP profile |
| project | | The current GCP project |
| active | `default` | The active config name written in `~/.config/gcloud/active_config` |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |
| Variable | Example | Description |
| -------- | ------------- | ------------------------------------------------------------------ |
| region | `us-central1` | The current GCP region |
| account | `foo` | The current GCP profile |
| domain | `example.com` | The current GCP profile domain |
| project | | The current GCP project |
| active | `default` | The active config name written in `~/.config/gcloud/active_config` |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |
\*: This variable can only be used as a part of a style string
@ -1074,7 +1075,7 @@ This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gc
# ~/.config/starship.toml
[gcloud]
format = 'on [$symbol$account(\($project\))]($style) '
format = 'on [$symbol$account(@$domain)(\($project\))]($style) '
```
#### Display active config name only

View File

@ -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,

View File

@ -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()?;