11
11
import java .io .InputStream ;
12
12
import java .io .InputStreamReader ;
13
13
import java .text .DecimalFormat ;
14
+ import java .util .ArrayList ;
14
15
import java .util .List ;
15
16
import java .util .Map ;
16
17
@@ -42,23 +43,8 @@ private boolean doLogin() {
42
43
43
44
System .out .print ("Password: " );
44
45
String password = console .readLine ().trim ();
45
-
46
- System .out .print ("Account (return if using Mosso directly): " );
47
- String account = console .readLine ().trim ();
48
-
49
- if (account .length () == 0 ) {
50
- account = null ;
51
- }
52
46
53
- client = new FilesClient (username , password , account );
54
- boolean result = client .login ();
55
-
56
- if (result ) {
57
- System .out .println ("Logged in!" );
58
- }
59
- else {
60
- System .out .println ("Login failed" );
61
- }
47
+ final boolean result = doLogin (username , password );
62
48
63
49
return result ;
64
50
} catch (Exception e ) {
@@ -70,6 +56,11 @@ private boolean doLogin() {
70
56
71
57
}
72
58
59
+ private boolean doLogin (final String username , final String password ) throws Exception {
60
+ client = new FilesClient (username , password );
61
+ return client .login ();
62
+ }
63
+
73
64
private static final String HELP_STRING =
74
65
"Commands:\n " +
75
66
" get List the containers for this account\n " +
@@ -93,9 +84,16 @@ private boolean evaluateCommand(String cmd) {
93
84
System .out .println (HELP_STRING );
94
85
return true ;
95
86
}
87
+ return evaluateCommand (components );
88
+ }
89
+
90
+ private boolean evaluateCommand (String [] components ) {
96
91
97
92
String command = components [0 ].toLowerCase ();
98
93
94
+ if ("help" .equals (command )) {
95
+ System .out .println (HELP_STRING );
96
+ }
99
97
// Exit
100
98
if ("exit" .equals (command ) || "quit" .equals (command )) {
101
99
System .out .println ("Exiting" );
@@ -362,17 +360,85 @@ else if(components.length == 3) {
362
360
}
363
361
364
362
// We should never get here
365
- System .out .println ("Unrecognized command " + cmd );
363
+ System .out .println ("Unrecognized command " + command );
366
364
System .out .println (HELP_STRING );
367
365
return true ;
368
366
}
369
367
370
368
369
+ public static class CommandLineOptions {
370
+ public final String userName ;
371
+ public final String password ;
372
+ public final String [] command ;
373
+
374
+ public CommandLineOptions (String [] args ) {
375
+ String userName = null ;
376
+ String password = null ;
377
+ List <String > command = new ArrayList <String >();
378
+ userName = System .getenv ("CLOUDFILES_USERNAME" );
379
+ password = System .getenv ("CLOUDFILES_PASSWORD" );
380
+ for (int i = 0 ; i < args .length ; i ++) {
381
+ if ("username" .equals (args [i ])) {
382
+ if (i >= args .length - 1 ) {
383
+ throw new RuntimeException ("No argument following option 'username'." );
384
+ }
385
+ userName = args [i + 1 ];
386
+ i ++;
387
+ } else if ("password" .equals (args [i ])) {
388
+ if (i >= args .length - 1 ) {
389
+ throw new RuntimeException ("No argument following option 'password'." );
390
+ }
391
+ password = args [i + 1 ];
392
+ i ++;
393
+ } else {
394
+ command .add (args [i ]);
395
+ }
396
+ }
397
+ if (userName == null ) {
398
+ throw new RuntimeException ("No username specified (use option 'username' or set CLOUDFILES_USERNAME environment variable)." );
399
+ }
400
+ if (password == null ) {
401
+ throw new RuntimeException ("No password specified (use option 'password' or set CLOUDFILES_PASSWORD environment variable)." );
402
+ }
403
+ this .userName = userName ;
404
+ this .password = password ;
405
+ this .command = new String [command .size ()];
406
+ command .toArray (this .command );
407
+ }
408
+
409
+ }
371
410
/**
372
411
* @param args
373
412
*/
374
413
public static void main (String [] args ) {
375
414
415
+ if (args .length < 1 ) {
416
+ interactiveMode ();
417
+ } else {
418
+ parseArgs (args );
419
+ }
420
+ }
421
+
422
+ public static void parseArgs (String [] args ) {
423
+ try {
424
+ final CommandLineOptions options = new CommandLineOptions (args );
425
+ final FilesCli cli = new FilesCli ();
426
+ if (!cli .doLogin (options .userName , options .password )) {
427
+ throw new RuntimeException ("Failed to login." );
428
+ }
429
+ if (options .command .length == 0 ) {
430
+ System .out .println ("Login was successful, but no other commands were specified." );
431
+ System .out .println (HELP_STRING );
432
+ } else {
433
+ cli .evaluateCommand (options .command );
434
+ }
435
+ } catch (Exception e ) {
436
+ System .err .println ("Error:" + e .getMessage ());
437
+ e .printStackTrace ();
438
+ }
439
+ }
440
+
441
+ public static void interactiveMode () {
376
442
FilesCli commandLine = new FilesCli ();
377
443
378
444
if (commandLine .doLogin ()) {
0 commit comments