-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tracker: implemented tracker command (incl. sub-commands start,stop&s…
…tatus)
- Loading branch information
Showing
1 changed file
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/leanovate/mite-go/date" | ||
"github.com/leanovate/mite-go/mite" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
trackerTimeEntryId string | ||
) | ||
|
||
func init() { | ||
trackerCommand.AddCommand(trackerStatusCommand) | ||
trackerStartCommand.Flags().StringVarP(&trackerTimeEntryId, "id", "i", "", "the time entry id to (re)start a tracker for (default: latest time entry for today)") | ||
trackerCommand.AddCommand(trackerStartCommand) | ||
trackerStopCommand.Flags().StringVarP(&trackerTimeEntryId, "id", "i", "", "the time entry id to stop a tracker for (default: latest time entry for today)") | ||
trackerCommand.AddCommand(trackerStopCommand) | ||
rootCmd.AddCommand(trackerCommand) | ||
} | ||
|
||
var trackerCommand = &cobra.Command{ | ||
Use: "tracker", | ||
Short: "starts, stops and shows the status of the tracker", | ||
RunE: trackerStatusCommand.RunE, | ||
} | ||
|
||
var trackerStatusCommand = &cobra.Command{ | ||
Use: "status", | ||
Short: "shows the status of the time tracker", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
panic("implement trackerStatusCommand") | ||
return nil | ||
}, | ||
} | ||
|
||
var trackerStartCommand = &cobra.Command{ | ||
Use: "start", | ||
Short: "starts the time tracker for a time entry", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if trackerTimeEntryId == "" { | ||
trackerTimeEntryId, err = fetchLatestTimeEntryForToday() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
fmt.Printf("passed id: %s\n", trackerTimeEntryId) | ||
panic("implement trackerStartCommand") | ||
return nil | ||
}, | ||
} | ||
|
||
func fetchLatestTimeEntryForToday() (string, error) { | ||
today := date.Today() | ||
|
||
entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{ | ||
To: &today, | ||
From: &today, | ||
Direction: "desc", | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(entries) == 0 { | ||
return "", errors.New("no time entries for today found") | ||
} | ||
|
||
return entries[0].Id, nil | ||
} | ||
|
||
var trackerStopCommand = &cobra.Command{ | ||
Use: "stop", | ||
Short: "stops the time tracker for a time entry", | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if trackerTimeEntryId == "" { | ||
trackerTimeEntryId, err = fetchLatestTimeEntryForToday() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
fmt.Printf("passed id: %s\n", trackerTimeEntryId) | ||
panic("implement trackerStopCommand") | ||
return nil | ||
}, | ||
} |