@@ -55,19 +55,19 @@ private interface ResourceManagerAction {
55
55
private static class CreateAction implements ResourceManagerAction {
56
56
@ Override
57
57
public void run (ResourceManager resourceManager , String ... args ) {
58
- if (args .length % 2 != 1 ) {
59
- System .out .println (usage ("create" , ACTIONS .get ("create" )));
60
- return ;
61
- }
62
58
String projectId = args [0 ];
63
59
Map <String , String > labels = new HashMap <>();
64
60
for (int i = 1 ; i < args .length ; i += 2 ) {
65
- labels .put (args [i ], args [i + 1 ]);
61
+ if (i + 1 < args .length ) {
62
+ labels .put (args [i ], args [i + 1 ]);
63
+ } else {
64
+ labels .put (args [i ], "" );
65
+ }
66
66
}
67
67
ProjectInfo project =
68
68
resourceManager .create (ProjectInfo .builder (projectId ).labels (labels ).build ());
69
- System .out .printf (
70
- "Successfully created project '%s': %s.%n" , projectId , projectDetails (project ));
69
+ System .out .printf (
70
+ "Successfully created project '%s': %s.%n" , projectId , projectDetails (project ));
71
71
}
72
72
73
73
@ Override
@@ -85,10 +85,10 @@ private static class DeleteAction implements ResourceManagerAction {
85
85
@ Override
86
86
public void run (ResourceManager resourceManager , String ... args ) {
87
87
String projectId = args [0 ];
88
- System .out .print ( " Are you sure [y/N]: " );
88
+ System .out .printf ( "Going to delete project \" %s \" . Are you sure [y/N]: ", projectId );
89
89
Scanner scanner = new Scanner (System .in );
90
90
if (scanner .nextLine ().toLowerCase ().equals ("y" )) {
91
- resourceManager .delete (args [ 0 ] );
91
+ resourceManager .delete (projectId );
92
92
System .out .println ("Successfully deleted project " + projectId + "." );
93
93
} else {
94
94
System .out .println ("Will not delete project " + projectId + "." );
@@ -159,7 +159,7 @@ public String[] getOptionalParams() {
159
159
}
160
160
161
161
private static String projectDetails (ProjectInfo project ) {
162
- return new StringBuffer ()
162
+ return new StringBuilder ()
163
163
.append ("{projectId:" )
164
164
.append (project .projectId ())
165
165
.append (", projectNumber:" )
@@ -174,18 +174,21 @@ private static String projectDetails(ProjectInfo project) {
174
174
.toString ();
175
175
}
176
176
177
- private static String usage ( String actionName , ResourceManagerAction action ) {
178
- StringBuilder usage = new StringBuilder ();
177
+ private static void addUsage (
178
+ String actionName , ResourceManagerAction action , StringBuilder usage ) {
179
179
usage .append (actionName );
180
- String requiredParam = Joiner .on (" " ).join (action .getRequiredParams ());
181
- String optionalParam = Joiner .on (" " ).join (action .getOptionalParams ());
182
- if (!requiredParam .isEmpty ()) {
183
- usage .append (' ' ).append (requiredParam );
184
- }
185
- if (!optionalParam .isEmpty ()) {
186
- usage .append (" [" ).append (optionalParam ).append ("]" );
180
+ Joiner joiner = Joiner .on (" " );
181
+ String [] requiredParams = action .getRequiredParams ();
182
+ String [] optionalParams = action .getOptionalParams ();
183
+ if (requiredParams .length > 0 ) {
184
+ usage .append (' ' );
185
+ joiner .appendTo (usage , requiredParams );
186
+ }
187
+ if (optionalParams .length > 0 ) {
188
+ usage .append (" [" );
189
+ joiner .appendTo (usage , optionalParams );
190
+ usage .append (']' );
187
191
}
188
- return usage .toString ();
189
192
}
190
193
191
194
public static void main (String ... args ) {
@@ -194,7 +197,7 @@ public static void main(String... args) {
194
197
if (action == null ) {
195
198
StringBuilder actionAndParams = new StringBuilder ();
196
199
for (Map .Entry <String , ResourceManagerAction > entry : ACTIONS .entrySet ()) {
197
- actionAndParams . append ( usage ( entry .getKey (), entry .getValue ()) );
200
+ addUsage ( entry .getKey (), entry .getValue (), actionAndParams );
198
201
actionAndParams .append ('|' );
199
202
}
200
203
actionAndParams .setLength (actionAndParams .length () - 1 );
@@ -209,7 +212,9 @@ public static void main(String... args) {
209
212
ResourceManager resourceManager = ResourceManagerOptions .defaultInstance ().service ();
210
213
args = args .length > 1 ? Arrays .copyOfRange (args , 1 , args .length ) : new String [] {};
211
214
if (args .length < action .getRequiredParams ().length ) {
212
- System .out .println ("Usage: " + usage (actionName , action ));
215
+ StringBuilder usage = new StringBuilder ();
216
+ addUsage (actionName , action , usage );
217
+ System .out .println ("Usage: " + usage );
213
218
} else {
214
219
action .run (resourceManager , args );
215
220
}
0 commit comments