1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-27 03:42:39 +00:00

refactor(gcloud): improve parsing of config files (#3844)

This commit is contained in:
David Knaack 2022-04-08 06:56:02 +02:00 committed by GitHub
parent 230e85be37
commit 4b7275efc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
use ini::Ini;
use once_cell::sync::{Lazy, OnceCell}; use once_cell::sync::{Lazy, OnceCell};
use std::ops::Deref; use std::borrow::Cow;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
@ -9,15 +10,15 @@ use crate::configs::gcloud::GcloudConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
use crate::utils; use crate::utils;
type Account = (String, Option<String>); type Account<'a> = (&'a str, Option<&'a str>);
struct GcloudContext { struct GcloudContext {
config_name: String, config_name: String,
config_path: PathBuf, config_path: PathBuf,
config: OnceCell<String>, config: OnceCell<Option<Ini>>,
} }
impl GcloudContext { impl<'a> GcloudContext {
pub fn new(config_name: &str, config_path: &Path) -> Self { pub fn new(config_name: &str, config_path: &Path) -> Self {
Self { Self {
config_name: config_name.to_string(), config_name: config_name.to_string(),
@ -26,54 +27,27 @@ impl GcloudContext {
} }
} }
fn get_config(&self) -> Option<&str> { fn get_config(&self) -> Option<&Ini> {
let config = self self.config
.config .get_or_init(|| Ini::load_from_file(&self.config_path).ok())
.get_or_try_init(|| utils::read_file(&self.config_path)); .as_ref()
match config {
Ok(data) => Some(data),
Err(_) => None,
}
} }
pub fn get_account(&self) -> Option<Account> { pub fn get_account(&'a self) -> Option<Account<'a>> {
let config = self.get_config()?; let config = self.get_config()?;
let account_line = config let account = config.section(Some("core"))?.get("account")?;
.lines()
.skip_while(|line| *line != "[core]")
.skip(1)
.take_while(|line| !line.starts_with('['))
.find(|line| line.starts_with("account"))?;
let account = account_line.split_once('=')?.1.trim();
let mut segments = account.splitn(2, '@'); let mut segments = account.splitn(2, '@');
Some(( Some((segments.next()?, segments.next()))
segments.next().map(String::from)?,
segments.next().map(String::from),
))
} }
pub fn get_project(&self) -> Option<String> { pub fn get_project(&'a self) -> Option<&'a str> {
let config = self.get_config()?; let config = self.get_config()?;
let project_line = config config.section(Some("core"))?.get("project")
.lines()
.skip_while(|line| *line != "[core]")
.skip(1)
.take_while(|line| !line.starts_with('['))
.find(|line| line.starts_with("project"))?;
let project = project_line.split_once('=')?.1.trim();
Some(project.to_string())
} }
pub fn get_region(&self) -> Option<String> { pub fn get_region(&'a self) -> Option<&'a str> {
let config = self.get_config()?; let config = self.get_config()?;
let region_line = config config.section(Some("compute"))?.get("region")
.lines()
.skip_while(|line| *line != "[compute]")
.skip(1)
.take_while(|line| !line.starts_with('['))
.find(|line| line.starts_with("region"))?;
let region = region_line.split_once('=')?.1.trim();
Some(region.to_string())
} }
} }
@ -112,7 +86,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 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
@ -126,35 +100,32 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}) })
.map(|variable| match variable { .map(|variable| match variable {
"account" => account "account" => account
.deref() .map(|(account, _)| account)
.as_ref() .map(Cow::Borrowed)
.map(|(account, _)| (*account).clone())
.map(Ok), .map(Ok),
"domain" => account "domain" => account
.deref() .and_then(|(_, domain)| domain)
.as_ref() .map(Cow::Borrowed)
.and_then(|(_, domain)| (*domain).clone())
.map(Ok), .map(Ok),
"region" => gcloud_context "region" => gcloud_context
.get_region() .get_region()
.map(|region| { .map(|region| config.region_aliases.get(region).copied().unwrap_or(region))
config .map(Cow::Borrowed)
.region_aliases
.get(&region)
.map_or(region, |alias| (*alias).to_owned())
})
.map(Ok), .map(Ok),
"project" => context "project" => context
.get_env("CLOUDSDK_CORE_PROJECT") .get_env("CLOUDSDK_CORE_PROJECT")
.or_else(|| gcloud_context.get_project()) .map(Cow::Owned)
.or_else(|| gcloud_context.get_project().map(Cow::Borrowed))
.map(|project| { .map(|project| {
config config
.project_aliases .project_aliases
.get(&project) .get(project.as_ref())
.map_or(project, |alias| (*alias).to_owned()) .copied()
.map(Cow::Borrowed)
.unwrap_or(project)
}) })
.map(Ok), .map(Ok),
"active" => Some(Ok(gcloud_context.config_name.clone())), "active" => Some(Ok(Cow::Borrowed(&gcloud_context.config_name))),
_ => None, _ => None,
}) })
.parse(None, Some(context)) .parse(None, Some(context))