Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic supports for adding tags #183

Merged
merged 3 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,21 @@ func listRecordsCommand(t *core.Timetrace) *cobra.Command {
billable = "yes"
}

rows[i] = make([]string, 6)
rows[i] = make([]string, 7)
rows[i][0] = strconv.Itoa(len(records) - i)
rows[i][1] = t.Formatter().RecordKey(record)
rows[i][2] = record.Project.Key
rows[i][3] = t.Formatter().TimeString(record.Start)
rows[i][4] = end
rows[i][5] = billable
rows[i][6] = t.Formatter().FormatTags(record.Tags)
}

footer := make([]string, 6)
footer := make([]string, 7)
footer[len(footer)-2] = "Total: "
footer[len(footer)-1] = t.Formatter().FormatDuration(getTotalTrackedTime(records))

out.Table([]string{"#", "Key", "Project", "Start", "End", "Billable"}, rows, footer)
out.Table([]string{"#", "Key", "Project", "Start", "End", "Billable", "Tags"}, rows, footer)
},
}

Expand Down
31 changes: 28 additions & 3 deletions cli/start.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package cli

import (
"fmt"
"strings"

"github.com/dominikbraun/timetrace/core"
"github.com/dominikbraun/timetrace/out"

"github.com/spf13/cobra"
)

const TagsPrefix = "+"

type startOptions struct {
isBillable bool
isNonBillable bool // Used for overwriting `billable: true` in the project config.
Expand All @@ -16,14 +21,15 @@ func startCommand(t *core.Timetrace) *cobra.Command {
var options startOptions

start := &cobra.Command{
Use: "start <PROJECT KEY>",
Use: "start <PROJECT KEY> [+TAG1, +TAG2, ...]",
Short: "Start tracking time",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectKey := args[0]
tags := args[1:]

isBillable := options.isBillable

// If there is a default configuration for the project key, use that configuration.
if projectConfig, ok := t.Config().Projects[projectKey]; ok {
isBillable = projectConfig.Billable
}
Expand All @@ -32,7 +38,13 @@ func startCommand(t *core.Timetrace) *cobra.Command {
isBillable = false
}

if err := t.Start(projectKey, isBillable); err != nil {
tagNames, err := extractTagNames(tags)
if err != nil {
out.Err("Failed to start tracking: %s", err.Error())
return
}

if err := t.Start(projectKey, isBillable, tagNames); err != nil {
out.Err("Failed to start tracking: %s", err.Error())
return
}
Expand All @@ -49,3 +61,16 @@ func startCommand(t *core.Timetrace) *cobra.Command {

return start
}

func extractTagNames(tagsWithPrefix []string) ([]string, error) {
tagNames := make([]string, 0)

for _, tagWithPrefix := range tagsWithPrefix {
if !strings.HasPrefix(tagWithPrefix, TagsPrefix) {
return nil, fmt.Errorf("'%s' is not a valid tag. Tags must start with %s", tagWithPrefix, TagsPrefix)
}
tagNames = append(tagNames, tagWithPrefix[1:])
}

return tagNames, nil
}
13 changes: 13 additions & 0 deletions core/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,16 @@ func (f *Formatter) FormatDuration(duration time.Duration) string {
}
return response
}

func (f *Formatter) FormatTags(tags []string) string {
var result string

for i, t := range tags {
result += t
if i < len(tags)-1 {
result += ", "
}
}

return result
}
32 changes: 32 additions & 0 deletions core/formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package core

import "testing"

func TestFormatter_FormatTags(t *testing.T) {
tests := map[string]struct {
tags []string
output string
}{
"one tag": {
tags: []string{"coffee"},
output: "coffee",
},
"two tags": {
tags: []string{"coffee", "espresso"},
output: "coffee, espresso",
},
"four tags": {
tags: []string{"coffee", "espresso", "morning"},
output: "coffee, espresso, morning",
},
}

formatter := Formatter{}

for name, tc := range tests {
formattedTags := formatter.FormatTags(tc.tags)
if formattedTags != tc.output {
t.Errorf("%s: expected %s, got %s", name, tc.output, formattedTags)
}
}
}
1 change: 1 addition & 0 deletions core/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Record struct {
End *time.Time `json:"end"`
Project *Project `json:"project"`
IsBillable bool `json:"is_billable"`
Tags []string `json:"tags"`
}

// Duration calculates time duration for a specific record. If the record doesn't
Expand Down
3 changes: 2 additions & 1 deletion core/timetrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func New(config *config.Config, fs Filesystem) *Timetrace {
// record with the current time as start time.
//
// Since parallel work isn't supported, the previous work must be stopped first.
func (t *Timetrace) Start(projectKey string, isBillable bool) error {
func (t *Timetrace) Start(projectKey string, isBillable bool, tags []string) error {
latestRecord, err := t.LoadLatestRecord()
if err != nil && !errors.Is(err, ErrAllDirectoriesEmpty) {
return err
Expand All @@ -90,6 +90,7 @@ func (t *Timetrace) Start(projectKey string, isBillable bool) error {
Start: time.Now(),
Project: project,
IsBillable: isBillable,
Tags: tags,
}

return t.SaveRecord(record, false)
Expand Down