From 30d61dc7d1e88a6dff5f80582e7b9cf58561812c 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 00:22:36 +0100 Subject: [PATCH] Extended finish command --- z/finish.go | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/z/finish.go b/z/finish.go index 5d585e5..561a411 100644 --- a/z/finish.go +++ b/z/finish.go @@ -1,9 +1,12 @@ package z import ( + "os" "log" + "fmt" "time" "github.com/spf13/cobra" + "github.com/gookit/color" ) var finishCmd = &cobra.Command{ @@ -19,7 +22,8 @@ var finishCmd = &cobra.Command{ } if runningEntryId == "" { - log.Fatal("Tracker not running!") + fmt.Printf("□ not running\n") + os.Exit(-1) } runningEntry, err := database.GetEntry(user, runningEntryId) @@ -27,20 +31,43 @@ var finishCmd = &cobra.Command{ log.Fatal(err) } - runningEntry.Finish = time.Now() - - entryId, err := database.FinishEntry(user, runningEntry) + tmpEntry, err := NewEntry(runningEntry.ID, begin, finish, project, task, user) if err != nil { log.Fatal(err) } - // entries, err := database.ListEntries() - // if err != nil { - // log.Fatal(err) - // } - // fmt.Printf("%+v", entries) + if begin != "" { + runningEntry.Begin = tmpEntry.Begin + } - log.Printf("Finished entry with ID %s!\n", entryId) + if finish != "" { + runningEntry.Finish = tmpEntry.Finish + } else { + runningEntry.Finish = time.Now() + } + + if project != "" { + runningEntry.Project = tmpEntry.Project + } + + if task != "" { + runningEntry.Task = tmpEntry.Task + } + + _, err = database.FinishEntry(user, runningEntry) + if err != nil { + log.Fatal(err) + } + + if runningEntry.Task != "" && runningEntry.Project != "" { + fmt.Printf("□ finished tracking %s on %s\n", color.FgLightWhite.Render(runningEntry.Task), color.FgLightWhite.Render(runningEntry.Project)) + } else if runningEntry.Task != "" && runningEntry.Project == "" { + fmt.Printf("□ finished tracking %s\n", color.FgLightWhite.Render(runningEntry.Task)) + } else if runningEntry.Task == "" && runningEntry.Project != "" { + fmt.Printf("□ finished tracking task on %s\n", color.FgLightWhite.Render(runningEntry.Project)) + } else { + fmt.Printf("□ finished tracking task\n") + } return }, } @@ -51,7 +78,6 @@ func init() { finishCmd.Flags().StringVarP(&finish, "finish", "s", "", "Time the entry should finish at\n\nEither in the formats 16:00 / 4:00PM \nor relative to the current time, \ne.g. -0:15 (now minus 15 minutes), +1.50 (now plus 1:30h).\nMust be after --begin time.") finishCmd.Flags().StringVarP(&project, "project", "p", "", "Project to be assigned") finishCmd.Flags().StringVarP(&task, "task", "t", "", "Task to be assigned") - finishCmd.Flags().BoolVarP(&force, "force", "f", false, "Force begin finishing of a new task \neven though another one is still running \n(ONLY IF YOU KNOW WHAT YOU'RE DOING!)") var err error database, err = InitDatabase()