diff --git a/cmd/config.go b/cmd/config.go index 2b10c24..2ace4e4 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -135,8 +135,9 @@ func InitConfig(output io.Writer) (*Config, error) { if conf.Examples { _, err := fmt.Fprintln(output, Examples) if err != nil { - Die(err) + Die("failed write to output file handle", err) } + os.Exit(0) } diff --git a/cmd/root.go b/cmd/root.go index b524134..818773e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -18,12 +18,13 @@ along with this program. If not, see . package cmd import ( + "fmt" "io" - "log" + "os" ) -func Die(err error) int { - log.Fatal("Error: ", err.Error()) +func Die(format string, err error) int { + fmt.Fprintf(os.Stderr, format+": %s\n", err) return 1 } @@ -31,13 +32,14 @@ func Die(err error) int { func Main(output io.Writer) int { conf, err := InitConfig(output) if err != nil { - return Die(err) + fmt.Println(1) + return Die("failed to initialize", err) } tp := NewTP(conf) if err := tp.ProcessTimestamps(); err != nil { - return Die(err) + return Die("failed to process timestamp[s]", err) } return 0 diff --git a/cmd/times.go b/cmd/times.go index 6398a10..006dc65 100644 --- a/cmd/times.go +++ b/cmd/times.go @@ -20,7 +20,6 @@ package cmd import ( "errors" "fmt" - "log" "regexp" "strconv" "time" @@ -60,23 +59,12 @@ func (tp *TimestampProccessor) ProcessTimestamps() error { case 1: return tp.SingleTimestamp(tp.Args[0]) case 2: - return tp.Calc(tp.Args[0], tp.Args[1]) + return tp.DualTimestamps(tp.Args[0], tp.Args[1]) } return nil } -func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error { - ts, err := tp.Parse(timestamp) - if err != nil { - return err - } - - tp.Print(ts) - - return nil -} - // Parse uses 3 different timestamp parser modules to provide maximum flexibility func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) { ts, err := anytime.Parse(timestamp, tp.Reference) @@ -94,7 +82,18 @@ func (tp *TimestampProccessor) Parse(timestamp string) (time.Time, error) { return dateparse.ParseAny(timestamp) } -func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error { +func (tp *TimestampProccessor) SingleTimestamp(timestamp string) error { + ts, err := tp.Parse(timestamp) + if err != nil { + return err + } + + tp.Print(ts) + + return nil +} + +func (tp *TimestampProccessor) DualTimestamps(timestampA, timestampB string) error { tsA, err := tp.Parse(timestampA) if err != nil { return err @@ -112,6 +111,12 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error { return err } + tp.CalcDiff(tsA, tsB) + + return nil +} + +func (tp *TimestampProccessor) CalcDiff(tsA time.Time, tsB time.Time) { switch tp.Mode { case ModeDiff: var diff time.Duration @@ -131,8 +136,6 @@ func (tp *TimestampProccessor) Calc(timestampA, timestampB string) error { tp.Print(TPdatetime{TimestampProccessor: *tp, Data: sum}) } - - return nil } func (tp *TimestampProccessor) CalcDuration(tsA time.Time, durB time.Duration) { @@ -151,7 +154,7 @@ func (tp *TimestampProccessor) CalcDuration(tsA time.Time, durB time.Duration) { func (tp *TimestampProccessor) Print(ts TimestampWriter) { _, err := fmt.Fprintln(tp.Output, ts.String()) if err != nil { - log.Fatalf("failed to print to given output handle: %s", err) + Die("failed to print to given output handle", err) } }