Add resume Cmd

Add resume Cmd
This commit is contained in:
Martin (Schretzi) Fuchsluger
2025-02-17 19:23:04 +01:00
parent 272e999605
commit 057b2fcf2e
2 changed files with 65 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
package z
import (
"github.com/spf13/cobra"
)
var resumeCmd = &cobra.Command{
Use: "resume",
Short: "Resume last task",
Long: "Track new activity with all parameters of the last task (based on begin time)",
Run: func(cmd *cobra.Command, args []string) {
resumeTask(1)
},
}
func init() {
rootCmd.AddCommand(resumeCmd)
resumeCmd.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).")
resumeCmd.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.")
}
+44
View File
@@ -226,3 +226,47 @@ func taskGit(task *Task, runningEntry *Entry) {
}
}
func resumeTask(index int) {
user := GetCurrentUser()
entries, err := database.ListEntries(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
lastEntry := entries[len(entries)-index]
runningEntryId, err := database.GetRunningEntryId(user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
if runningEntryId != "" {
fmt.Printf("%s a task is already running\n", CharTrack)
os.Exit(1)
}
project = lastEntry.Project
task = lastEntry.Task
newEntry, err := NewEntry("", begin, finish, project, task, user)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
if lastEntry.Notes != "" {
newEntry.Notes = lastEntry.Notes
}
isRunning := newEntry.Finish.IsZero()
_, err = database.AddEntry(user, newEntry, isRunning)
if err != nil {
fmt.Printf("%s %+v\n", CharError, err)
os.Exit(1)
}
fmt.Print(newEntry.GetOutputForTrack(isRunning, false))
}