From fc2c29cf525fa641f76311f60529720f87b37d8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Thu, 15 Oct 2020 21:38:25 +0100 Subject: [PATCH] Refactored error output & exit --- z/finish.go | 18 +++++++++++------- z/root.go | 4 ++-- z/track.go | 15 +++++++++------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/z/finish.go b/z/finish.go index b350349..4b72ed3 100644 --- a/z/finish.go +++ b/z/finish.go @@ -2,7 +2,6 @@ package z import ( "os" - "log" "fmt" "time" "github.com/spf13/cobra" @@ -17,22 +16,25 @@ var finishCmd = &cobra.Command{ runningEntryId, err := database.GetRunningEntryId(user) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } if runningEntryId == "" { fmt.Printf("□ not running\n") - os.Exit(-1) + os.Exit(1) } runningEntry, err := database.GetEntry(user, runningEntryId) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } tmpEntry, err := NewEntry(runningEntry.ID, begin, finish, project, task, user) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } if begin != "" { @@ -55,7 +57,8 @@ var finishCmd = &cobra.Command{ _, err = database.FinishEntry(user, runningEntry) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } fmt.Printf(runningEntry.GetOutputForFinish()) @@ -73,6 +76,7 @@ func init() { var err error database, err = InitDatabase() if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } } diff --git a/z/root.go b/z/root.go index f1c3916..edc71ba 100644 --- a/z/root.go +++ b/z/root.go @@ -23,8 +23,8 @@ var rootCmd = &cobra.Command{ func Execute() { if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + fmt.Printf("△ %+v\n", err) + os.Exit(-1) } } diff --git a/z/track.go b/z/track.go index 4da62d7..6c794b6 100644 --- a/z/track.go +++ b/z/track.go @@ -2,7 +2,6 @@ package z import ( "os" - "log" "fmt" "github.com/spf13/cobra" ) @@ -16,24 +15,27 @@ var trackCmd = &cobra.Command{ runningEntryId, err := database.GetRunningEntryId(user) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } if runningEntryId != "" { fmt.Printf("▷ a task is already running\n") - os.Exit(-1) + os.Exit(1) } newEntry, err := NewEntry("", begin, finish, project, task, user) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } isRunning := newEntry.Finish.IsZero() _, err = database.AddEntry(user, newEntry, isRunning) if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } fmt.Printf(newEntry.GetOutputForTrack(isRunning)) @@ -52,6 +54,7 @@ func init() { var err error database, err = InitDatabase() if err != nil { - log.Fatal(err) + fmt.Printf("△ %+v\n", err) + os.Exit(1) } }