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:
parent
d242d40db6
commit
ff3c893a76
|
@ -1045,24 +1045,25 @@ This is based on the `~/.config/gcloud/active_config` file and the `~/.config/gc
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Option | Default | Description |
|
| Option | Default | Description |
|
||||||
| ---------------- | ---------------------------------------------- | --------------------------------------------------------------- |
|
| ---------------- | -------------------------------------------------------- | --------------------------------------------------------------- |
|
||||||
| `format` | `'on [$symbol$account(\($region\))]($style) '` | The format for the module. |
|
| `format` | `'on [$symbol$account(@$domain)(\($region\))]($style) '` | The format for the module. |
|
||||||
| `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. |
|
| `symbol` | `"☁️ "` | The symbol used before displaying the current GCP profile. |
|
||||||
| `region_aliases` | | Table of region aliases to display in addition to the GCP name. |
|
| `region_aliases` | | Table of region aliases to display in addition to the GCP name. |
|
||||||
| `style` | `"bold blue"` | The style for the module. |
|
| `style` | `"bold blue"` | The style for the module. |
|
||||||
| `disabled` | `false` | Disables the `gcloud` module. |
|
| `disabled` | `false` | Disables the `gcloud` module. |
|
||||||
|
|
||||||
### Variables
|
### Variables
|
||||||
|
|
||||||
| Variable | Example | Description |
|
| Variable | Example | Description |
|
||||||
| -------- | ----------------- | ------------------------------------------------------------------ |
|
| -------- | ------------- | ------------------------------------------------------------------ |
|
||||||
| region | `us-central1` | The current GCP region |
|
| region | `us-central1` | The current GCP region |
|
||||||
| account | `foo@example.com` | The current GCP profile |
|
| account | `foo` | The current GCP profile |
|
||||||
| project | | The current GCP project |
|
| domain | `example.com` | The current GCP profile domain |
|
||||||
| active | `default` | The active config name written in `~/.config/gcloud/active_config` |
|
| project | | The current GCP project |
|
||||||
| symbol | | Mirrors the value of option `symbol` |
|
| active | `default` | The active config name written in `~/.config/gcloud/active_config` |
|
||||||
| style\* | | Mirrors the value of option `style` |
|
| 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
|
\*: 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
|
# ~/.config/starship.toml
|
||||||
|
|
||||||
[gcloud]
|
[gcloud]
|
||||||
format = 'on [$symbol$account(\($project\))]($style) '
|
format = 'on [$symbol$account(@$domain)(\($project\))]($style) '
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Display active config name only
|
#### Display active config name only
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub struct GcloudConfig<'a> {
|
||||||
impl<'a> Default for GcloudConfig<'a> {
|
impl<'a> Default for GcloudConfig<'a> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
GcloudConfig {
|
GcloudConfig {
|
||||||
format: "on [$symbol$account(\\($region\\))]($style) ",
|
format: "on [$symbol$account(@$domain)(\\($region\\))]($style) ",
|
||||||
symbol: "☁️ ",
|
symbol: "☁️ ",
|
||||||
style: "bold blue",
|
style: "bold blue",
|
||||||
disabled: false,
|
disabled: false,
|
||||||
|
|
|
@ -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::Path;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
@ -8,6 +9,8 @@ use crate::configs::gcloud::GcloudConfig;
|
||||||
use crate::formatter::StringFormatter;
|
use crate::formatter::StringFormatter;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
|
type Account = (String, Option<String>);
|
||||||
|
|
||||||
struct GcloudContext {
|
struct GcloudContext {
|
||||||
config_name: String,
|
config_name: String,
|
||||||
config_path: PathBuf,
|
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 config = self.get_config()?;
|
||||||
let account_line = config
|
let account_line = config
|
||||||
.lines()
|
.lines()
|
||||||
|
@ -42,7 +45,11 @@ impl GcloudContext {
|
||||||
.take_while(|line| !line.starts_with('['))
|
.take_while(|line| !line.starts_with('['))
|
||||||
.find(|line| line.starts_with("account"))?;
|
.find(|line| line.starts_with("account"))?;
|
||||||
let account = account_line.splitn(2, '=').nth(1)?.trim();
|
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> {
|
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 (config_name, config_path) = get_current_config(context)?;
|
||||||
let gcloud_context = GcloudContext::new(&config_name, &config_path);
|
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| {
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||||
formatter
|
formatter
|
||||||
|
@ -117,7 +125,16 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
_ => None,
|
_ => None,
|
||||||
})
|
})
|
||||||
.map(|variable| match variable {
|
.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
|
"region" => gcloud_context
|
||||||
.get_region()
|
.get_region()
|
||||||
.map(|region| {
|
.map(|region| {
|
||||||
|
@ -187,6 +204,36 @@ account = foo@example.com
|
||||||
dir.close()
|
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]
|
#[test]
|
||||||
fn account_and_region_set() -> io::Result<()> {
|
fn account_and_region_set() -> io::Result<()> {
|
||||||
let dir = tempfile::tempdir()?;
|
let dir = tempfile::tempdir()?;
|
||||||
|
|
Loading…
Reference in New Issue