From d8836afcbcd5dbd7b3c843eac4ca5889e7ad1ce5 Mon Sep 17 00:00:00 2001 From: Bilung Lee Date: Tue, 13 Nov 2012 20:29:52 -0800 Subject: [PATCH] SQOOP-642 Provide resource reading for interactive and batch mode (Jarek Jarcec Cecho) --- .../apache/sqoop/client/shell/SqoopShell.java | 84 ++++++++++++++++--- 1 file changed, 71 insertions(+), 13 deletions(-) diff --git a/client/src/main/java/org/apache/sqoop/client/shell/SqoopShell.java b/client/src/main/java/org/apache/sqoop/client/shell/SqoopShell.java index 5ac8dd27..b2d05f46 100644 --- a/client/src/main/java/org/apache/sqoop/client/shell/SqoopShell.java +++ b/client/src/main/java/org/apache/sqoop/client/shell/SqoopShell.java @@ -20,6 +20,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.util.HashSet; import java.util.Iterator; @@ -30,18 +31,42 @@ import org.codehaus.groovy.tools.shell.Groovysh; import org.codehaus.groovy.tools.shell.IO.Verbosity; -public final class SqoopShell -{ - private static final String banner = +/** + * Main entry point to Sqoop client. + * + * Sqoop shell is implemented on top of Groovy shell. + */ +public final class SqoopShell { + + /** + * Location of resource file that can contain few initial commands that will + * be loaded during each client execution. + */ + private static final String RC_FILE = ".sqoop2rc"; + + /** + * Banner message that is displayed in interactive mode after client start. + */ + private static final String BANNER = "@|green Sqoop Shell:|@ Type '@|bold help|@' or '@|bold \\h|@' for help."; + /** + * Hash of commands that we want to have in history in all cases. + */ public final static HashSet commandsToKeep; + static { commandsToKeep = new HashSet(); commandsToKeep.add("exit"); commandsToKeep.add("history"); } + /** + * Main entry point to the client execution. + * + * @param args Command line arguments + * @throws Exception + */ public static void main (String[] args) throws Exception { System.setProperty("groovysh.prompt", "sqoop"); @@ -73,10 +98,21 @@ public static void main (String[] args) throws Exception shell.register(new CloneCommand(shell)); shell.register(new SubmissionCommand(shell)); + // Let's see if user do have resource file with initial commands that he + // would like to apply. + String homeDir = System.getProperty("user.home"); + File rcFile = new File(homeDir, RC_FILE); + + if(rcFile.exists()) { + shell.getIo().out.println("Loading resource file " + RC_FILE); + interpretFileContent(rcFile, shell); + shell.getIo().out.println("Resource file loaded."); + } + if (args.length == 0) { // Interactive mode: shell.getIo().setVerbosity(Verbosity.QUIET); - shell.getIo().out.println(banner); + shell.getIo().out.println(BANNER); shell.getIo().out.println(); shell.run(args); @@ -88,15 +124,37 @@ public static void main (String[] args) throws Exception script = new File(userDir, args[0]); } - BufferedReader in = new BufferedReader(new FileReader(script)); - String line; - while ((line = in.readLine()) != null) { - shell.getIo().out.print(shell.renderPrompt()); - shell.getIo().out.println(line); - Object result = shell.execute(line); - if (result != null) { - shell.getIo().out.println(result); - } + interpretFileContent(script, shell); + } + } + + /** + * Interpret file content in given shell. + * + * @param script Script file that should be interpreted + * @param shell Shell where the script should be interpreted + * @throws IOException + */ + private static void interpretFileContent(File script, Groovysh shell) throws IOException { + BufferedReader in = new BufferedReader(new FileReader(script)); + String line; + + // Iterate over all lines and executed them one by one + while ((line = in.readLine()) != null) { + + // Skip comments and empty lines as we don't need to interpret those + if(line.isEmpty() || line.startsWith("#")) { + continue; + } + + // Render shell and command to get user perception that it was run as usual + shell.getIo().out.print(shell.renderPrompt()); + shell.getIo().out.println(line); + + // Manually trigger command line parsing + Object result = shell.execute(line); + if (result != null) { + shell.getIo().out.println(result); } } }