Permalink
Browse files

initial commit

  • Loading branch information...
ptgoetz committed Feb 4, 2012
0 parents commit ace16b4af175b2dd7742dd6456a1aef2aa5aa058
No changes.
34 pom.xml
@@ -0,0 +1,34 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.github.ptgoetz</groupId>
+ <artifactId>stemshell</artifactId>
+ <version>0.1.0-SNAPSHOT</version>
+ <name>StemShell CLI Framework</name>
+ <description>Framework for creating Java-based command line shell applications.</description>
+ <dependencies>
+ <dependency>
+ <groupId>commons-cli</groupId>
+ <artifactId>commons-cli</artifactId>
+ <version>1.2</version>
+ </dependency>
+ <dependency>
+ <groupId>org.sonatype.jline</groupId>
+ <artifactId>jline</artifactId>
+ <version>2.5</version>
+ </dependency>
+
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,102 @@
+// Copyright (c) 2012 Health Market Science, Inc.
+
+package com.instanceone.stemshell;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jline.console.ConsoleReader;
+import jline.console.completer.AggregateCompleter;
+import jline.console.completer.ArgumentCompleter;
+import jline.console.completer.Completer;
+import jline.console.completer.StringsCompleter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.apache.commons.cli.PosixParser;
+import org.fusesource.jansi.AnsiConsole;
+
+import com.instanceone.stemshell.command.Command;
+
+public abstract class AbstractShell {
+ private static CommandLineParser parser = new PosixParser();
+ private Environment env = new Environment();
+
+ public final void run(String[] arguments) throws Exception {
+ initialize(this.env);
+
+ // create completers
+ ArrayList<Completer> completers = new ArrayList<Completer>();
+ for (String cmdName : env.commandList()) {
+ // command name
+ StringsCompleter sc = new StringsCompleter(cmdName);
+
+
+ ArrayList<Completer> cmdCompleters = new ArrayList<Completer>();
+ // add a completer for the command name
+ cmdCompleters.add(sc);
+ // add the completer for the command
+ cmdCompleters.add(env.getCommand(cmdName).getCompleter());
+ // add a terminator for the command
+ //cmdCompleters.add(new NullCompleter());
+
+ ArgumentCompleter ac = new ArgumentCompleter(cmdCompleters);
+ completers.add(ac);
+ }
+
+ AggregateCompleter aggComp = new AggregateCompleter(completers);
+
+ ConsoleReader reader = new ConsoleReader();
+ reader.addCompleter(aggComp);
+
+ AnsiConsole.systemInstall();
+ String line;
+
+ while ((line = reader.readLine(this.env.getPrompt() +" ")) != null) {
+ String[] argv = line.split("\\s");
+ String cmdName = argv[0];
+
+ Command command = env.getCommand(cmdName);
+ if (command != null) {
+ System.out.println("Running: " + command.getName() + " ("
+ + command.getClass().getName() + ")");
+ String[] cmdArgs = Arrays.copyOfRange(argv, 1, argv.length);
+ CommandLine cl = parse(command, cmdArgs);
+ if (cl != null) {
+ try {
+ command.execute(env, cl, reader);
+ }
+ catch (Throwable e) {
+ System.out.println("Command failed with error: " + e.getMessage());
+ if(cl.hasOption("v")){
+ e.printStackTrace();
+ }
+ }
+ }
+
+ }
+ else {
+ if(cmdName != null && cmdName.length() > 0){
+ System.out.println(cmdName + ": command not found");
+ }
+ }
+ }
+ }
+
+ private static CommandLine parse(Command cmd, String[] args) {
+ Options opts = cmd.getOptions();
+ CommandLine retval = null;
+ try {
+ retval = parser.parse(opts, args);
+ }
+ catch (ParseException e) {
+ System.err.println(e.getMessage());
+ }
+ return retval;
+ }
+
+ public abstract void initialize(Environment env) throws Exception;
+
+}
@@ -0,0 +1,67 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell;
+
+import java.util.HashMap;
+import java.util.Properties;
+import java.util.Set;
+
+import com.instanceone.stemshell.command.Command;
+
+public class Environment {
+
+ private String prompt = "eggshell$ ";
+ private Properties props = new Properties();
+ private HashMap<String, Object> values = new HashMap<String, Object>();
+
+ public Environment(){
+ }
+
+ private HashMap<String, Command> commands = new HashMap<String, Command>();
+
+
+ public void addCommand(Command cmd){
+ this.commands.put(cmd.getName(), cmd);
+ }
+
+ public Command getCommand(String name){
+ return this.commands.get(name);
+ }
+
+ public Set<String> commandList(){
+ return this.commands.keySet();
+ }
+
+ public void setProperty(String key, String value){
+ if(value == null){
+ this.props.remove(key);
+ } else{
+ this.props.setProperty(key, value);
+ }
+ }
+
+ public String getProperty(String key){
+ return this.props.getProperty(key);
+ }
+
+ public Properties getProperties(){
+ return this.props;
+ }
+
+ public void setValue(String key, Object value){
+ this.values.put(key, value);
+ }
+
+ public Object getValue(String key){
+ return this.values.get(key);
+ }
+
+
+ public String getPrompt(){
+ return this.prompt;
+ }
+
+ public void setPrompt(String prompt){
+ this.prompt = prompt;
+ }
+}
@@ -0,0 +1,11 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell;
+
+
+public abstract class Shell extends AbstractShell {
+
+ @Override
+ public abstract void initialize(Environment env) throws Exception;
+
+}
@@ -0,0 +1,56 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.command;
+
+import jline.console.completer.Completer;
+import jline.console.completer.NullCompleter;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+public abstract class AbstractCommand implements Command{
+ private String name;
+ protected Completer completer = new NullCompleter();
+
+
+ public AbstractCommand(String name){
+ this.name = name;
+ }
+
+ public String getHelpHeader() {
+ return "Options:";
+ }
+
+ public String gethelpFooter() {
+ return null;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+
+ public Options getOptions() {
+ Options opts = new Options();
+ opts.addOption("v", "verbose", false, "show verbose output");
+ return opts;
+ }
+
+ public String getUsage(){
+ return getName() + " [OPTION ...] [ARGS ...]";
+ }
+
+ protected static void logv(CommandLine cmd, String log){
+ if(cmd.hasOption("v")){
+ System.out.println(log);
+ }
+ }
+
+ protected static void log(CommandLine cmd, String log){
+ System.out.println(log);
+ }
+
+ public Completer getCompleter() {
+ return this.completer;
+ }
+}
@@ -0,0 +1,26 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.command;
+
+import jline.console.ConsoleReader;
+import jline.console.completer.Completer;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+import com.instanceone.stemshell.Environment;
+
+public interface Command {
+
+ String getHelpHeader();
+ String gethelpFooter();
+ String getUsage();
+
+ String getName();
+
+ void execute(Environment env, CommandLine cmd, ConsoleReader reader);
+
+ Options getOptions();
+
+ Completer getCompleter();
+}
@@ -0,0 +1,46 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.commands;
+
+import java.util.Properties;
+
+import jline.console.ConsoleReader;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Options;
+
+import com.instanceone.stemshell.Environment;
+import com.instanceone.stemshell.command.AbstractCommand;
+
+public class Env extends AbstractCommand {
+
+ public Env(String name) {
+ super(name);
+ }
+
+ public void execute(Environment env, CommandLine cmd, ConsoleReader reader) {
+ if (cmd.hasOption("l") || !cmd.hasOption("s")) {
+ Properties props = env.getProperties();
+ log(cmd, "Local Properties:");
+ for (Object key : props.keySet()) {
+ log(cmd, "\t" + key + "=" + props.get(key));
+ }
+ }
+ if (cmd.hasOption("s")) {
+ log(cmd, "System Properties:");
+ Properties props = System.getProperties();
+ for (Object key : props.keySet()) {
+ log(cmd, "\t" + key + "=" + props.get(key));
+ }
+ }
+ }
+
+ @Override
+ public Options getOptions() {
+ Options opts = super.getOptions();
+ opts.addOption("s", "system", false, "list system properties.");
+ opts.addOption("l", "local", false, "list local properties.");
+ return opts;
+ }
+
+}
@@ -0,0 +1,34 @@
+// Copyright (c) 2012 P. Taylor Goetz (ptgoetz@gmail.com)
+
+package com.instanceone.stemshell.commands;
+
+import jline.console.ConsoleReader;
+
+import org.apache.commons.cli.CommandLine;
+
+import com.instanceone.stemshell.Environment;
+import com.instanceone.stemshell.command.AbstractCommand;
+
+public class Exit extends AbstractCommand {
+
+ public Exit(String name) {
+ super(name);
+ }
+
+ public void execute(Environment env, CommandLine cmd, ConsoleReader reader) {
+ System.exit(0);
+ }
+
+ @Override
+ public String getHelpHeader() {
+ return "exit the command shell";
+ }
+
+ @Override
+ public String getUsage() {
+ return this.getName();
+ }
+
+
+
+}
Oops, something went wrong.

0 comments on commit ace16b4

Please sign in to comment.