Compare commits
5 Commits
0.0.4
...
c6d8fee715
| Author | SHA1 | Date | |
|---|---|---|---|
| c6d8fee715 | |||
| 34a94fa269 | |||
| 163e7abf47 | |||
| d51f409497 | |||
| e7fa789b2b |
23
README.md
23
README.md
@@ -0,0 +1,23 @@
|
|||||||
|
# charmite
|
||||||
|
|
||||||
|
Charmite is a simple TUI to add/view time entries in the [Mite](https://mite.de/en/)
|
||||||
|
time tracking tool.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Download the appropriate binary from https://code.ppl.town/justin/charmite/releases
|
||||||
|
|
||||||
|
Or compile from the source code with:
|
||||||
|
|
||||||
|
go build .
|
||||||
|
|
||||||
|
## Using
|
||||||
|
|
||||||
|
You'll need to set the environment variables:
|
||||||
|
|
||||||
|
* MITE_API
|
||||||
|
* MITE_DOMAIN
|
||||||
|
|
||||||
|
To appropriate values.
|
||||||
|
|
||||||
|
See the [Mite FAQ](https://mite.de/en/faq.html) under "How do I allow API access for my account? Where can I get an API key?" for details on how to obtain your API key.
|
||||||
10
build.sh
10
build.sh
@@ -1,10 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
GOOS=linux GOARCH=amd64 go build -o cal_linux_amd64
|
|
||||||
GOOS=darwin GOARCH=arm64 go build -o cal_darwin_arm64
|
|
||||||
GOOS=windows GOARCH=amd64 go build -o cal_windows_lol_amd64
|
|
||||||
|
|
||||||
zip cal.zip cal_linux_amd64 cal_darwin_arm64 cal_windows_lol_amd64
|
|
||||||
|
|
||||||
|
|
||||||
open .
|
|
||||||
65
mite/mite.go
65
mite/mite.go
@@ -3,6 +3,7 @@ package mite
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
@@ -56,7 +57,7 @@ func (p Project) GetName() string { return p.Name + " (" + p.CustomerName + ")"
|
|||||||
func (a APIClient) GetProjects() (Projects, error) {
|
func (a APIClient) GetProjects() (Projects, error) {
|
||||||
// GET /projects.json
|
// GET /projects.json
|
||||||
p := APIProjects{}
|
p := APIProjects{}
|
||||||
err := get(a.domain, a.apiKey, "/projects.json", &p)
|
err := a.get("/projects.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -98,7 +99,7 @@ func (c Customer) GetName() string { return c.Name }
|
|||||||
func (a APIClient) GetCustomers() (Customers, error) {
|
func (a APIClient) GetCustomers() (Customers, error) {
|
||||||
// GET /customers.json
|
// GET /customers.json
|
||||||
p := apiCustomers{}
|
p := apiCustomers{}
|
||||||
err := get(a.domain, a.apiKey, "/customers.json", &p)
|
err := a.get("/customers.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -142,7 +143,7 @@ func (s Service) GetName() string {
|
|||||||
func (a APIClient) GetServices() (Services, error) {
|
func (a APIClient) GetServices() (Services, error) {
|
||||||
// GET /services.json
|
// GET /services.json
|
||||||
p := apiServices{}
|
p := apiServices{}
|
||||||
err := get(a.domain, a.apiKey, "/services.json", &p)
|
err := a.get("/services.json", &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -215,7 +216,7 @@ func (a APIClient) GetTimeEntries(from, to time.Time) (TimeEntries, error) {
|
|||||||
p := apiTimeEntry{}
|
p := apiTimeEntry{}
|
||||||
u := fmt.Sprintf("/time_entries.json?from=%s&to=%s", from.Format(time.DateOnly), to.Format(time.DateOnly))
|
u := fmt.Sprintf("/time_entries.json?from=%s&to=%s", from.Format(time.DateOnly), to.Format(time.DateOnly))
|
||||||
|
|
||||||
err := get(a.domain, a.apiKey, u, &p)
|
err := a.get(u, &p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -253,23 +254,62 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI
|
|||||||
req.RequestTimeEntryHolder.ProjectID = projectId
|
req.RequestTimeEntryHolder.ProjectID = projectId
|
||||||
req.RequestTimeEntryHolder.ServiceID = serviceId
|
req.RequestTimeEntryHolder.ServiceID = serviceId
|
||||||
|
|
||||||
err := post(a.domain, a.apiKey, "/time_entries.json", req)
|
err := a.post("/time_entries.json", req)
|
||||||
return err
|
return err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func post(domain, apiKey, path string, data any) error {
|
type apiTimeTrackerEntry struct {
|
||||||
|
TimeTrackerHolder timeTrackerHolder `json:"tracker"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type timeTrackerHolder struct {
|
||||||
|
TrackingTimeEntry *TrackingTimeEntry `json:"tracking_time_entry"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TrackingTimeEntry struct {
|
||||||
|
ID int `json:"id"`
|
||||||
|
Minutes int `json:"minutes"`
|
||||||
|
Since time.Time `json:"since"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// {
|
||||||
|
// "tracker": {
|
||||||
|
// "tracking_time_entry": {
|
||||||
|
// "id": 36135321,
|
||||||
|
// "minutes": 247,
|
||||||
|
// "since": "2015-10-15T17:05:04+02:00"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
var ErrNoTracker = errors.New("no time tracker running")
|
||||||
|
|
||||||
|
// GetTimeTracker gets the current running time tracker. If no tracker is
|
||||||
|
// running, the error returned will be ErrNoTracker
|
||||||
|
func (a APIClient) GetTimeTracker() (TrackingTimeEntry, error) {
|
||||||
|
r := apiTimeTrackerEntry{}
|
||||||
|
err := a.get("/tracker.json", &r)
|
||||||
|
if err != nil {
|
||||||
|
return TrackingTimeEntry{}, err
|
||||||
|
}
|
||||||
|
if r.TimeTrackerHolder.TrackingTimeEntry == nil {
|
||||||
|
return TrackingTimeEntry{}, ErrNoTracker
|
||||||
|
}
|
||||||
|
return *r.TimeTrackerHolder.TrackingTimeEntry, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a APIClient) post(path string, data any) error {
|
||||||
|
|
||||||
b, err := json.Marshal(data)
|
b, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", baseurl(domain, path), bytes.NewBuffer(b))
|
req, err := http.NewRequest("POST", baseurl(a.domain, path), bytes.NewBuffer(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add("X-MiteApiKey", apiKey)
|
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||||
req.Header.Add("Content-Type", "application/json")
|
req.Header.Add("Content-Type", "application/json")
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
@@ -285,12 +325,12 @@ func post(domain, apiKey, path string, data any) error {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func get(domain, apiKey, path string, data any) error {
|
func (a APIClient) get(path string, data any) error {
|
||||||
req, err := http.NewRequest("GET", baseurl(domain, path), nil)
|
req, err := http.NewRequest("GET", baseurl(a.domain, path), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
req.Header.Add("X-MiteApiKey", apiKey)
|
req.Header.Add("X-MiteApiKey", a.apiKey)
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -305,7 +345,6 @@ func get(domain, apiKey, path string, data any) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func baseurl(domain, path string) string {
|
func baseurl(domain, path string) string {
|
||||||
|
|||||||
46
mite/mite_test.go
Normal file
46
mite/mite_test.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package mite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUnmarshalTimeTracking(t *testing.T) {
|
||||||
|
trackerOn := []byte(`
|
||||||
|
{
|
||||||
|
"tracker": {
|
||||||
|
"tracking_time_entry": {
|
||||||
|
"id": 36135321,
|
||||||
|
"minutes": 247,
|
||||||
|
"since": "2015-10-15T17:05:04+02:00"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
trackerOff := []byte(`{
|
||||||
|
"tracker": {}
|
||||||
|
}`)
|
||||||
|
off := apiTimeTrackerEntry{}
|
||||||
|
err := json.Unmarshal(trackerOff, &off)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
if off.TimeTrackerHolder.TrackingTimeEntry != nil {
|
||||||
|
t.Error("expected nil, but is not")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
on := apiTimeTrackerEntry{}
|
||||||
|
err = json.Unmarshal(trackerOn, &on)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
} else {
|
||||||
|
if on.TimeTrackerHolder.TrackingTimeEntry == nil {
|
||||||
|
t.Error("expected note nil, but is not")
|
||||||
|
} else {
|
||||||
|
if on.TimeTrackerHolder.TrackingTimeEntry.ID != 36135321 {
|
||||||
|
t.Error("bad unmarshal?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
31
model.go
31
model.go
@@ -76,10 +76,10 @@ func initialModel(miteDomain, miteApiKey string) model {
|
|||||||
tab.SetStyles(s)
|
tab.SetStyles(s)
|
||||||
|
|
||||||
tab.SetColumns([]table.Column{
|
tab.SetColumns([]table.Column{
|
||||||
table.Column{Title: "min", Width: 5},
|
{Title: " min", Width: 4},
|
||||||
table.Column{Title: "lck", Width: 4},
|
{Title: "🔐", Width: 2},
|
||||||
table.Column{Title: "customer", Width: 10},
|
{Title: "customer", Width: 10},
|
||||||
table.Column{Title: "description", Width: 40},
|
{Title: "description", Width: 40},
|
||||||
})
|
})
|
||||||
|
|
||||||
m.start = calendarTime{time.Now()}
|
m.start = calendarTime{time.Now()}
|
||||||
@@ -304,7 +304,7 @@ func (m model) tableDataForDate(t time.Time) []table.Row {
|
|||||||
if entry.Locked {
|
if entry.Locked {
|
||||||
lock = "🔐"
|
lock = "🔐"
|
||||||
}
|
}
|
||||||
rows = append(rows, table.Row{fmt.Sprint(entry.Minutes), lock, entry.CustomerName, entry.Note})
|
rows = append(rows, table.Row{fmt.Sprintf("%4d", entry.Minutes), lock, entry.CustomerName, entry.Note})
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
@@ -346,6 +346,12 @@ func (m model) View() string {
|
|||||||
lhs := strings.Builder{}
|
lhs := strings.Builder{}
|
||||||
rhs := strings.Builder{}
|
rhs := strings.Builder{}
|
||||||
|
|
||||||
|
nb := lipgloss.NewStyle().Border(lipgloss.NormalBorder())
|
||||||
|
db := lipgloss.NewStyle().Border(lipgloss.DoubleBorder())
|
||||||
|
|
||||||
|
lhsS := nb
|
||||||
|
rhsS := nb
|
||||||
|
|
||||||
// if any entries are not "locked", we always use that
|
// if any entries are not "locked", we always use that
|
||||||
// regular colour in preference
|
// regular colour in preference
|
||||||
styles := map[string]lipgloss.Style{}
|
styles := map[string]lipgloss.Style{}
|
||||||
@@ -367,6 +373,7 @@ func (m model) View() string {
|
|||||||
|
|
||||||
if m.tuiMode == MODE_CAL {
|
if m.tuiMode == MODE_CAL {
|
||||||
lhs.WriteString("(f)etch time data\n")
|
lhs.WriteString("(f)etch time data\n")
|
||||||
|
lhsS = db
|
||||||
} else {
|
} else {
|
||||||
lhs.WriteString("\n")
|
lhs.WriteString("\n")
|
||||||
}
|
}
|
||||||
@@ -378,18 +385,24 @@ func (m model) View() string {
|
|||||||
if m.tuiMode == MODE_FORMENTRY {
|
if m.tuiMode == MODE_FORMENTRY {
|
||||||
lhs.WriteString("(esc) abort form\n")
|
lhs.WriteString("(esc) abort form\n")
|
||||||
lhs.WriteString("\n")
|
lhs.WriteString("\n")
|
||||||
|
rhsS = db
|
||||||
} else {
|
} else {
|
||||||
lhs.WriteString("(tab) switch panes\n")
|
lhs.WriteString("(tab) switch panes\n")
|
||||||
lhs.WriteString("(q)uit\n")
|
lhs.WriteString("(q)uit\n")
|
||||||
}
|
}
|
||||||
|
if m.tuiMode == MODE_TIMEENTRIES {
|
||||||
|
rhsS = db
|
||||||
|
}
|
||||||
|
|
||||||
calendarWidth := 25
|
lhsWidth := 25
|
||||||
tableWidth := m.windowWidth - calendarWidth
|
rhsWidth := m.windowWidth - lhsWidth - 4
|
||||||
|
|
||||||
if m.tuiMode == MODE_FORMENTRY {
|
if m.tuiMode == MODE_FORMENTRY {
|
||||||
rhs.WriteString(m.formData.form.View())
|
rhs.WriteString(m.formData.form.View())
|
||||||
} else {
|
} else {
|
||||||
if m.fetchedData {
|
if m.fetchedData {
|
||||||
|
m.timeData.table.Columns()[3].Width = rhsWidth - 30
|
||||||
|
m.timeData.table.SetHeight(14)
|
||||||
rhs.WriteString(m.timeData.table.View())
|
rhs.WriteString(m.timeData.table.View())
|
||||||
rhs.WriteString("\n")
|
rhs.WriteString("\n")
|
||||||
}
|
}
|
||||||
@@ -397,8 +410,8 @@ func (m model) View() string {
|
|||||||
|
|
||||||
out := lipgloss.JoinHorizontal(
|
out := lipgloss.JoinHorizontal(
|
||||||
lipgloss.Top,
|
lipgloss.Top,
|
||||||
lipgloss.NewStyle().Width(calendarWidth).Render(lhs.String()),
|
lhsS.Render(lipgloss.NewStyle().Width(lhsWidth).Render(lhs.String())),
|
||||||
lipgloss.NewStyle().Width(tableWidth).Render(rhs.String()),
|
rhsS.Render(lipgloss.NewStyle().Width(rhsWidth).Render(rhs.String())),
|
||||||
)
|
)
|
||||||
|
|
||||||
sofar := lipgloss.Height(out)
|
sofar := lipgloss.Height(out)
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|||||||
case "esc":
|
case "esc":
|
||||||
m.tuiMode = MODE_CAL
|
m.tuiMode = MODE_CAL
|
||||||
return m, nil
|
return m, nil
|
||||||
|
case "ctrl+c":
|
||||||
|
return m, tea.Quit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user