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,8 +94,7 @@ 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()?;
@ -104,17 +106,19 @@ pub fn day(rt: &Runtime) -> i32 {
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
Ok((tag, start, end))
})
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
} }
.to_exit_code()
Ok(())
})
}) })
.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,8 +110,7 @@ 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()?;
@ -119,17 +122,19 @@ pub fn month(rt: &Runtime) -> i32 {
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
Ok((tag, start, end))
})
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
} }
.to_exit_code()
Ok(())
})
}) })
.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,8 +107,7 @@ 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()?;
@ -117,17 +119,19 @@ pub fn week(rt: &Runtime) -> i32 {
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
Ok((tag, start, end))
})
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
} }
.to_exit_code()
Ok(())
})
}) })
.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,8 +106,7 @@ 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()?;
@ -116,17 +118,19 @@ pub fn year(rt: &Runtime) -> i32 {
let end = e.get_end_datetime()?; let end = e.get_end_datetime()?;
debug!(" -> end = {:?}", end); debug!(" -> end = {:?}", end);
Ok((tag, start, end))
})
.trace_unwrap_exit(1)
.map(|(tag, start, end)| {
match (start, end) { match (start, end) {
(None, _) => println!("{} has no start time.", tag), (None, _) => writeln!(out, "{} has no start time.", tag),
(Some(s), None) => println!("{} | {} - ...", tag, s), (Some(s), None) => writeln!(out, "{} | {} - ...", tag, s),
(Some(s), Some(e)) => println!("{} | {} - {}", tag, s, e), (Some(s), Some(e)) => writeln!(out, "{} | {} - {}", tag, s, e),
} }
.to_exit_code()
Ok(())
})
}) })
.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;
} }