@@ -55,19 +55,19 @@ private interface ResourceManagerAction {
5555 private static class CreateAction implements ResourceManagerAction {
5656 @ Override
5757 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- }
6258 String projectId = args [0 ];
6359 Map <String , String > labels = new HashMap <>();
6460 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+ }
6666 }
6767 ProjectInfo project =
6868 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 ));
7171 }
7272
7373 @ Override
@@ -85,10 +85,10 @@ private static class DeleteAction implements ResourceManagerAction {
8585 @ Override
8686 public void run (ResourceManager resourceManager , String ... args ) {
8787 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 );
8989 Scanner scanner = new Scanner (System .in );
9090 if (scanner .nextLine ().toLowerCase ().equals ("y" )) {
91- resourceManager .delete (args [ 0 ] );
91+ resourceManager .delete (projectId );
9292 System .out .println ("Successfully deleted project " + projectId + "." );
9393 } else {
9494 System .out .println ("Will not delete project " + projectId + "." );
@@ -159,7 +159,7 @@ public String[] getOptionalParams() {
159159 }
160160
161161 private static String projectDetails (ProjectInfo project ) {
162- return new StringBuffer ()
162+ return new StringBuilder ()
163163 .append ("{projectId:" )
164164 .append (project .projectId ())
165165 .append (", projectNumber:" )
@@ -174,18 +174,21 @@ private static String projectDetails(ProjectInfo project) {
174174 .toString ();
175175 }
176176
177- private static String usage (String actionName , ResourceManagerAction action ) {
178- StringBuilder usage = new StringBuilder ();
177+ private static void usage (
178+ String actionName , ResourceManagerAction action , StringBuilder usage ) {
179179 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 (']' );
187191 }
188- return usage .toString ();
189192 }
190193
191194 public static void main (String ... args ) {
@@ -194,7 +197,7 @@ public static void main(String... args) {
194197 if (action == null ) {
195198 StringBuilder actionAndParams = new StringBuilder ();
196199 for (Map .Entry <String , ResourceManagerAction > entry : ACTIONS .entrySet ()) {
197- actionAndParams . append ( usage (entry .getKey (), entry .getValue ()) );
200+ usage (entry .getKey (), entry .getValue (), actionAndParams );
198201 actionAndParams .append ('|' );
199202 }
200203 actionAndParams .setLength (actionAndParams .length () - 1 );
@@ -209,7 +212,9 @@ public static void main(String... args) {
209212 ResourceManager resourceManager = ResourceManagerOptions .defaultInstance ().service ();
210213 args = args .length > 1 ? Arrays .copyOfRange (args , 1 , args .length ) : new String [] {};
211214 if (args .length < action .getRequiredParams ().length ) {
212- System .out .println ("Usage: " + usage (actionName , action ));
215+ StringBuilder usage = new StringBuilder ();
216+ usage (actionName , action , usage );
217+ System .out .println ("Usage: " + usage );
213218 } else {
214219 action .run (resourceManager , args );
215220 }
0 commit comments