|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "github.com/leanovate/mite-go/date" |
| 7 | + "github.com/leanovate/mite-go/mite" |
| 8 | + "github.com/spf13/cobra" |
| 9 | +) |
| 10 | + |
| 11 | +var ( |
| 12 | + trackerTimeEntryId string |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + trackerCommand.AddCommand(trackerStatusCommand) |
| 17 | + trackerStartCommand.Flags().StringVarP(&trackerTimeEntryId, "id", "i", "", "the time entry id to (re)start a tracker for (default: latest time entry for today)") |
| 18 | + trackerCommand.AddCommand(trackerStartCommand) |
| 19 | + trackerStopCommand.Flags().StringVarP(&trackerTimeEntryId, "id", "i", "", "the time entry id to stop a tracker for (default: latest time entry for today)") |
| 20 | + trackerCommand.AddCommand(trackerStopCommand) |
| 21 | + rootCmd.AddCommand(trackerCommand) |
| 22 | +} |
| 23 | + |
| 24 | +var trackerCommand = &cobra.Command{ |
| 25 | + Use: "tracker", |
| 26 | + Short: "starts, stops and shows the status of the tracker", |
| 27 | + RunE: trackerStatusCommand.RunE, |
| 28 | +} |
| 29 | + |
| 30 | +var trackerStatusCommand = &cobra.Command{ |
| 31 | + Use: "status", |
| 32 | + Short: "shows the status of the time tracker", |
| 33 | + RunE: func(cmd *cobra.Command, args []string) (err error) { |
| 34 | + panic("implement trackerStatusCommand") |
| 35 | + return nil |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +var trackerStartCommand = &cobra.Command{ |
| 40 | + Use: "start", |
| 41 | + Short: "starts the time tracker for a time entry", |
| 42 | + RunE: func(cmd *cobra.Command, args []string) (err error) { |
| 43 | + if trackerTimeEntryId == "" { |
| 44 | + trackerTimeEntryId, err = fetchLatestTimeEntryForToday() |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + } |
| 49 | + fmt.Printf("passed id: %s\n", trackerTimeEntryId) |
| 50 | + panic("implement trackerStartCommand") |
| 51 | + return nil |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +func fetchLatestTimeEntryForToday() (string, error) { |
| 56 | + today := date.Today() |
| 57 | + |
| 58 | + entries, err := deps.miteApi.TimeEntries(&mite.TimeEntryQuery{ |
| 59 | + To: &today, |
| 60 | + From: &today, |
| 61 | + Direction: "desc", |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + return "", err |
| 65 | + } |
| 66 | + |
| 67 | + if len(entries) == 0 { |
| 68 | + return "", errors.New("no time entries for today found") |
| 69 | + } |
| 70 | + |
| 71 | + return entries[0].Id, nil |
| 72 | +} |
| 73 | + |
| 74 | +var trackerStopCommand = &cobra.Command{ |
| 75 | + Use: "stop", |
| 76 | + Short: "stops the time tracker for a time entry", |
| 77 | + RunE: func(cmd *cobra.Command, args []string) (err error) { |
| 78 | + if trackerTimeEntryId == "" { |
| 79 | + trackerTimeEntryId, err = fetchLatestTimeEntryForToday() |
| 80 | + if err != nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + } |
| 84 | + fmt.Printf("passed id: %s\n", trackerTimeEntryId) |
| 85 | + panic("implement trackerStopCommand") |
| 86 | + return nil |
| 87 | + }, |
| 88 | +} |
0 commit comments