Skip to content

Commit

Permalink
list: implemented basic entry list
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 1, 2019
1 parent fb9eeff commit 6fb1b5b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
34 changes: 34 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"os"
"time"
)

type Project struct {
Expand All @@ -21,6 +22,18 @@ type Service struct {
ServiceBody GenericBody `json:"service"`
}

type TimeEntry struct {
TimeEntryBody TimeEntryBody `json:"time_entry"`
}

type TimeEntryBody struct {
GenericBody
Minutes int `json:"minutes"`
Date string `json:"date_at"`
ProjectName string `json:"project_name"`
ServiceName string `json:"service_name"`
}

func apiGetProjects() (projects []Project) {
client := &http.Client{}
req := buildGetRequest("/projects.json")
Expand All @@ -44,6 +57,7 @@ func buildGetRequest(path string) *http.Request {
_, _ = fmt.Fprintln(os.Stderr, err)
}
req.Header.Add("X-MiteApiKey", configGetApiKey())
req.Header.Add("User-Agent", "mite-go/0.1 (+github.com/phiros/mite-go)")

return req
}
Expand All @@ -63,3 +77,23 @@ func apiGetServices() (services []Service) {
}
return services
}

func apiGetEntries() (timeEntries []TimeEntry) {
client := &http.Client{}

toDate := time.Now().Format("2006-01-02")
fromDate := time.Now().AddDate(0, 0, -7).Format("2006-01-02")
requestPathAndParams := fmt.Sprintf("/time_entries.json?from=%s&to=%s", fromDate, toDate)
req := buildGetRequest(requestPathAndParams)
res, err := client.Do(req)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&timeEntries)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}
return timeEntries
}
30 changes: 30 additions & 0 deletions cmd-list.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/spf13/cobra"
"strings"
)

func init() {
listCommand.AddCommand(listProjectsCommand)
listCommand.AddCommand(listServicesCommand)
listCommand.AddCommand(listTimeEntriesCommand)
rootCmd.AddCommand(listCommand)
}

Expand Down Expand Up @@ -48,3 +51,30 @@ var listServicesCommand = &cobra.Command{
t.Print()
},
}

var listTimeEntriesCommand = &cobra.Command{
Use: "entries",
Short: "list time entries",
Run: func(cmd *cobra.Command, args []string) {
entries := apiGetEntries()
t := tabby.New()
t.AddHeader("id", "notes", "date", "time", "project,service")
for _, entry := range entries {
s := entry.TimeEntryBody
trimmedNotes := strings.Replace(s.Note, "\r\n", ",", -1)
shortendNotes := fmt.Sprintf("%.50s", trimmedNotes)
shortenedProjectService := fmt.Sprintf("%.50s", s.ProjectName+","+s.ServiceName)
t.AddLine(s.Id, shortendNotes, s.Date, formatMinutesToHuman(s.Minutes), shortenedProjectService)
}
t.Print()
},
}

func formatMinutesToHuman(minutes int) string {
if minutes > 60 {
hours := minutes / 60
return fmt.Sprintf("%.2dh:%.2dm", hours, minutes-hours*60)
}

return fmt.Sprintf("0h:%.2dm", minutes)
}

0 comments on commit 6fb1b5b

Please sign in to comment.