Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ajay Kannan committed Dec 19, 2015
1 parent 1a10c7b commit 741b106
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ private interface ResourceManagerAction {
private static class CreateAction implements ResourceManagerAction {
@Override
public void run(ResourceManager resourceManager, String... args) {
if (args.length % 2 != 1) {
System.out.println(usage("create", ACTIONS.get("create")));
return;
}
String projectId = args[0];
Map<String, String> labels = new HashMap<>();
for (int i = 1; i < args.length; i += 2) {
labels.put(args[i], args[i + 1]);
if (i + 1 < args.length) {
labels.put(args[i], args[i + 1]);
} else {
labels.put(args[i], "");
}
}
ProjectInfo project =
resourceManager.create(ProjectInfo.builder(projectId).labels(labels).build());
System.out.printf(
"Successfully created project '%s': %s.%n", projectId, projectDetails(project));
System.out.printf(
"Successfully created project '%s': %s.%n", projectId, projectDetails(project));
}

@Override
Expand All @@ -85,10 +85,10 @@ private static class DeleteAction implements ResourceManagerAction {
@Override
public void run(ResourceManager resourceManager, String... args) {
String projectId = args[0];
System.out.print("Are you sure [y/N]: ");
System.out.printf("Going to delete project \"%s\". Are you sure [y/N]: ", projectId);
Scanner scanner = new Scanner(System.in);
if (scanner.nextLine().toLowerCase().equals("y")) {
resourceManager.delete(args[0]);
resourceManager.delete(projectId);
System.out.println("Successfully deleted project " + projectId + ".");
} else {
System.out.println("Will not delete project " + projectId + ".");
Expand Down Expand Up @@ -159,7 +159,7 @@ public String[] getOptionalParams() {
}

private static String projectDetails(ProjectInfo project) {
return new StringBuffer()
return new StringBuilder()
.append("{projectId:")
.append(project.projectId())
.append(", projectNumber:")
Expand All @@ -174,18 +174,21 @@ private static String projectDetails(ProjectInfo project) {
.toString();
}

private static String usage(String actionName, ResourceManagerAction action) {
StringBuilder usage = new StringBuilder();
private static void addUsage(
String actionName, ResourceManagerAction action, StringBuilder usage) {
usage.append(actionName);
String requiredParam = Joiner.on(" ").join(action.getRequiredParams());
String optionalParam = Joiner.on(" ").join(action.getOptionalParams());
if (!requiredParam.isEmpty()) {
usage.append(' ').append(requiredParam);
}
if (!optionalParam.isEmpty()) {
usage.append(" [").append(optionalParam).append("]");
Joiner joiner = Joiner.on(" ");
String[] requiredParams = action.getRequiredParams();
String[] optionalParams = action.getOptionalParams();
if (requiredParams.length > 0) {
usage.append(' ');
joiner.appendTo(usage, requiredParams);
}
if (optionalParams.length > 0) {
usage.append(" [");
joiner.appendTo(usage, optionalParams);
usage.append(']');
}
return usage.toString();
}

public static void main(String... args) {
Expand All @@ -194,7 +197,7 @@ public static void main(String... args) {
if (action == null) {
StringBuilder actionAndParams = new StringBuilder();
for (Map.Entry<String, ResourceManagerAction> entry : ACTIONS.entrySet()) {
actionAndParams.append(usage(entry.getKey(), entry.getValue()));
addUsage(entry.getKey(), entry.getValue(), actionAndParams);
actionAndParams.append('|');
}
actionAndParams.setLength(actionAndParams.length() - 1);
Expand All @@ -209,7 +212,9 @@ public static void main(String... args) {
ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
args = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[] {};
if (args.length < action.getRequiredParams().length) {
System.out.println("Usage: " + usage(actionName, action));
StringBuilder usage = new StringBuilder();
addUsage(actionName, action, usage);
System.out.println("Usage: " + usage);
} else {
action.run(resourceManager, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
* A client to Google Cloud Resource Manager.
*
* <p>Here's a simple usage example for using gcloud-java-resourcemanager:
*
* <pre>{@code
* ResourceManager resourceManager = ResourceManagerOptions.defaultInstance().service();
* String myProjectId = "my-globally-unique-project-id"; // Change to a unique project ID.
Expand Down

0 comments on commit 741b106

Please sign in to comment.