2020-10-11 14:13:08 +00:00
package z
import (
2020-10-14 23:22:41 +00:00
"os"
"fmt"
2020-10-11 14:13:08 +00:00
"github.com/spf13/cobra"
)
var trackCmd = & cobra . Command {
Use : "track" ,
Short : "Tracking time" ,
2020-10-15 20:56:04 +00:00
Long : "Track new activity, which can either be kept running until 'finish' is being called or parameterized to be a finished activity." ,
2020-10-11 14:13:08 +00:00
Run : func ( cmd * cobra . Command , args [ ] string ) {
2020-10-11 19:36:52 +00:00
user := GetCurrentUser ( )
runningEntryId , err := database . GetRunningEntryId ( user )
if err != nil {
2020-10-15 21:07:36 +00:00
fmt . Printf ( "%s %+v\n" , CharError , err )
2020-10-15 20:38:25 +00:00
os . Exit ( 1 )
2020-10-11 19:36:52 +00:00
}
if runningEntryId != "" {
2020-10-15 21:07:36 +00:00
fmt . Printf ( "%s a task is already running\n" , CharTrack )
2020-10-15 20:38:25 +00:00
os . Exit ( 1 )
2020-10-11 19:36:52 +00:00
}
newEntry , err := NewEntry ( "" , begin , finish , project , task , user )
2020-10-11 14:13:08 +00:00
if err != nil {
2020-10-15 21:07:36 +00:00
fmt . Printf ( "%s %+v\n" , CharError , err )
2020-10-15 20:38:25 +00:00
os . Exit ( 1 )
2020-10-11 14:13:08 +00:00
}
2020-10-17 12:17:01 +00:00
if notes != "" {
newEntry . Notes = notes
}
2020-10-14 23:42:58 +00:00
isRunning := newEntry . Finish . IsZero ( )
_ , err = database . AddEntry ( user , newEntry , isRunning )
2020-10-11 20:15:05 +00:00
if err != nil {
2020-10-15 21:07:36 +00:00
fmt . Printf ( "%s %+v\n" , CharError , err )
2020-10-15 20:38:25 +00:00
os . Exit ( 1 )
2020-10-11 20:15:05 +00:00
}
2020-10-15 20:56:04 +00:00
fmt . Printf ( newEntry . GetOutputForTrack ( isRunning , false ) )
2020-10-11 20:15:05 +00:00
return
2020-10-11 14:13:08 +00:00
} ,
}
func init ( ) {
rootCmd . AddCommand ( trackCmd )
2020-10-15 20:56:04 +00:00
trackCmd . 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)." )
trackCmd . 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." )
2020-10-11 19:36:52 +00:00
trackCmd . Flags ( ) . StringVarP ( & project , "project" , "p" , "" , "Project to be assigned" )
trackCmd . Flags ( ) . StringVarP ( & task , "task" , "t" , "" , "Task to be assigned" )
2020-10-17 12:17:01 +00:00
trackCmd . Flags ( ) . StringVarP ( & notes , "notes" , "n" , "" , "Activity notes" )
2020-10-11 19:36:52 +00:00
trackCmd . Flags ( ) . BoolVarP ( & force , "force" , "f" , false , "Force begin tracking of a new task \neven though another one is still running \n(ONLY IF YOU KNOW WHAT YOU'RE DOING!)" )
2020-10-11 14:13:08 +00:00
var err error
database , err = InitDatabase ( )
if err != nil {
2020-10-15 21:07:36 +00:00
fmt . Printf ( "%s %+v\n" , CharError , err )
2020-10-15 20:38:25 +00:00
os . Exit ( 1 )
2020-10-11 14:13:08 +00:00
}
}