Extended finish command

This commit is contained in:
マリウス 2020-10-15 00:22:36 +01:00
parent 2078a3f675
commit 30d61dc7d1
No known key found for this signature in database
GPG key ID: C228EF0A530AF06F

View file

@ -1,9 +1,12 @@
package z package z
import ( import (
"os"
"log" "log"
"fmt"
"time" "time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/gookit/color"
) )
var finishCmd = &cobra.Command{ var finishCmd = &cobra.Command{
@ -19,7 +22,8 @@ var finishCmd = &cobra.Command{
} }
if runningEntryId == "" { if runningEntryId == "" {
log.Fatal("Tracker not running!") fmt.Printf("□ not running\n")
os.Exit(-1)
} }
runningEntry, err := database.GetEntry(user, runningEntryId) runningEntry, err := database.GetEntry(user, runningEntryId)
@ -27,20 +31,43 @@ var finishCmd = &cobra.Command{
log.Fatal(err) log.Fatal(err)
} }
runningEntry.Finish = time.Now() tmpEntry, err := NewEntry(runningEntry.ID, begin, finish, project, task, user)
entryId, err := database.FinishEntry(user, runningEntry)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
// entries, err := database.ListEntries() if begin != "" {
// if err != nil { runningEntry.Begin = tmpEntry.Begin
// log.Fatal(err) }
// }
// fmt.Printf("%+v", entries)
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 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(&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(&project, "project", "p", "", "Project to be assigned")
finishCmd.Flags().StringVarP(&task, "task", "t", "", "Task 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 var err error
database, err = InitDatabase() database, err = InitDatabase()