Tear down everything
Fix config
This commit is contained in:
411
internal/timewarrior/timewarrior.go
Normal file
411
internal/timewarrior/timewarrior.go
Normal file
@@ -0,0 +1,411 @@
|
||||
// TODO: error handling
|
||||
// TODO: split combinedOutput and handle stderr differently
|
||||
package timewarrior
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
twBinary = "timew"
|
||||
)
|
||||
|
||||
type TimeWarrior interface {
|
||||
GetConfig() *TWConfig
|
||||
|
||||
GetTags() []string
|
||||
GetTagCombinations() []string
|
||||
|
||||
GetIntervals(filter ...string) Intervals
|
||||
StartTracking(tags []string) error
|
||||
StopTracking() error
|
||||
ContinueTracking() error
|
||||
ContinueInterval(id int) error
|
||||
CancelTracking() error
|
||||
DeleteInterval(id int) error
|
||||
FillInterval(id int) error
|
||||
JoinInterval(id int) error
|
||||
ModifyInterval(interval *Interval, adjust bool) error
|
||||
GetSummary(filter ...string) string
|
||||
GetActive() *Interval
|
||||
|
||||
Undo()
|
||||
}
|
||||
|
||||
type TimewarriorInterop struct {
|
||||
configLocation string
|
||||
defaultArgs []string
|
||||
config *TWConfig
|
||||
ctx context.Context
|
||||
|
||||
mutex sync.Mutex
|
||||
}
|
||||
|
||||
func NewTimewarriorInterop(ctx context.Context, configLocation string) *TimewarriorInterop {
|
||||
if _, err := exec.LookPath(twBinary); err != nil {
|
||||
slog.Error("Timewarrior not found")
|
||||
return nil
|
||||
}
|
||||
|
||||
ts := &TimewarriorInterop{
|
||||
configLocation: configLocation,
|
||||
defaultArgs: []string{},
|
||||
ctx: ctx,
|
||||
mutex: sync.Mutex{},
|
||||
}
|
||||
ts.config = ts.extractConfig()
|
||||
|
||||
return ts
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) GetConfig() *TWConfig {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
return ts.config
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) GetTags() []string {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"tags"}...)...)
|
||||
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
slog.Error("Failed getting tags", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
tags := make([]string, 0)
|
||||
lines := strings.Split(string(output), "\n")
|
||||
|
||||
// Skip header lines and parse tag names
|
||||
for i, line := range lines {
|
||||
if i < 3 || line == "" {
|
||||
continue
|
||||
}
|
||||
// Tags are space-separated, first column is the tag name
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) > 0 {
|
||||
tags = append(tags, fields[0])
|
||||
}
|
||||
}
|
||||
|
||||
slices.Sort(tags)
|
||||
return tags
|
||||
}
|
||||
|
||||
// GetTagCombinations returns unique tag combinations from intervals,
|
||||
// ordered newest first (most recent intervals' tags appear first).
|
||||
// Returns formatted strings like "dev client-work meeting".
|
||||
func (ts *TimewarriorInterop) GetTagCombinations() []string {
|
||||
intervals := ts.GetIntervals() // Already sorted newest first
|
||||
|
||||
// Track unique combinations while preserving order
|
||||
seen := make(map[string]bool)
|
||||
var combinations []string
|
||||
|
||||
for _, interval := range intervals {
|
||||
if len(interval.Tags) == 0 {
|
||||
continue // Skip intervals with no tags
|
||||
}
|
||||
|
||||
// Format tags (handles spaces with quotes)
|
||||
combo := formatTagsForCombination(interval.Tags)
|
||||
|
||||
if !seen[combo] {
|
||||
seen[combo] = true
|
||||
combinations = append(combinations, combo)
|
||||
}
|
||||
}
|
||||
|
||||
return combinations
|
||||
}
|
||||
|
||||
// formatTagsForCombination formats tags consistently for display
|
||||
func formatTagsForCombination(tags []string) string {
|
||||
var formatted []string
|
||||
for _, t := range tags {
|
||||
if strings.Contains(t, " ") {
|
||||
formatted = append(formatted, "\""+t+"\"")
|
||||
} else {
|
||||
formatted = append(formatted, t)
|
||||
}
|
||||
}
|
||||
return strings.Join(formatted, " ")
|
||||
}
|
||||
|
||||
// getIntervalsUnlocked fetches intervals without acquiring mutex (for internal use only).
|
||||
// Caller must hold ts.mutex.
|
||||
func (ts *TimewarriorInterop) getIntervalsUnlocked(filter ...string) Intervals {
|
||||
args := append(ts.defaultArgs, "export")
|
||||
if filter != nil {
|
||||
args = append(args, filter...)
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
if ts.ctx.Err() == context.Canceled {
|
||||
return nil
|
||||
}
|
||||
slog.Error("Failed getting intervals", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
intervals := make(Intervals, 0)
|
||||
err = json.Unmarshal(output, &intervals)
|
||||
if err != nil {
|
||||
slog.Error("Failed unmarshalling intervals", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Reverse the intervals to show newest first
|
||||
slices.Reverse(intervals)
|
||||
|
||||
// Assign IDs based on new order (newest is @1)
|
||||
for i := range intervals {
|
||||
intervals[i].ID = i + 1
|
||||
}
|
||||
|
||||
return intervals
|
||||
}
|
||||
|
||||
// GetIntervals fetches intervals with the given filter, acquiring mutex lock.
|
||||
func (ts *TimewarriorInterop) GetIntervals(filter ...string) Intervals {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
return ts.getIntervalsUnlocked(filter...)
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) StartTracking(tags []string) error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
if len(tags) == 0 {
|
||||
return fmt.Errorf("at least one tag is required")
|
||||
}
|
||||
|
||||
args := append(ts.defaultArgs, "start")
|
||||
args = append(args, tags...)
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, args...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed starting tracking", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) StopTracking() error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, "stop")...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed stopping tracking", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) ContinueTracking() error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, "continue")...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed continuing tracking", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) ContinueInterval(id int) error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"continue", fmt.Sprintf("@%d", id)}...)...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed continuing interval", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) CancelTracking() error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, "cancel")...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed canceling tracking", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) DeleteInterval(id int) error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"delete", fmt.Sprintf("@%d", id)}...)...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed deleting interval", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) FillInterval(id int) error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"fill", fmt.Sprintf("@%d", id)}...)...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed filling interval", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) JoinInterval(id int) error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
// Join the current interval with the previous one
|
||||
// The previous interval has id+1 (since intervals are ordered newest first)
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"join", fmt.Sprintf("@%d", id+1), fmt.Sprintf("@%d", id)}...)...)
|
||||
if err := cmd.Run(); err != nil {
|
||||
slog.Error("Failed joining interval", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) ModifyInterval(interval *Interval, adjust bool) error {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
// Export the modified interval
|
||||
intervals, err := json.Marshal(Intervals{interval})
|
||||
if err != nil {
|
||||
slog.Error("Failed marshalling interval", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Build import command with optional :adjust hint
|
||||
args := append(ts.defaultArgs, "import")
|
||||
if adjust {
|
||||
args = append(args, ":adjust")
|
||||
}
|
||||
|
||||
// Import the modified interval
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, args...)
|
||||
cmd.Stdin = bytes.NewBuffer(intervals)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
slog.Error("Failed modifying interval", "error", err, "output", string(out))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) GetSummary(filter ...string) string {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
args := append(ts.defaultArgs, "summary")
|
||||
if filter != nil {
|
||||
args = append(args, filter...)
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, args...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
slog.Error("Failed getting summary", "error", err)
|
||||
return ""
|
||||
}
|
||||
|
||||
return string(output)
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) GetActive() *Interval {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"get", "dom.active"}...)...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil || string(output) == "0\n" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get the active interval using unlocked version (we already hold the mutex)
|
||||
intervals := ts.getIntervalsUnlocked()
|
||||
for _, interval := range intervals {
|
||||
if interval.End == "" {
|
||||
return interval
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) Undo() {
|
||||
ts.mutex.Lock()
|
||||
defer ts.mutex.Unlock()
|
||||
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"undo"}...)...)
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
slog.Error("Failed undoing", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *TimewarriorInterop) extractConfig() *TWConfig {
|
||||
cmd := exec.CommandContext(ts.ctx, twBinary, append(ts.defaultArgs, []string{"show"}...)...)
|
||||
output, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
slog.Error("Failed getting config", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
return NewConfig(strings.Split(string(output), "\n"))
|
||||
}
|
||||
|
||||
func extractTags(config string) []string {
|
||||
re := regexp.MustCompile(`tag\.([^.]+)\.[^.]+`)
|
||||
matches := re.FindAllStringSubmatch(config, -1)
|
||||
uniques := make(map[string]struct{})
|
||||
for _, match := range matches {
|
||||
uniques[match[1]] = struct{}{}
|
||||
}
|
||||
|
||||
var tags []string
|
||||
for tag := range uniques {
|
||||
tags = append(tags, tag)
|
||||
}
|
||||
|
||||
slices.Sort(tags)
|
||||
return tags
|
||||
}
|
||||
Reference in New Issue
Block a user