Skip to content

Commit

Permalink
added cli defaults loading from property file
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrypekar committed May 26, 2015
1 parent cdfe1ac commit c157501
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
*.iml
todo.txt
punxsutawney*.jar
punxsutawney.properties

119 changes: 84 additions & 35 deletions src/java/ly/stealth/punxsutawney/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
import org.apache.log4j.*;

import java.io.File;
import java.net.URI;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import static java.lang.System.err;
import static java.lang.System.out;
Expand All @@ -24,45 +29,22 @@ public static void main(String... args) throws Exception {
}

private static void run(String... args) throws Exception {
Marathon.App app = new Marathon.App();

OptionParser parser = new OptionParser();
parser.accepts("marathon", "marathon url (http://master:8080)").withRequiredArg().required().ofType(String.class);
parser.accepts("listen", "listen url to download jar (http://192.168.3.1:5000)").withRequiredArg().required().ofType(String.class);

parser.accepts("id", "app id").withOptionalArg().ofType(String.class).defaultsTo(app.id);
parser.accepts("instances", "number of servers").withOptionalArg().ofType(Integer.class).defaultsTo(app.instances);
parser.accepts("cpus", "amount of cpu to use").withOptionalArg().ofType(Double.class).defaultsTo(app.cpus);
parser.accepts("mem", "amount of memory to use").withOptionalArg().ofType(Integer.class).defaultsTo(app.mem);
parser.accepts("help", "show this help");

OptionSet options;
try {
options = parser.parse(args);
} catch (OptionException e) {
parser.printHelpOn(out);
throw new Error(e.getMessage());
}
Options options = new Options(args);
out.println("Options:\n" + options);

if (options.has("help")) {
parser.printHelpOn(out);
return;
}

Marathon.url = (String) options.valueOf("marathon");
String listen = (String) options.valueOf("listen");
Marathon.url = "" + options.marathon;

app.uris.add(listen + "/jar/punxsutawney.jar");
app.id = (String) options.valueOf("id");
app.instances = (int) options.valueOf("instances");
Marathon.App app = new Marathon.App();
app.uris.add(options.listen + "/jar/punxsutawney.jar");
app.id = options.id;
app.instances = options.instances;

app.cpus = (double) options.valueOf("cpus");
app.mem = (int) options.valueOf("mem");
app.cpus = options.cpus;
app.mem = options.mem;
app.cmd = "while true; do echo 'Hello World'; sleep 1; done";


HttpServer httpServer = new HttpServer();
httpServer.setPort(new URI(listen).getPort());
httpServer.setPort(options.listen.getPort());
httpServer.setJar(findJar());
httpServer.start();

Expand Down Expand Up @@ -103,7 +85,74 @@ private static File findJar() {
throw new IllegalStateException("No " + mask + " found in . folder");
}

public static class Error extends java.lang.Error {
private static class Options {
public URL marathon;
public URL listen;

public String id;
public int instances;

public double cpus;
public int mem;

Options(String... args) {
Marathon.App app = new Marathon.App();

OptionParser parser = new OptionParser();
parser.accepts("marathon", "marathon url (http://master:8080)").withRequiredArg().ofType(String.class);
parser.accepts("listen", "listen url to download jar (http://192.168.3.1:5000)").withRequiredArg().ofType(String.class);

parser.accepts("id", "app id").withOptionalArg().ofType(String.class).defaultsTo(app.id);
parser.accepts("instances", "number of servers").withOptionalArg().ofType(Integer.class).defaultsTo(app.instances);
parser.accepts("cpus", "amount of cpu to use").withOptionalArg().ofType(Double.class).defaultsTo(app.cpus);
parser.accepts("mem", "amount of memory to use").withOptionalArg().ofType(Integer.class).defaultsTo(app.mem);

OptionSet options;
try {
options = parser.parse(args);
} catch (OptionException e) {
try { parser.printHelpOn(out); } catch (IOException ignore) {}
throw new Error(e.getMessage());
}

Properties props = new Properties();

File file = new File("punxsutawney.properties");
if (file.exists())
try (InputStream stream = new FileInputStream(file)) {
props.load(stream);
} catch (IOException e) {
throw new Error(e.getMessage());
}

String marathonValue = (String)options.valueOf("marathon");
if (marathonValue == null) marathonValue = props.getProperty("marathon");
if (marathonValue == null) throw new Error("Undefined marathon option");
try { marathon = new URL(marathonValue); }
catch (MalformedURLException e) { throw new Error("Invalid marathon value: " + marathonValue); }

String listenValue = (String)options.valueOf("listen");
if (listenValue == null) listenValue = props.getProperty("listen");
if (listenValue == null) throw new Error("Undefined listen option");
try { listen = new URL(listenValue); }
catch (MalformedURLException e) { throw new Error("Invalid listen value: " + listenValue); }

id = (String)options.valueOf("id");
instances = (Integer)options.valueOf("instances");

cpus = (double)options.valueOf("cpus");
mem = (int)options.valueOf("mem");
}

public String toString() {
String s = "";
s += "marathon:" + marathon + ", listen:" + listen;
s += "\nid:" + id + ", instances:" + instances + ", cpus:" + cpus+ ", mem:" + mem;
return s;
}
}

private static class Error extends java.lang.Error {
public Error(String message) { super(message); }
}
}

0 comments on commit c157501

Please sign in to comment.