Skip to content

Commit

Permalink
Merge pull request #831 from mziccard/active-gcloud-project
Browse files Browse the repository at this point in the history
Get projectId from gcloud active config
  • Loading branch information
aozarov committed Apr 1, 2016
2 parents 4b1a4fd + 379a0a8 commit 6cbddff
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import com.google.gcloud.spi.ServiceRpcFactory;

import java.io.BufferedReader;
Expand All @@ -42,6 +43,7 @@
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Objects;
Expand Down Expand Up @@ -381,6 +383,18 @@ protected String defaultProject() {
return projectId != null ? projectId : googleCloudProjectId();
}

private static String activeGoogleCloudConfig(File configDir) {
String activeGoogleCloudConfig = null;
try {
activeGoogleCloudConfig =
Files.readFirstLine(new File(configDir, "active_config"), Charset.defaultCharset());
} catch (IOException ex) {
// ignore
}
// if reading active_config failed or the file is empty we try default
return firstNonNull(activeGoogleCloudConfig, "default");
}

protected static String googleCloudProjectId() {
File configDir;
if (System.getenv().containsKey("CLOUDSDK_CONFIG")) {
Expand All @@ -390,9 +404,10 @@ protected static String googleCloudProjectId() {
} else {
configDir = new File(System.getProperty("user.home"), ".config/gcloud");
}
String activeConfig = activeGoogleCloudConfig(configDir);
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(configDir, "configurations/config_default"));
fileReader = new FileReader(new File(configDir, "configurations/config_" + activeConfig));
} catch (FileNotFoundException newConfigFileNotFoundEx) {
try {
fileReader = new FileReader(new File(configDir, "properties"));
Expand Down

0 comments on commit 6cbddff

Please sign in to comment.