Skip to content

Commit afeb617

Browse files
author
Tim Simpson
committed
Added a non-interactive mode to the CLI tool and a batch file for Windows users.
1 parent 67c52e4 commit afeb617

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed

run_cli.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@ECHO OFF
2+
SET SCRIPT_DIR=%~dp0
3+
java -classpath %SCRIPT_DIR%lib/httpcore-4.1.jar;%SCRIPT_DIR%lib/commons-cli-1.1.jar;%SCRIPT_DIR%lib/httpclient-4.0.3.jar;%SCRIPT_DIR%lib/commons-lang-2.4.jar;%SCRIPT_DIR%lib/junit.jar;%SCRIPT_DIR%lib/commons-codec-1.3.jar;%SCRIPT_DIR%lib/commons-io-1.4.jar;%SCRIPT_DIR%lib/commons-logging-1.1.1.jar;%SCRIPT_DIR%lib/log4j-1.2.15.jar;%SCRIPT_DIR%dist/java-cloudfiles.jar;%SCRIPT_DIR%. com.rackspacecloud.client.cloudfiles.sample.FilesCli %*
4+
@ECHO ON

run_cli.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
export CLASSPATH=lib/httpcore-4.1.jar:lib/commons-cli-1.1.jar:lib/httpclient-4.0.3.jar:lib/commons-lang-2.4.jar:lib/junit.jar:lib/commons-codec-1.3.jar:lib/commons-io-1.4.jar:lib/commons-logging-1.1.1.jar:lib/log4j-1.2.15.jar:dist/java-cloudfiles.jar:.
44

5-
java com.rackspacecloud.client.cloudfiles.sample.FilesCli
5+
java com.rackspacecloud.client.cloudfiles.sample.FilesCli $@

src/main/java/com/rackspacecloud/client/cloudfiles/sample/FilesCli.java

Lines changed: 83 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.InputStream;
1212
import java.io.InputStreamReader;
1313
import java.text.DecimalFormat;
14+
import java.util.ArrayList;
1415
import java.util.List;
1516
import java.util.Map;
1617

@@ -42,23 +43,8 @@ private boolean doLogin() {
4243

4344
System.out.print("Password: ");
4445
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-
}
5246

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);
6248

6349
return result;
6450
} catch (Exception e) {
@@ -70,6 +56,11 @@ private boolean doLogin() {
7056

7157
}
7258

59+
private boolean doLogin(final String username, final String password) throws Exception {
60+
client = new FilesClient(username, password);
61+
return client.login();
62+
}
63+
7364
private static final String HELP_STRING =
7465
"Commands:\n" +
7566
" get List the containers for this account\n" +
@@ -93,9 +84,16 @@ private boolean evaluateCommand(String cmd) {
9384
System.out.println(HELP_STRING);
9485
return true;
9586
}
87+
return evaluateCommand(components);
88+
}
89+
90+
private boolean evaluateCommand(String[] components) {
9691

9792
String command = components[0].toLowerCase();
9893

94+
if ("help".equals(command)) {
95+
System.out.println(HELP_STRING);
96+
}
9997
// Exit
10098
if("exit".equals(command) || "quit".equals(command)) {
10199
System.out.println("Exiting");
@@ -362,17 +360,85 @@ else if(components.length == 3) {
362360
}
363361

364362
// We should never get here
365-
System.out.println("Unrecognized command " + cmd);
363+
System.out.println("Unrecognized command " + command);
366364
System.out.println(HELP_STRING);
367365
return true;
368366
}
369367

370368

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+
}
371410
/**
372411
* @param args
373412
*/
374413
public static void main(String[] args) {
375414

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() {
376442
FilesCli commandLine = new FilesCli();
377443

378444
if (commandLine.doLogin()) {

0 commit comments

Comments
 (0)