Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 8, 2019
2 parents d458a3c + c3561f9 commit 203f7cf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
31 changes: 16 additions & 15 deletions cmd/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import (
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"strings"
"time"
)

var (
listTo string
listFrom string
listOrder string
createDate string
createDuration time.Duration
createMinutes string
createNote string
createProjectId string
createServiceId string
createActivity string
editTimeEntryId string
editDate string
editDuration string
editMinutes string
editNote string
editProjectId string
editServiceId string
Expand All @@ -34,26 +33,24 @@ var (
func init() {
today := datetime.Today()
defaultFrom := today.Add(0, 0, -7)
defaultDuration, err := time.ParseDuration("0m")
if err != nil {
panic(err)
}
defaultMinutes := datetime.NewMinutes(0).String()

// list
entriesListCommand.Flags().StringVarP(&listTo, "to", "t", today.String(), "list only entries until date (in YYYY-MM-DD format)")
entriesListCommand.Flags().StringVarP(&listFrom, "from", "f", defaultFrom.String(), "list only entries starting at date (in YYYY-MM-DD format)")
entriesListCommand.Flags().StringVarP(&listOrder, "order", "o", "asc", "list only entries starting at date (in YYYY-MM-DD format)")
entriesCommand.AddCommand(entriesListCommand)
// create
entriesCreateCommand.Flags().StringVarP(&createDate, "date", "D", today.String(), "day for which to create entry (in YYYY-MM-DD format)")
entriesCreateCommand.Flags().DurationVarP(&createDuration, "duration", "d", defaultDuration, "duration of entry (format examples: '1h15m' or '300m' or '6h')")
entriesCreateCommand.Flags().StringVarP(&createMinutes, "duration", "d", defaultMinutes, "duration of entry (format examples: '1h15m' or '300m' or '6h')")
entriesCreateCommand.Flags().StringVarP(&createNote, "note", "n", "", "a note describing what was worked on")
entriesCreateCommand.Flags().StringVarP(&createProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
entriesCreateCommand.Flags().StringVarP(&createServiceId, "serviceid", "s", "", "service id for time entry (HINT: use the 'service' sub-command to find the id)")
entriesCreateCommand.Flags().StringVarP(&createActivity, "activity", "a", "", "activity describing a specific project and service combination")
entriesCommand.AddCommand(entriesCreateCommand)
// edit
entriesEditCommand.Flags().StringVarP(&editDate, "date", "D", "", "day for which to edit entry (in YYYY-MM-DD format)")
entriesEditCommand.Flags().StringVarP(&editDuration, "duration", "d", "", "duration of entry (format examples: '1h15m' or '300m' or '6h')")
entriesEditCommand.Flags().StringVarP(&editMinutes, "duration", "d", "", "duration of entry (format examples: '1h15m' or '300m' or '6h')")
entriesEditCommand.Flags().StringVarP(&editNote, "note", "n", "", "a note describing what was worked on")
entriesEditCommand.Flags().StringVarP(&editTimeEntryId, "id", "i", "", "the time entry id to edit")
entriesEditCommand.Flags().StringVarP(&editProjectId, "projectid", "p", "", "project id for time entry (HINT: use the 'project' sub-command to find the id)")
Expand Down Expand Up @@ -109,7 +106,7 @@ func printEntries(entries []*mite.TimeEntry) {
shortenedNotes := fmt.Sprintf("%.50s", trimmedNotes)
shortenedProject := fmt.Sprintf("%.25s", entry.ProjectName)
shortenedService := fmt.Sprintf("%.25s", entry.ServiceName)
t.AddLine(entry.Id, shortenedNotes, entry.Date, entry.Duration.String(), shortenedProject, shortenedService)
t.AddLine(entry.Id, shortenedNotes, entry.Date, entry.Minutes.String(), shortenedProject, shortenedService)
}
t.Print()
}
Expand All @@ -128,10 +125,14 @@ var entriesCreateCommand = &cobra.Command{
if err != nil {
return err
}
cMinutes, err := datetime.ParseMinutes(createMinutes)
if err != nil {
return err
}

timeEntry := mite.TimeEntryCommand{
Date: &cDate,
Duration: &createDuration,
Minutes: &cMinutes,
Note: createNote,
ProjectId: projectId,
ServiceId: servicesId,
Expand Down Expand Up @@ -181,7 +182,7 @@ var entriesEditCommand = &cobra.Command{
// use retrieved values as defaults
timeEntry := mite.TimeEntryCommand{
Date: &entry.Date,
Duration: &entry.Duration,
Minutes: &entry.Minutes,
Note: entry.Note,
ProjectId: entry.ProjectId,
ServiceId: entry.ServiceId,
Expand All @@ -196,12 +197,12 @@ var entriesEditCommand = &cobra.Command{
timeEntry.Date = &eDate
}

if editDuration != "" {
eDuration, err := time.ParseDuration(editDuration)
if editMinutes != "" {
eMinutes, err := datetime.ParseMinutes(editMinutes)
if err != nil {
return err
}
timeEntry.Duration = &eDuration
timeEntry.Minutes = &eMinutes
}

if editNote != "" {
Expand Down
8 changes: 4 additions & 4 deletions cmd/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var trackerStatusCommand = &cobra.Command{

t := tabby.New()
t.AddHeader("id", "time", "state", "since")
t.AddLine(tracking.Id, tracking.Duration, "tracking", tracking.Since)
t.AddLine(tracking.Id, tracking.Minutes, "tracking", tracking.Since)
t.Print()

return nil
Expand All @@ -66,9 +66,9 @@ var trackerStartCommand = &cobra.Command{

t := tabby.New()
t.AddHeader("id", "time", "state", "since")
t.AddLine(tracking.Id, tracking.Duration, "tracking", tracking.Since)
t.AddLine(tracking.Id, tracking.Minutes, "tracking", tracking.Since)
if stopped != nil {
t.AddLine(stopped.Id, stopped.Duration, "stopped")
t.AddLine(stopped.Id, stopped.Minutes, "stopped")
}
t.Print()

Expand All @@ -94,7 +94,7 @@ var trackerStopCommand = &cobra.Command{

t := tabby.New()
t.AddHeader("id", "time", "state")
t.AddLine(stopped.Id, stopped.Duration, "stopped")
t.AddLine(stopped.Id, stopped.Minutes, "stopped")
t.Print()

return nil
Expand Down
5 changes: 3 additions & 2 deletions datetime/minutes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datetime

import (
"math"
"strings"
"time"
)

Expand All @@ -19,13 +20,13 @@ func ParseMinutes(s string) (Minutes, error) {
return Minutes{}, err
}

return Minutes{duration: d.Truncate(time.Minute)}, nil
return Minutes{duration: d.Round(time.Minute)}, nil
}

func (m Minutes) Value() int {
return int(math.Min(m.duration.Minutes(), math.MaxInt32))
}

func (m Minutes) String() string {
return m.duration.String()
return strings.TrimSuffix(m.duration.String(), "0s")
}
4 changes: 2 additions & 2 deletions datetime/minutes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Test_ParseMinutes(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, expected, actual)

actual, err = datetime.ParseMinutes("23m11s")
actual, err = datetime.ParseMinutes("22m33s")

assert.Nil(t, err)
assert.Equal(t, expected, actual)
Expand All @@ -30,7 +30,7 @@ func TestMinutes_Value(t *testing.T) {
}

func TestMinutes_String(t *testing.T) {
expected := "23m0s"
expected := "23m"
actual := datetime.NewMinutes(23).String()

assert.Equal(t, expected, actual)
Expand Down
11 changes: 5 additions & 6 deletions mite/time_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package mite
import (
"fmt"
"github.com/leanovate/mite-go/datetime"
"math"
"net/url"
"strconv"
"time"
)

type TimeEntry struct {
Id string
Duration time.Duration
Minutes datetime.Minutes
Date datetime.LocalDate
Note string
Billable bool
Expand All @@ -32,7 +31,7 @@ type TimeEntry struct {

type TimeEntryCommand struct {
Date *datetime.LocalDate
Duration *time.Duration
Minutes *datetime.Minutes
Note string
UserId string
ProjectId string
Expand All @@ -45,8 +44,8 @@ func (c *TimeEntryCommand) toRequest() *timeEntryRequest {
if c.Date != nil {
r.TimeEntry.Date = c.Date.String()
}
if c.Duration != nil {
r.TimeEntry.Minutes = int(math.Floor(math.Round(c.Duration.Minutes()))) // BOGUS
if c.Minutes != nil {
r.TimeEntry.Minutes = c.Minutes.Value()
}
r.TimeEntry.Note = c.Note
r.TimeEntry.UserId = c.UserId
Expand Down Expand Up @@ -123,7 +122,7 @@ func (r *timeEntryResponse) toTimeEntry() *TimeEntry {

return &TimeEntry{
Id: strconv.Itoa(r.TimeEntry.Id),
Duration: time.Duration(r.TimeEntry.Minutes) * time.Minute,
Minutes: datetime.NewMinutes(r.TimeEntry.Minutes),
Date: d,
Note: r.TimeEntry.Note,
Billable: r.TimeEntry.Billable,
Expand Down
21 changes: 11 additions & 10 deletions mite/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package mite

import (
"fmt"
"github.com/leanovate/mite-go/datetime"
"strconv"
"time"
)

type TrackingTimeEntry struct {
Id string
Duration time.Duration
Since time.Time
Id string
Minutes datetime.Minutes
Since time.Time
}

type StoppedTimeEntry struct {
Id string
Duration time.Duration
Id string
Minutes datetime.Minutes
}

type trackerResponse struct {
Expand All @@ -37,9 +38,9 @@ func (r *trackerResponse) toTrackingTimeEntry() *TrackingTimeEntry {
}

return &TrackingTimeEntry{
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Duration: time.Duration(r.Tracker.TrackingTimeEntry.Minutes) * time.Minute,
Since: r.Tracker.TrackingTimeEntry.Since,
Id: strconv.Itoa(r.Tracker.TrackingTimeEntry.Id),
Minutes: datetime.NewMinutes(r.Tracker.TrackingTimeEntry.Minutes),
Since: r.Tracker.TrackingTimeEntry.Since,
}
}

Expand All @@ -49,8 +50,8 @@ func (r *trackerResponse) toStoppedTimeEntry() *StoppedTimeEntry {
}

return &StoppedTimeEntry{
Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id),
Duration: time.Duration(r.Tracker.StoppedTimeEntry.Minutes) * time.Minute,
Id: strconv.Itoa(r.Tracker.StoppedTimeEntry.Id),
Minutes: datetime.NewMinutes(r.Tracker.StoppedTimeEntry.Minutes),
}
}

Expand Down

0 comments on commit 203f7cf

Please sign in to comment.