Skip to content

Commit

Permalink
Extract mite api package
Browse files Browse the repository at this point in the history
  • Loading branch information
Ulrich Lissé committed Apr 2, 2019
1 parent 2f33454 commit 64bc3bd
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 120 deletions.
99 changes: 0 additions & 99 deletions api.go

This file was deleted.

56 changes: 35 additions & 21 deletions cmd-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package main
import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"os"
"strings"
"time"
)

func init() {
Expand All @@ -26,12 +29,16 @@ var listProjectsCommand = &cobra.Command{
Use: "projects",
Short: "list projects",
Run: func(cmd *cobra.Command, args []string) {
projects := apiGetProjects()
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
projects, err := api.Projects()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

t := tabby.New()
t.AddHeader("id", "name", "notes")
for _, project := range projects {
p := project.ProjectBody
t.AddLine(p.Id, p.Name, p.Note)
t.AddLine(project.Id, project.Name, project.Note)
}
t.Print()
},
Expand All @@ -41,12 +48,16 @@ var listServicesCommand = &cobra.Command{
Use: "services",
Short: "list services",
Run: func(cmd *cobra.Command, args []string) {
services := apiGetServices()
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
services, err := api.Services()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

t := tabby.New()
t.AddHeader("id", "name", "notes")
for _, project := range services {
s := project.ServiceBody
t.AddLine(s.Id, s.Name, s.Note)
for _, service := range services {
t.AddLine(service.Id, service.Name, service.Note)
}
t.Print()
},
Expand All @@ -56,25 +67,28 @@ var listTimeEntriesCommand = &cobra.Command{
Use: "entries",
Short: "list time entries",
Run: func(cmd *cobra.Command, args []string) {
entries := apiGetEntries()
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
to := time.Now()
from := to.AddDate(0, 0, -7)
direction := mite.DirectionAsc

entries, err := api.TimeEntries(&mite.TimeEntryParameters{
To: &to,
From: &from,
Direction: &direction,
})
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

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)
trimmedNotes := strings.Replace(entry.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)
shortenedProjectService := fmt.Sprintf("%.50s", entry.ProjectName+","+entry.ServiceName)
t.AddLine(entry.Id, shortendNotes, entry.Date, entry.Duration.String(), 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)
}
Loading

0 comments on commit 64bc3bd

Please sign in to comment.