zeit/z/finish.go

83 lines
2.2 KiB
Go
Raw Normal View History

package z
import (
2020-10-14 23:22:36 +00:00
"os"
"fmt"
"time"
"github.com/spf13/cobra"
)
var finishCmd = &cobra.Command{
Use: "finish",
2020-10-15 20:56:04 +00:00
Short: "Finish currently running activity",
Long: "Finishing tracking of currently running activity.",
Run: func(cmd *cobra.Command, args []string) {
user := GetCurrentUser()
runningEntryId, err := database.GetRunningEntryId(user)
if err != nil {
2020-10-15 20:38:25 +00:00
fmt.Printf("△ %+v\n", err)
os.Exit(1)
}
if runningEntryId == "" {
2020-10-14 23:22:36 +00:00
fmt.Printf("□ not running\n")
2020-10-15 20:38:25 +00:00
os.Exit(1)
}
runningEntry, err := database.GetEntry(user, runningEntryId)
if err != nil {
2020-10-15 20:38:25 +00:00
fmt.Printf("△ %+v\n", err)
os.Exit(1)
}
2020-10-14 23:22:36 +00:00
tmpEntry, err := NewEntry(runningEntry.ID, begin, finish, project, task, user)
if err != nil {
2020-10-15 20:38:25 +00:00
fmt.Printf("△ %+v\n", err)
os.Exit(1)
}
2020-10-14 23:22:36 +00:00
if begin != "" {
runningEntry.Begin = tmpEntry.Begin
}
if finish != "" {
runningEntry.Finish = tmpEntry.Finish
} else {
runningEntry.Finish = time.Now()
}
2020-10-14 23:22:36 +00:00
if project != "" {
runningEntry.Project = tmpEntry.Project
}
if task != "" {
runningEntry.Task = tmpEntry.Task
}
_, err = database.FinishEntry(user, runningEntry)
if err != nil {
2020-10-15 20:38:25 +00:00
fmt.Printf("△ %+v\n", err)
os.Exit(1)
2020-10-14 23:22:36 +00:00
}
2020-10-15 20:38:03 +00:00
fmt.Printf(runningEntry.GetOutputForFinish())
return
},
}
func init() {
rootCmd.AddCommand(finishCmd)
2020-10-15 20:56:04 +00:00
finishCmd.Flags().StringVarP(&begin, "begin", "b", "", "Time the activity should begin 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).")
finishCmd.Flags().StringVarP(&finish, "finish", "s", "", "Time the activity 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")
var err error
database, err = InitDatabase()
if err != nil {
2020-10-15 20:38:25 +00:00
fmt.Printf("△ %+v\n", err)
os.Exit(1)
}
}