Skip to content

Commit 379a0a8

Browse files
committed
Get projectId from gcloud active config
1 parent 249cac6 commit 379a0a8

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

gcloud-java-core/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.google.api.client.http.javanet.NetHttpTransport;
2828
import com.google.auth.http.HttpCredentialsAdapter;
2929
import com.google.common.collect.Iterables;
30+
import com.google.common.io.Files;
3031
import com.google.gcloud.spi.ServiceRpcFactory;
3132

3233
import java.io.BufferedReader;
@@ -42,6 +43,7 @@
4243
import java.lang.reflect.Method;
4344
import java.net.HttpURLConnection;
4445
import java.net.URL;
46+
import java.nio.charset.Charset;
4547
import java.util.Enumeration;
4648
import java.util.Locale;
4749
import java.util.Objects;
@@ -381,6 +383,18 @@ protected String defaultProject() {
381383
return projectId != null ? projectId : googleCloudProjectId();
382384
}
383385

386+
private static String activeGoogleCloudConfig(File configDir) {
387+
String activeGoogleCloudConfig = null;
388+
try {
389+
activeGoogleCloudConfig =
390+
Files.readFirstLine(new File(configDir, "active_config"), Charset.defaultCharset());
391+
} catch (IOException ex) {
392+
// ignore
393+
}
394+
// if reading active_config failed or the file is empty we try default
395+
return firstNonNull(activeGoogleCloudConfig, "default");
396+
}
397+
384398
protected static String googleCloudProjectId() {
385399
File configDir;
386400
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
@@ -390,9 +404,10 @@ protected static String googleCloudProjectId() {
390404
} else {
391405
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
392406
}
407+
String activeConfig = activeGoogleCloudConfig(configDir);
393408
FileReader fileReader = null;
394409
try {
395-
fileReader = new FileReader(new File(configDir, "configurations/config_default"));
410+
fileReader = new FileReader(new File(configDir, "configurations/config_" + activeConfig));
396411
} catch (FileNotFoundException newConfigFileNotFoundEx) {
397412
try {
398413
fileReader = new FileReader(new File(configDir, "properties"));

0 commit comments

Comments
 (0)