2016-10-01 15:35:06 +00:00
|
|
|
//
|
|
|
|
// imag - the personal information management suite for the commandline
|
|
|
|
// Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; version
|
|
|
|
// 2.1 of the License.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
//
|
|
|
|
|
2016-05-12 16:49:14 +00:00
|
|
|
extern crate crossbeam;
|
2016-08-06 15:43:28 +00:00
|
|
|
extern crate clap;
|
2016-05-12 16:49:14 +00:00
|
|
|
#[macro_use] extern crate version;
|
2016-09-01 09:24:31 +00:00
|
|
|
#[macro_use] extern crate log;
|
2016-05-12 16:49:14 +00:00
|
|
|
extern crate walkdir;
|
|
|
|
|
2016-08-06 15:44:28 +00:00
|
|
|
extern crate libimagrt;
|
2016-09-03 15:58:14 +00:00
|
|
|
extern crate libimagerror;
|
2016-08-06 15:44:28 +00:00
|
|
|
|
2016-05-12 16:49:14 +00:00
|
|
|
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-09-03 15:58:14 +00:00
|
|
|
use clap::{Arg, AppSettings, SubCommand};
|
2016-05-12 16:49:14 +00:00
|
|
|
|
2016-08-06 15:55:52 +00:00
|
|
|
use libimagrt::runtime::Runtime;
|
2016-09-03 15:58:14 +00:00
|
|
|
use libimagerror::trace::trace_error;
|
2016-07-21 22:08:11 +00:00
|
|
|
|
2016-09-07 09:07:02 +00:00
|
|
|
/// Returns the helptext, putting the Strings in cmds as possible
|
|
|
|
/// subcommands into it
|
2016-09-03 12:58:14 +00:00
|
|
|
fn help_text(cmds: Vec<String>) -> String {
|
|
|
|
let text = format!(r#"
|
2016-05-12 16:49:14 +00:00
|
|
|
|
|
|
|
_
|
|
|
|
(_)_ __ ___ __ _ __ _
|
|
|
|
| | '_ \` _ \/ _\`|/ _\`|
|
|
|
|
| | | | | | | (_| | (_| |
|
|
|
|
|_|_| |_| |_|\__,_|\__, |
|
|
|
|
|___/
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
2016-09-03 12:58:14 +00:00
|
|
|
{imagbins}
|
2016-05-12 16:49:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-09-03 12:58:14 +00:00
|
|
|
(c) 2016 Matthias Beyer and contributors"#, imagbins = cmds.into_iter()
|
|
|
|
.map(|cmd| format!("\t{}\n", cmd))
|
|
|
|
.fold(String::new(), |s, c| {
|
|
|
|
let s = s + c.as_str();
|
|
|
|
s
|
|
|
|
}));
|
|
|
|
text
|
2016-05-12 16:49:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-07 09:07:02 +00:00
|
|
|
/// Returns the list of imag-* executables found in $PATH
|
2016-05-12 16:49:14 +00:00
|
|
|
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-08-07 13:11:48 +00:00
|
|
|
.and_then(|s| s.splitn(2, "-").nth(1).map(String::from))
|
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-09-03 15:58:14 +00:00
|
|
|
|
|
|
|
fn main() {
|
2016-09-07 09:07:02 +00:00
|
|
|
// Initialize the Runtime and build the CLI
|
2016-09-03 15:58:14 +00:00
|
|
|
let appname = "imag";
|
|
|
|
let version = &version!();
|
|
|
|
let about = "imag - the PIM suite for the commandline";
|
|
|
|
let commands = get_commands();
|
2016-09-07 09:07:02 +00:00
|
|
|
let helptext = help_text(commands.clone());
|
2016-09-03 15:58:14 +00:00
|
|
|
let app = Runtime::get_default_cli_builder(appname, version, about)
|
2016-09-07 08:37:16 +00:00
|
|
|
.settings(&[AppSettings::AllowExternalSubcommands, AppSettings::ArgRequiredElseHelp])
|
2016-08-07 13:19:20 +00:00
|
|
|
.arg(Arg::with_name("version")
|
|
|
|
.long("version")
|
|
|
|
.takes_value(false)
|
|
|
|
.required(false)
|
|
|
|
.multiple(false)
|
|
|
|
.help("Get the version of imag"))
|
|
|
|
.arg(Arg::with_name("versions")
|
|
|
|
.long("versions")
|
|
|
|
.takes_value(false)
|
|
|
|
.required(false)
|
|
|
|
.multiple(false)
|
|
|
|
.help("Get the versions of the imag commands"))
|
2016-08-26 14:35:15 +00:00
|
|
|
.subcommand(SubCommand::with_name("help").help("Show help"))
|
2016-09-03 15:58:14 +00:00
|
|
|
.help(helptext.as_str());
|
|
|
|
let rt = Runtime::new(app)
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
println!("Runtime couldn't be setup. Exiting");
|
|
|
|
trace_error(&e);
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
let matches = rt.cli();
|
2016-08-06 15:55:52 +00:00
|
|
|
|
2016-09-01 09:24:53 +00:00
|
|
|
debug!("matches: {:?}", matches);
|
2016-08-07 12:31:34 +00:00
|
|
|
|
2016-09-07 09:07:02 +00:00
|
|
|
// Begin checking for arguments
|
|
|
|
|
2016-08-06 15:55:52 +00:00
|
|
|
if matches.is_present("version") {
|
2016-09-01 09:24:53 +00:00
|
|
|
debug!("Showing version");
|
2016-08-06 15:55:52 +00:00
|
|
|
println!("imag {}", &version!()[..]);
|
|
|
|
exit(0);
|
|
|
|
}
|
2016-05-13 12:41:19 +00:00
|
|
|
|
2016-08-06 15:55:52 +00:00
|
|
|
if matches.is_present("versions") {
|
2016-09-01 09:24:53 +00:00
|
|
|
debug!("Showing versions");
|
2016-08-06 15:55:52 +00:00
|
|
|
let mut result = vec![];
|
2016-09-07 10:57:34 +00:00
|
|
|
for command in commands.iter() {
|
2016-08-06 15:55:52 +00:00
|
|
|
result.push(crossbeam::scope(|scope| {
|
|
|
|
scope.spawn(|| {
|
2016-09-07 09:14:08 +00:00
|
|
|
let v = Command::new(format!("imag-{}",command)).arg("--version").output();
|
2016-08-06 15:55:52 +00:00
|
|
|
match v {
|
|
|
|
Ok(v) => match String::from_utf8(v.stdout) {
|
2016-09-07 09:24:36 +00:00
|
|
|
Ok(s) => format!("{:10} -> {}", command, s),
|
2016-05-12 16:49:14 +00:00
|
|
|
Err(e) => format!("Failed calling {} -> {:?}", command, e),
|
2016-08-06 15:55:52 +00:00
|
|
|
},
|
|
|
|
Err(e) => format!("Failed calling {} -> {:?}", command, e),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
2016-05-12 16:49:14 +00:00
|
|
|
|
2016-08-06 15:55:52 +00:00
|
|
|
for versionstring in result.into_iter().map(|handle| handle.join()) {
|
2016-09-07 10:53:38 +00:00
|
|
|
// The amount of newlines may differ depending on the subprocess
|
|
|
|
println!("{}", versionstring.trim());
|
2016-08-06 15:55:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-12 16:49:14 +00:00
|
|
|
|
2016-09-07 09:07:02 +00:00
|
|
|
// Matches any subcommand given
|
2016-09-02 14:43:31 +00:00
|
|
|
match matches.subcommand() {
|
|
|
|
(subcommand, Some(scmd)) => {
|
2016-09-07 09:07:02 +00:00
|
|
|
// Get all given arguments and further subcommands to pass to
|
|
|
|
// the imag-<> binary
|
|
|
|
// Providing no arguments is OK, and is therefore ignored here
|
2016-09-07 08:12:55 +00:00
|
|
|
let subcommand_args : Vec<&str> = match scmd.values_of("") {
|
|
|
|
Some(values) => values.collect(),
|
|
|
|
None => Vec::new()
|
|
|
|
};
|
2016-09-07 08:31:00 +00:00
|
|
|
|
2016-09-07 09:07:02 +00:00
|
|
|
// Typos happen, so check if the given subcommand is one found in $PATH
|
2016-09-07 10:57:34 +00:00
|
|
|
if !commands.contains(&String::from(subcommand)) {
|
2016-09-07 08:31:00 +00:00
|
|
|
println!("No such command: 'imag-{}'", subcommand);
|
|
|
|
println!("See 'imag --help' for available subcommands");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2016-09-02 07:48:02 +00:00
|
|
|
debug!("Calling 'imag-{}' with args: {:?}", subcommand, subcommand_args);
|
|
|
|
|
2016-09-07 09:07:02 +00:00
|
|
|
// Create a Command, and pass it the gathered arguments
|
2016-08-06 15:55:52 +00:00
|
|
|
match Command::new(format!("imag-{}", subcommand))
|
2016-05-12 16:49:14 +00:00
|
|
|
.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() {
|
2016-09-02 07:48:02 +00:00
|
|
|
debug!("{} exited with non-zero exit code: {:?}", subcommand, exit_status);
|
2016-08-06 15:55:52 +00:00
|
|
|
println!("{} exited with non-zero exit code", subcommand);
|
2016-09-07 08:16:36 +00:00
|
|
|
exit(exit_status.code().unwrap_or(1));
|
2016-05-12 16:49:14 +00:00
|
|
|
}
|
2016-09-02 07:48:02 +00:00
|
|
|
debug!("Successful exit!");
|
2016-05-12 16:49:14 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
Err(e) => {
|
2016-09-02 07:48:02 +00:00
|
|
|
debug!("Error calling the subcommand");
|
2016-05-12 16:49:14 +00:00
|
|
|
match e.kind() {
|
|
|
|
ErrorKind::NotFound => {
|
2016-09-07 08:31:00 +00:00
|
|
|
// With the check above, this absolutely should not happen.
|
|
|
|
// Keeping it to be safe
|
2016-08-06 15:55:52 +00:00
|
|
|
println!("No such command: 'imag-{}'", subcommand);
|
2016-09-03 10:39:21 +00:00
|
|
|
println!("See 'imag --help' for available subcommands");
|
2016-05-12 16:49:14 +00:00
|
|
|
exit(2);
|
|
|
|
},
|
|
|
|
ErrorKind::PermissionDenied => {
|
2016-08-06 15:55:52 +00:00
|
|
|
println!("No permission to execute: 'imag-{}'", subcommand);
|
2016-05-12 16:49:14 +00:00
|
|
|
exit(1);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
println!("Error spawning: {:?}", e);
|
2016-09-07 08:16:36 +00:00
|
|
|
exit(1);
|
2016-05-12 16:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 14:43:31 +00:00
|
|
|
},
|
2016-09-07 09:07:02 +00:00
|
|
|
// Calling for example 'imag --versions' will lead here, as this option does not exit.
|
|
|
|
// There's nothing to do in such a case
|
|
|
|
_ => {},
|
2016-09-02 14:43:31 +00:00
|
|
|
}
|
2016-05-12 16:49:14 +00:00
|
|
|
}
|