Fix single quote issues and make error messages better

This commit is contained in:
Dheepak Krishnamurthy 2020-10-23 20:42:48 -06:00
parent 70e570ed9d
commit 17c2411a44

View file

@ -52,7 +52,7 @@ pub fn cmp(t1: &Task, t2: &Task) -> Ordering {
UDAValue::U64(u) => *u as f64, UDAValue::U64(u) => *u as f64,
UDAValue::F64(f) => *f, UDAValue::F64(f) => *f,
}; };
urgency2.partial_cmp(&urgency1).unwrap() urgency2.partial_cmp(&urgency1).unwrap_or(Ordering::Less)
} }
pub enum DateState { pub enum DateState {
@ -975,6 +975,7 @@ impl TTApp {
} else { } else {
self.filter.as_str().into() self.filter.as_str().into()
}; };
match shlex::split(&filter) { match shlex::split(&filter) {
Some(cmd) => { Some(cmd) => {
for s in cmd { for s in cmd {
@ -989,7 +990,7 @@ impl TTApp {
let output = task let output = task
.output() .output()
.expect("Unable to run `task export`. Check documentation for more information."); .expect("Unable to run `task export`. Check documentation for more information.");
let data = String::from_utf8(output.stdout).unwrap(); let data = String::from_utf8(output.stdout).unwrap_or_default();
let imported = import(data.as_bytes()); let imported = import(data.as_bytes());
{ {
if let Ok(i) = imported { if let Ok(i) = imported {
@ -1004,9 +1005,13 @@ impl TTApp {
return Ok(()); return Ok(());
} }
match shlex::split(&self.command) { let shell = self.command.as_str().replace("'", "\\'");
match shlex::split(&shell) {
Some(cmd) => { Some(cmd) => {
// first argument must be a binary
let mut command = Command::new(&cmd[0]); let mut command = Command::new(&cmd[0]);
// remaining arguments are args
for (i, s) in cmd.iter().enumerate() { for (i, s) in cmd.iter().enumerate() {
if i == 0 { if i == 0 {
continue; continue;
@ -1021,11 +1026,11 @@ impl TTApp {
} }
Err(_) => Err(format!( Err(_) => Err(format!(
"Shell command `{}` exited with non-zero output", "Shell command `{}` exited with non-zero output",
self.command.as_str() shell,
)), )),
} }
} }
None => Err(format!("Unable to split `{}`", self.command.as_str())), None => Err(format!("Cannot run subprocess. Unable to shlex split `{}`", shell)),
} }
} }
@ -1038,7 +1043,9 @@ impl TTApp {
command.arg("log"); command.arg("log");
match shlex::split(&self.command) { let shell = self.command.as_str().replace("'", "\\'");
match shlex::split(&shell) {
Some(cmd) => { Some(cmd) => {
for s in cmd { for s in cmd {
command.arg(&s); command.arg(&s);
@ -1049,12 +1056,10 @@ impl TTApp {
self.command.update("", 0); self.command.update("", 0);
Ok(()) Ok(())
} }
Err(_) => { Err(_) => Err(format!("Cannot run `task log {}`. Check documentation for more information", shell))
Err("Cannot run `task log` for task `{}`. Check documentation for more information".to_string())
} }
} }
} None => Err(format!("Unable to run `task log`. Cannot shlex split `{}`", shell.as_str())),
None => Err(format!("Unable to run `task log` with `{}`", self.command.as_str())),
} }
} }
@ -1067,7 +1072,9 @@ impl TTApp {
let mut command = Command::new("task"); let mut command = Command::new("task");
command.arg(format!("{}", task_id)).arg("modify"); command.arg(format!("{}", task_id)).arg("modify");
match shlex::split(&self.modify) { let shell = self.modify.as_str().replace("'", "\\'");
match shlex::split(&shell) {
Some(cmd) => { Some(cmd) => {
for s in cmd { for s in cmd {
command.arg(&s); command.arg(&s);
@ -1079,15 +1086,15 @@ impl TTApp {
Ok(()) Ok(())
} }
Err(_) => Err(format!( Err(_) => Err(format!(
"Cannot run `task modify` for task `{}`. Check documentation for more information", "Cannot run `task {} modify {}`. Check documentation for more information",
task_id task_id, shell,
)), )),
} }
} }
None => Err(format!( None => Err(format!(
"Unable to run `task modify` with `{}` on task {}", "Unable to run `task {} modify`. Cannot shlex split `{}`",
self.modify.as_str(), task_id,
&task_id shell,
)), )),
} }
} }
@ -1101,7 +1108,9 @@ impl TTApp {
let mut command = Command::new("task"); let mut command = Command::new("task");
command.arg(format!("{}", task_id)).arg("annotate"); command.arg(format!("{}", task_id)).arg("annotate");
match shlex::split(&self.command) { let shell = self.command.as_str().replace("'", "\\'");
match shlex::split(&shell) {
Some(cmd) => { Some(cmd) => {
for s in cmd { for s in cmd {
command.arg(&s); command.arg(&s);
@ -1112,10 +1121,10 @@ impl TTApp {
self.command.update("", 0); self.command.update("", 0);
Ok(()) Ok(())
} }
Err(_) => Err("Cannot run `task annotate`. Check documentation for more information".to_string()), Err(_) => Err(format!("Cannot run `task {} annotate {}`. Check documentation for more information", task_id, shell)),
} }
} }
None => Err(format!("Unable to run `task add` with `{}`", self.command.as_str())), None => Err(format!("Unable to run `task {} annotate`. Cannot shlex split `{}`", task_id, shell)),
} }
} }
@ -1123,7 +1132,9 @@ impl TTApp {
let mut command = Command::new("task"); let mut command = Command::new("task");
command.arg("add"); command.arg("add");
match shlex::split(&self.command) { let shell = self.command.as_str().replace("'", "\\'");
match shlex::split(&shell) {
Some(cmd) => { Some(cmd) => {
for s in cmd { for s in cmd {
command.arg(&s); command.arg(&s);
@ -1134,10 +1145,10 @@ impl TTApp {
self.command.update("", 0); self.command.update("", 0);
Ok(()) Ok(())
} }
Err(_) => Err("Cannot run `task add`. Check documentation for more information".to_string()), Err(_) => Err(format!("Cannot run `task add {}`. Check documentation for more information", shell)),
} }
} }
None => Err(format!("Unable to run `task add` with `{}`", self.command.as_str())), None => Err(format!("Unable to run `task add`. Cannot shlex split `{}`", shell)),
} }
} }
@ -1146,7 +1157,7 @@ impl TTApp {
match output { match output {
Ok(output) => { Ok(output) => {
let data = String::from_utf8(output.stdout).unwrap(); let data = String::from_utf8(output.stdout).unwrap_or_default();
for line in data.split('\n') { for line in data.split('\n') {
if line.starts_with("Virtual tags") { if line.starts_with("Virtual tags") {
let line = line.to_string(); let line = line.to_string();
@ -1255,8 +1266,8 @@ impl TTApp {
Err(format!( Err(format!(
"`task edit` for task `{}` failed. {}{}", "`task edit` for task `{}` failed. {}{}",
task_id, task_id,
String::from_utf8(output.stdout).unwrap(), String::from_utf8(output.stdout).unwrap_or_default(),
String::from_utf8(output.stderr).unwrap() String::from_utf8(output.stderr).unwrap_or_default()
)) ))
} else { } else {
Ok(()) Ok(())