Skip to content

Commit 0a83828

Browse files
committed
gcloud: retrieve project ID at initialization
1 parent ba6e7e6 commit 0a83828

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/gcloud_authorized_user.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@ use crate::Token;
1313
#[derive(Debug)]
1414
pub(crate) struct GCloudAuthorizedUser {
1515
gcloud: PathBuf,
16+
project_id: Option<String>,
1617
}
1718

1819
impl GCloudAuthorizedUser {
1920
pub(crate) fn new() -> Result<Self, Error> {
20-
which("gcloud")
21-
.map_err(|_| GCloudNotFound)
22-
.map(|path| Self { gcloud: path })
23-
}
24-
25-
fn project_id(&self) -> Result<String, Error> {
26-
run(&self.gcloud, &["config", "get-value", "project"])
21+
let gcloud = which("gcloud").map_err(|_| GCloudNotFound)?;
22+
let project_id = run(&gcloud, &["config", "get-value", "project"]).ok();
23+
Ok(Self { gcloud, project_id })
2724
}
2825

2926
fn token(&self) -> Result<Token, Error> {
@@ -37,7 +34,7 @@ impl GCloudAuthorizedUser {
3734
#[async_trait]
3835
impl ServiceAccount for GCloudAuthorizedUser {
3936
async fn project_id(&self, _: &HyperClient) -> Result<String, Error> {
40-
self.project_id()
37+
self.project_id.clone().ok_or(Error::NoProjectId)
4138
}
4239

4340
fn get_token(&self, _scopes: &[&str]) -> Option<Token> {
@@ -73,7 +70,7 @@ mod tests {
7370
#[ignore]
7471
fn gcloud() {
7572
let gcloud = GCloudAuthorizedUser::new().unwrap();
76-
println!("{:?}", gcloud.project_id());
73+
println!("{:?}", gcloud.project_id);
7774
println!("{:?}", gcloud.token());
7875
}
7976
}

0 commit comments

Comments
 (0)