Skip to content

Commit b3ad20f

Browse files
committed
---
yaml --- r: 5965 b: refs/heads/tswast-patch-1 c: 648ae8d h: refs/heads/master i: 5963: a82ea68
1 parent 2ff2013 commit b3ad20f

3 files changed

Lines changed: 46 additions & 14 deletions

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: d9b994bccf49a33707fa15b3ffc4020bcf935913
60+
refs/heads/tswast-patch-1: 648ae8d389f3a7b72153b2724b8ed2ef3d0f780b
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b

branches/tswast-patch-1/src/main/java/com/google/gcloud/ServiceOptions.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@
2727
import com.google.gcloud.spi.ServiceRpcFactory;
2828

2929
import java.io.BufferedReader;
30+
import java.io.File;
31+
import java.io.FileReader;
3032
import java.io.IOException;
33+
import java.io.InputStream;
3134
import java.io.InputStreamReader;
3235
import java.io.Serializable;
3336
import java.lang.reflect.Method;
37+
import java.net.HttpURLConnection;
3438
import java.net.URL;
35-
import java.net.URLConnection;
3639
import java.util.Objects;
3740
import java.util.Set;
41+
import java.util.regex.Matcher;
42+
import java.util.regex.Pattern;
3843

3944
public abstract class ServiceOptions<R, O extends ServiceOptions<R, O>> implements Serializable {
4045

@@ -168,16 +173,36 @@ protected static String appEngineAppId() {
168173
protected static String googleCloudProjectId() {
169174
try {
170175
URL url = new URL("http://metadata/computeMetadata/v1/project/project-id");
171-
URLConnection connection = url.openConnection();
176+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
172177
connection.setRequestProperty("X-Google-Metadata-Request", "True");
173-
try (BufferedReader reader =
174-
new BufferedReader(new InputStreamReader(connection.getInputStream(), UTF_8))) {
175-
return reader.readLine();
178+
InputStream input = connection.getInputStream();
179+
if (connection.getResponseCode() == 200) {
180+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input, UTF_8))) {
181+
return reader.readLine();
182+
}
176183
}
177184
} catch (IOException ignore) {
178-
// return null if can't determine
179-
return null;
185+
// ignore
186+
}
187+
String configDir = System.getenv("CLOUDSDK_CONFIG");
188+
if (configDir == null) {
189+
configDir = new File(System.getProperty("user.home"), "/.config/gcloud/").getPath();
190+
}
191+
try (BufferedReader reader =
192+
new BufferedReader(new FileReader(new File(configDir, "properties")))) {
193+
String line;
194+
Pattern pattern = Pattern.compile("^\\s*project\\s*=\\s*(.*?)\\s*$");
195+
while((line = reader.readLine()) != null) {
196+
Matcher matcher = pattern.matcher(line);
197+
if (matcher.matches()) {
198+
return matcher.group(1);
199+
}
200+
}
201+
} catch (IOException ex) {
202+
// ignore
180203
}
204+
// return null if can't determine
205+
return null;
181206
}
182207

183208
protected static String getAppEngineProjectId() {

branches/tswast-patch-1/src/main/java/com/google/gcloud/examples/StorageExample.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
* <li>compile using maven - {@code mvn compile}</li>
4747
* <li>run using maven -
4848
* {@code mvn exec:java -Dexec.mainClass="com.google.gcloud.examples.StorageExample"
49-
* -Dexec.args="project_id list [<bucket>]| info [<bucket> [<file>]]| get <bucket> <path>|
49+
* -Dexec.args="[<project_id>] list [<bucket>]| info [<bucket> [<file>]]| get <bucket> <path>|
5050
* upload <local_file> <bucket> [<path>]| delete <bucket> <path>+|
5151
* cp <from_bucket> <from_path> <to_bucket> <to_path>| compose <bucket> <from_path>+ <to_path>"}
5252
* </li>
@@ -300,20 +300,27 @@ public static void printUsage() {
300300

301301
@SuppressWarnings("unchecked")
302302
public static void main(String... args) throws Exception {
303-
if (args.length < 2) {
303+
if (args.length < 1) {
304304
System.out.println("Missing required project id and action");
305305
printUsage();
306306
return;
307307
}
308-
StorageAction action = ACTIONS.get(args[1]);
308+
StorageServiceOptions.Builder optionsBuilder = StorageServiceOptions.builder();
309+
StorageAction action;
310+
if (args.length >= 2 && !ACTIONS.containsKey(args[0])) {
311+
optionsBuilder.project(args[0]);
312+
action = ACTIONS.get(args[1]);
313+
args = Arrays.copyOfRange(args, 2, args.length);
314+
} else {
315+
action = ACTIONS.get(args[0]);
316+
args = Arrays.copyOfRange(args, 1, args.length);
317+
}
309318
if (action == null) {
310319
System.out.println("Unrecognized action '" + args[1] + "'");
311320
printUsage();
312321
return;
313322
}
314-
StorageServiceOptions options = StorageServiceOptions.builder().project(args[0]).build();
315-
StorageService storage = StorageServiceFactory.instance().get(options);
316-
args = args.length > 2 ? Arrays.copyOfRange(args, 2, args.length) : new String[] {};
323+
StorageService storage = StorageServiceFactory.instance().get(optionsBuilder.build());
317324
Object request;
318325
try {
319326
request = action.parse(args);

0 commit comments

Comments
 (0)