Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
Add run subcommand for running factfiles (closes #65, closes #70)
Browse files Browse the repository at this point in the history
This commit converts to the docopt crate from getopts for
getting command line arguments. This prevents panics on invalid
arguments.

This commit also fixes a few compiler nags about unused imports.
  • Loading branch information
ninjabear committed Sep 30, 2016
1 parent 701763a commit bec54e9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.2.0"
authors = ["Ed Lewis <support@snowplowanalytics.com>"]

[dependencies]
getopts = "0.2"
docopt = "0.6"
log = "0.3.5"
log4rs = "0.3.3"
daggy = "0.4.0"
Expand Down
1 change: 0 additions & 1 deletion src/factotum/parser/schemavalidator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
mod tests;

use valico::json_schema;
use valico::common::error::*;
use rustc_serialize::json::{Json, error_str};

use rustc_serialize::json::ParserError::{self, SyntaxError, IoError};
Expand Down
1 change: 0 additions & 1 deletion src/factotum/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use factotum::factfile::Task;
use factotum::factfile::OnResult;
use daggy::*;

pub fn compare_tasks(expected:Vec<Vec<&str>>, actual:Vec<Vec<&Task>>) {
for i in 0..expected.len() {
Expand Down
67 changes: 31 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
#[macro_use]
extern crate log;
extern crate log4rs;
extern crate getopts;
extern crate docopt;
extern crate daggy;
extern crate rustc_serialize;
extern crate valico;
extern crate colored;
extern crate chrono;

use getopts::Options;
use std::env;
use docopt::Docopt;
use std::fs;
use factotum::executor::ExecutionResult;
use factotum::executor::TaskExecutionResult;
Expand All @@ -39,6 +38,27 @@ const PROC_PARSE_ERROR: i32 = 1;
const PROC_EXEC_ERROR: i32 = 2;
const PROC_OTHER_ERROR: i32 = 3;

const USAGE: &'static str = "
Factotum.
Usage:
factotum run <factfile> [--start=<start_task>] [--env=<env>]
factotum (-h | --help)
Options:
-h --help Show this screen.
--start=<start_task> Begin at specified task.
--env=<env> Supply JSON to define mustache variables in Factfile.
";

#[derive(Debug, RustcDecodable)]
struct Args {
flag_start: Option<String>,
flag_env: Option<String>,
arg_factfile: String,
cmd_run: bool
}

// macro to simplify printing to stderr
// https://github.com/rust-lang/rfcs/issues/1078
macro_rules! print_err {
Expand All @@ -54,11 +74,6 @@ macro_rules! print_err {
)
}

fn print_usage(program:&str, opts:Options) {
let brief = format!("Usage: {} FILE", program);
print!("{}", opts.usage(&brief))
}

fn get_duration_as_string(d:&Duration) -> String {
// duration doesn't support the normal display format
// for now lets put together something that produces some sensible output
Expand Down Expand Up @@ -285,35 +300,15 @@ fn main() {
fn factotum() -> i32 {
init_logger();

let args: Vec<String> = env::args().collect();
let program = args[0].clone();

let mut opts = Options::new();
opts.optflag("h","help", "Print out this help menu");
opts.optopt("e", "env", "A JSON string to be used to 'fill in' variables", "JSON");
opts.optopt("s", "start", "Start from the specified task name rather than the beginning of the job", "TASK_NAME");

let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => panic!(f.to_string())
};

if matches.opt_present("h") {
print_usage(&program, opts);
return PROC_OTHER_ERROR;
}

let env:Option<String> = matches.opt_str("e");
let start_task:Option<String> = matches.opt_str("s");

let inputfile = if !matches.free.is_empty() {
matches.free[0].clone()
} else {
print_usage(&program, opts);
return PROC_OTHER_ERROR;
};
let args: Args = match Docopt::new(USAGE).and_then(|d| d.decode()) {
Ok(a) => a,
Err(e) => {
print!("{}", e);
return PROC_OTHER_ERROR
}
};

parse_file_and_execute(&inputfile, env, start_task)
parse_file_and_execute(&args.arg_factfile, args.flag_env, args.flag_start)
}

#[test]
Expand Down

0 comments on commit bec54e9

Please sign in to comment.