Refactor timetrack subcommands output for broken pipe errors

This commit is contained in:
Matthias Beyer 2018-02-14 18:21:00 +01:00
parent 6a341d3723
commit c2dfb2ef52
5 changed files with 99 additions and 76 deletions

View file

@ -17,6 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use std::str::FromStr; use std::str::FromStr;
use filters::filter::Filter; use filters::filter::Filter;
@ -25,6 +26,7 @@ use chrono::NaiveDateTime;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagerror::io::ToExitCode;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::error::TimeTrackError as TTE;
use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetrackingstore::TimeTrackStore;
@ -84,6 +86,7 @@ pub fn day(rt: &Runtime) -> i32 {
tags_filter.and(start_time_filter).and(end_time_filter) tags_filter.and(start_time_filter).and(end_time_filter)
}; };
let mut out = ::std::io::stdout();
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
@ -91,30 +94,31 @@ pub fn day(rt: &Runtime) -> i32 {
.filter(Option::is_some) .filter(Option::is_some)
.map(Option::unwrap) .map(Option::unwrap)
.filter(|e| filter.filter(e)) .filter(|e| filter.filter(e))
.fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { .map(|e| -> Result<_, TTE> {
acc.and_then(|_| { debug!("Processing {:?}", e.get_location());
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { Ok((tag, start, end))
(None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
}
Ok(())
})
}) })
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) {
(None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
}
.to_exit_code()
})
.collect::<Result<Vec<()>, _>>()
.map(|_| 0) .map(|_| 0)
.map_err_trace() .unwrap_or_else(|e| e.code())
.unwrap_or(1)
} }

View file

@ -17,6 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use std::str::FromStr; use std::str::FromStr;
use filters::filter::Filter; use filters::filter::Filter;
@ -24,6 +25,7 @@ use chrono::NaiveDateTime;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::io::ToExitCode;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::error::TimeTrackError as TTE;
@ -99,6 +101,8 @@ pub fn month(rt: &Runtime) -> i32 {
tags_filter.and(start_time_filter).and(end_time_filter) tags_filter.and(start_time_filter).and(end_time_filter)
}; };
let mut out = ::std::io::stdout();
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
@ -106,30 +110,31 @@ pub fn month(rt: &Runtime) -> i32 {
.filter(Option::is_some) .filter(Option::is_some)
.map(Option::unwrap) .map(Option::unwrap)
.filter(|e| filter.filter(e)) .filter(|e| filter.filter(e))
.fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { .map(|e| -> Result<_, TTE> {
acc.and_then(|_| { debug!("Processing {:?}", e.get_location());
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { Ok((tag, start, end))
(None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
}
Ok(())
})
}) })
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) {
(None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
}
.to_exit_code()
})
.collect::<Result<Vec<()>, _>>()
.map(|_| 0) .map(|_| 0)
.map_err_trace() .unwrap_or_else(|e| e.code())
.unwrap_or(1)
} }

View file

@ -17,6 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use std::str::FromStr; use std::str::FromStr;
use filters::filter::Filter; use filters::filter::Filter;
@ -25,6 +26,7 @@ use chrono::NaiveDateTime;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagerror::io::ToExitCode;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::error::TimeTrackError as TTE;
use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetrackingstore::TimeTrackStore;
@ -97,6 +99,7 @@ pub fn week(rt: &Runtime) -> i32 {
tags_filter.and(start_time_filter).and(end_time_filter) tags_filter.and(start_time_filter).and(end_time_filter)
}; };
let mut out = ::std::io::stdout();
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
@ -104,30 +107,31 @@ pub fn week(rt: &Runtime) -> i32 {
.filter(Option::is_some) .filter(Option::is_some)
.map(Option::unwrap) .map(Option::unwrap)
.filter(|e| filter.filter(e)) .filter(|e| filter.filter(e))
.fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { .map(|e| -> Result<_, TTE> {
acc.and_then(|_| { debug!("Processing {:?}", e.get_location());
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { Ok((tag, start, end))
(None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
}
Ok(())
})
}) })
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) {
(None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
}
.to_exit_code()
})
.collect::<Result<Vec<()>, _>>()
.map(|_| 0) .map(|_| 0)
.map_err_trace() .unwrap_or_else(|e| e.code())
.unwrap_or(1)
} }

View file

@ -17,6 +17,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// //
use std::io::Write;
use std::str::FromStr; use std::str::FromStr;
use filters::filter::Filter; use filters::filter::Filter;
@ -25,6 +26,7 @@ use chrono::NaiveDateTime;
use libimagerror::trace::trace_error; use libimagerror::trace::trace_error;
use libimagerror::trace::MapErrTrace; use libimagerror::trace::MapErrTrace;
use libimagerror::iter::TraceIterator; use libimagerror::iter::TraceIterator;
use libimagerror::io::ToExitCode;
use libimagstore::store::FileLockEntry; use libimagstore::store::FileLockEntry;
use libimagtimetrack::error::TimeTrackError as TTE; use libimagtimetrack::error::TimeTrackError as TTE;
use libimagtimetrack::timetrackingstore::TimeTrackStore; use libimagtimetrack::timetrackingstore::TimeTrackStore;
@ -96,6 +98,7 @@ pub fn year(rt: &Runtime) -> i32 {
tags_filter.and(start_time_filter).and(end_time_filter) tags_filter.and(start_time_filter).and(end_time_filter)
}; };
let mut out = ::std::io::stdout();
rt.store() rt.store()
.get_timetrackings() .get_timetrackings()
.map_err_trace_exit_unwrap(1) .map_err_trace_exit_unwrap(1)
@ -103,30 +106,31 @@ pub fn year(rt: &Runtime) -> i32 {
.filter(Option::is_some) .filter(Option::is_some)
.map(Option::unwrap) .map(Option::unwrap)
.filter(|e| filter.filter(e)) .filter(|e| filter.filter(e))
.fold(Ok(()), |acc: Result<(), ::libimagtimetrack::error::TimeTrackError>, e| { .map(|e| -> Result<_, TTE> {
acc.and_then(|_| { debug!("Processing {:?}", e.get_location());
debug!("Processing {:?}", e.get_location());
let tag = e.get_timetrack_tag()?; let tag = e.get_timetrack_tag()?;
debug!(" -> tag = {:?}", tag); debug!(" -> tag = {:?}", tag);
let start = e.get_start_datetime()?; let start = e.get_start_datetime()?;
debug!(" -> start = {:?}", start); debug!(" -> start = {:?}", start);
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
match (start, end) { Ok((tag, start, end))
(None, _) => println!("{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e),
}
Ok(())
})
}) })
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) {
(None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
}
.to_exit_code()
})
.collect::<Result<Vec<()>, _>>()
.map(|_| 0) .map(|_| 0)
.map_err_trace() .unwrap_or_else(|e| e.code())
.unwrap_or(1)
} }

View file

@ -25,6 +25,12 @@ impl From<i32> for ExitCode {
} }
} }
impl ExitCode {
pub fn code(self) -> i32 {
self.0
}
}
pub trait ExitUnwrap<T> { pub trait ExitUnwrap<T> {
fn unwrap_or_exit(self) -> T; fn unwrap_or_exit(self) -> T;
} }