2016-05-12 16:49:14 +00:00
|
|
|
extern crate crossbeam;
|
|
|
|
#[macro_use] extern crate version;
|
|
|
|
extern crate walkdir;
|
|
|
|
|
|
|
|
use std::env;
|
|
|
|
use std::process::exit;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::process::Stdio;
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
use crossbeam::*;
|
|
|
|
|
2016-07-21 22:08:11 +00:00
|
|
|
const DBG_FLAG: &'static str = "--debug";
|
|
|
|
|
2016-05-12 16:49:14 +00:00
|
|
|
fn help(cmds: Vec<String>) {
|
|
|
|
println!(r#"
|
|
|
|
|
|
|
|
_
|
|
|
|
(_)_ __ ___ __ _ __ _
|
|
|
|
| | '_ \` _ \/ _\`|/ _\`|
|
|
|
|
| | | | | | | (_| | (_| |
|
|
|
|
|_|_| |_| |_|\__,_|\__, |
|
|
|
|
|___/
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
Usage: imag [--version | --versions | -h | --help] <command> <args...>
|
|
|
|
|
|
|
|
imag - the personal information management suite for the commandline
|
|
|
|
|
|
|
|
imag is a PIM suite for the commandline. It consists of several commands,
|
|
|
|
called "modules". Each module implements one PIM aspect and all of these
|
|
|
|
modules can be used independently.
|
|
|
|
|
|
|
|
Available commands:
|
|
|
|
"#);
|
|
|
|
|
|
|
|
for cmd in cmds.iter() {
|
|
|
|
println!("\t{}", cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
println!(r#"
|
|
|
|
|
|
|
|
Call a command with 'imag <command> <args>'
|
|
|
|
Each command can be called with "--help" to get the respective helptext.
|
|
|
|
|
|
|
|
Please visit https://github.com/matthiasbeyer/imag to view the source code,
|
|
|
|
follow the development of imag or maybe even contribute to imag.
|
|
|
|
|
|
|
|
imag is free software. It is released under the terms of LGPLv2.1
|
|
|
|
|
|
|
|
(c) 2016 Matthias Beyer and contributors"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_commands() -> Vec<String> {
|
|
|
|
let path = env::var("PATH");
|
|
|
|
if path.is_err() {
|
|
|
|
println!("PATH error: {:?}", path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
let pathelements = path.unwrap();
|
|
|
|
let pathelements = pathelements.split(":");
|
|
|
|
|
|
|
|
let joinhandles : Vec<ScopedJoinHandle<Vec<String>>> = pathelements
|
|
|
|
.map(|elem| {
|
|
|
|
crossbeam::scope(|scope| {
|
|
|
|
scope.spawn(|| {
|
|
|
|
WalkDir::new(elem)
|
|
|
|
.max_depth(1)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|path| {
|
|
|
|
match path {
|
2016-07-04 14:23:45 +00:00
|
|
|
&Ok(ref p) => p.file_name()
|
|
|
|
.to_str()
|
|
|
|
.map_or(false, |filename| filename.starts_with("imag-")),
|
2016-05-12 16:49:14 +00:00
|
|
|
&Err(_) => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.filter_map(|x| x.ok())
|
2016-07-04 14:23:45 +00:00
|
|
|
.filter_map(|path| {
|
|
|
|
path.file_name()
|
|
|
|
.to_str()
|
2016-07-21 11:41:09 +00:00
|
|
|
.and_then(|s| s.splitn(2, "-").nth(1).map(|s| format!("imag {}", s)))
|
2016-05-12 16:49:14 +00:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut execs = vec![];
|
|
|
|
for joinhandle in joinhandles.into_iter() {
|
|
|
|
let mut v = joinhandle.join();
|
|
|
|
execs.append(&mut v);
|
|
|
|
}
|
|
|
|
|
|
|
|
execs
|
|
|
|
}
|
|
|
|
|
2016-05-13 12:41:11 +00:00
|
|
|
fn find_command() -> Option<String> {
|
|
|
|
env::args().skip(1).filter(|x| !x.starts_with("-")).next()
|
|
|
|
}
|
|
|
|
|
2016-07-19 18:38:52 +00:00
|
|
|
fn find_flag() -> Option<String> {
|
|
|
|
env::args().skip(1).filter(|x| x.starts_with("-")).next()
|
|
|
|
}
|
|
|
|
|
2016-07-21 22:08:11 +00:00
|
|
|
fn is_debug_flag<T: AsRef<str>>(ref s: &T) -> bool {
|
|
|
|
s.as_ref() == DBG_FLAG
|
|
|
|
}
|
|
|
|
|
2016-05-13 12:41:19 +00:00
|
|
|
fn find_args(command: &str) -> Vec<String> {
|
|
|
|
env::args()
|
|
|
|
.skip(1)
|
|
|
|
.position(|e| e == command)
|
|
|
|
.map(|pos| env::args().skip(pos + 2).collect::<Vec<String>>())
|
|
|
|
.unwrap_or(vec![])
|
|
|
|
}
|
|
|
|
|
2016-05-12 16:49:14 +00:00
|
|
|
fn main() {
|
|
|
|
let commands = get_commands();
|
|
|
|
let mut args = env::args();
|
|
|
|
let _ = args.next();
|
2016-05-13 12:41:11 +00:00
|
|
|
let first_arg = match find_command() {
|
2016-05-12 16:49:14 +00:00
|
|
|
Some(s) => s,
|
2016-07-19 18:38:52 +00:00
|
|
|
None => match find_flag() {
|
|
|
|
Some(s) => s,
|
|
|
|
None => {
|
|
|
|
help(commands);
|
|
|
|
exit(0);
|
|
|
|
},
|
2016-05-12 16:49:14 +00:00
|
|
|
},
|
|
|
|
};
|
2016-07-21 22:08:11 +00:00
|
|
|
let is_debug = env::args().skip(1).find(is_debug_flag).is_some();
|
2016-05-12 16:49:14 +00:00
|
|
|
|
|
|
|
match &first_arg[..] {
|
|
|
|
"--help" | "-h" => {
|
|
|
|
help(commands);
|
|
|
|
exit(0);
|
|
|
|
},
|
|
|
|
|
|
|
|
"--version" => println!("imag {}", &version!()[..]),
|
|
|
|
|
|
|
|
"--versions" => {
|
|
|
|
let mut result = vec![];
|
|
|
|
for command in commands.iter() {
|
|
|
|
result.push(crossbeam::scope(|scope| {
|
|
|
|
scope.spawn(|| {
|
|
|
|
let v = Command::new(command).arg("--version").output();
|
|
|
|
match v {
|
|
|
|
Ok(v) => match String::from_utf8(v.stdout) {
|
|
|
|
Ok(s) => format!("{} -> {}", command, s),
|
|
|
|
Err(e) => format!("Failed calling {} -> {:?}", command, e),
|
|
|
|
},
|
|
|
|
Err(e) => format!("Failed calling {} -> {:?}", command, e),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
for versionstring in result.into_iter().map(|handle| handle.join()) {
|
|
|
|
println!("{}", versionstring);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
s => {
|
2016-07-19 18:57:49 +00:00
|
|
|
let mut subcommand_args = find_args(s);
|
2016-07-21 22:08:11 +00:00
|
|
|
if is_debug && subcommand_args.iter().find(is_debug_flag).is_none() {
|
2016-07-21 22:26:22 +00:00
|
|
|
subcommand_args.insert(0, String::from(DBG_FLAG));
|
2016-07-19 18:43:48 +00:00
|
|
|
}
|
2016-05-12 16:49:14 +00:00
|
|
|
match Command::new(format!("imag-{}", s))
|
|
|
|
.stdin(Stdio::inherit())
|
|
|
|
.stdout(Stdio::inherit())
|
|
|
|
.stderr(Stdio::inherit())
|
2016-07-19 18:57:49 +00:00
|
|
|
.args(&subcommand_args[..])
|
2016-05-12 16:49:14 +00:00
|
|
|
.spawn()
|
|
|
|
.and_then(|mut handle| handle.wait())
|
|
|
|
{
|
|
|
|
Ok(exit_status) => {
|
|
|
|
if !exit_status.success() {
|
|
|
|
println!("{} exited with non-zero exit code", s);
|
|
|
|
exit(exit_status.code().unwrap_or(42));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
Err(e) => {
|
|
|
|
match e.kind() {
|
|
|
|
ErrorKind::NotFound => {
|
|
|
|
println!("No such command: 'imag-{}'", s);
|
|
|
|
exit(2);
|
|
|
|
},
|
|
|
|
ErrorKind::PermissionDenied => {
|
|
|
|
println!("No permission to execute: 'imag-{}'", s);
|
|
|
|
exit(1);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
println!("Error spawning: {:?}", e);
|
|
|
|
exit(1337);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|