use of com.nextdoor.bender.handler.HandlerException in project bender by Nextdoor.
the class Bender method main.
/**
* Main entrypoint for the Bender CLI tool - handles the argument parsing and triggers the
* appropriate methods for ultimately invoking a Bender Handler.
*
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
/*
* Create the various types of options that we support
*/
Option help = Option.builder("H").longOpt("help").desc("Print this message").build();
Option handler = Option.builder("h").longOpt("handler").hasArg().desc("Which Event Handler do you want to simulate? \n" + "Your options are: KinesisHandler, S3Handler. \n" + "Default: KinesisHandler").build();
Option source_file = Option.builder("s").longOpt("source_file").required().hasArg().desc("Reference to the file that you want to process. Usage depends " + "on the Handler you chose. If you chose KinesisHandler " + "then this is a local file (file://path/to/file). If you chose " + "S3Handler, then this is the path to the file in S3 that you want to process " + "(s3://bucket/file...)").build();
Option kinesis_stream_name = Option.builder().longOpt("kinesis_stream_name").hasArg().desc("What stream name should we mimic? " + "Default: " + KINESIS_STREAM_NAME + " (Kinesis Handler Only)").build();
/*
* Build out the option handler and parse the options
*/
Options options = new Options();
options.addOption(help);
options.addOption(handler);
options.addOption(kinesis_stream_name);
options.addOption(source_file);
/*
* Prepare our help formatter
*/
HelpFormatter formatter = new HelpFormatter();
formatter.setWidth(100);
formatter.setSyntaxPrefix("usage: BENDER_CONFIG=file://config.yaml java -jar");
/*
* Parse the options themselves. Throw an error and help if necessary.
*/
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch (UnrecognizedOptionException | MissingOptionException | MissingArgumentException e) {
logger.error(e.getMessage());
formatter.printHelp(name, options);
System.exit(1);
}
/*
* The CLI tool doesn't have any configuration files built into it. We require that the user set
* BENDER_CONFIG to something reasonable.
*/
if (System.getenv("BENDER_CONFIG") == null) {
logger.error("You must set the BENDER_CONFIG environment variable. \n" + "Valid options include: file://<file>");
formatter.printHelp(name, options);
System.exit(1);
}
if (cmd.hasOption("help")) {
formatter.printHelp(name, options);
System.exit(0);
}
/*
* Depending on the desired Handler, we invoke a specific method and pass in the options (or
* defaults) required for that handler.
*/
String handler_value = cmd.getOptionValue(handler.getLongOpt(), KINESIS);
try {
switch(handler_value.toLowerCase()) {
case KINESIS:
invokeKinesisHandler(cmd.getOptionValue(kinesis_stream_name.getLongOpt(), KINESIS_STREAM_NAME), cmd.getOptionValue(source_file.getLongOpt()));
break;
case S3:
invokeS3Handler(cmd.getOptionValue(source_file.getLongOpt()));
break;
/*
* Error out if an invalid handler was supplied.
*/
default:
logger.error("Invalid Handler Option (" + handler_value + "), valid options are: " + KINESIS);
formatter.printHelp(name, options);
System.exit(1);
}
} catch (HandlerException e) {
logger.error("Error executing handler: " + e);
System.exit(1);
}
}
Aggregations