1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![crate_name = "mentat_cli"]
#[macro_use] extern crate log;
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate error_chain;
extern crate combine;
extern crate env_logger;
extern crate getopts;
extern crate linefeed;
extern crate rusqlite;
extern crate tabwriter;
extern crate termion;
extern crate time;
extern crate mentat;
extern crate edn;
extern crate mentat_query;
extern crate mentat_core;
extern crate mentat_db;
use getopts::Options;
use termion::{
color,
};
static BLUE: color::Rgb = color::Rgb(0x99, 0xaa, 0xFF);
static GREEN: color::Rgb = color::Rgb(0x77, 0xFF, 0x99);
pub mod command_parser;
pub mod input;
pub mod repl;
pub mod errors;
pub fn run() -> i32 {
env_logger::init();
let args = std::env::args().collect::<Vec<_>>();
let mut opts = Options::new();
opts.optopt("d", "", "The path to a database to open", "DATABASE");
if cfg!(feature = "sqlcipher") {
opts.optopt("k", "key", "The key to use to open the database (only available when using sqlcipher)", "KEY");
}
opts.optflag("h", "help", "Print this help message and exit");
opts.optmulti("q", "query", "Execute a query on startup. Queries are executed after any transacts.", "QUERY");
opts.optmulti("t", "transact", "Execute a transact on startup. Transacts are executed before queries.", "TRANSACT");
opts.optmulti("i", "import", "Execute an import on startup. Imports are executed before queries.", "PATH");
opts.optflag("v", "version", "Print version and exit");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(e) => {
println!("{}: {}", args[0], e);
return 1;
}
};
if matches.opt_present("version") {
print_version();
return 0;
}
if matches.opt_present("help") {
print_usage(&args[0], &opts);
return 0;
}
let key = matches.opt_str("key");
if key.is_some() && !cfg!(feature = "sqlcipher") {
eprintln!("Decryption key provided, but this build does not have sqlcipher support");
return 1;
}
let mut last_arg: Option<&str> = None;
let cmds:Vec<command_parser::Command> = args.iter().filter_map(|arg| {
match last_arg {
Some("-d") => {
last_arg = None;
if let &Some(ref k) = &key {
Some(command_parser::Command::OpenEncrypted(arg.clone(), k.clone()))
} else {
Some(command_parser::Command::Open(arg.clone()))
}
},
Some("-q") => {
last_arg = None;
Some(command_parser::Command::Query(arg.clone()))
},
Some("-i") => {
last_arg = None;
Some(command_parser::Command::Import(arg.clone()))
},
Some("-t") => {
last_arg = None;
Some(command_parser::Command::Transact(arg.clone()))
},
Some(_) |
None => {
last_arg = Some(&arg);
None
},
}
}).collect();
let repl = repl::Repl::new();
if repl.is_ok() {
repl.unwrap().run(Some(cmds));
} else {
println!("{}", repl.err().unwrap());
}
0
}
pub fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
fn print_usage(arg0: &str, opts: &Options) {
print!("{}", opts.usage(&format!(
"Usage: {} [OPTIONS] [FILE]", arg0)));
}
fn print_version() {
println!("mentat {}", version());
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}