Skip to content

Commit 901d9f0

Browse files
committed
Add option to return current total time when recording file events
1 parent a3f36e8 commit 901d9f0

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

command/commit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (r GitCommit) Run(args []string) int {
5555

5656
func (r GitCommit) Synopsis() string {
5757
return `
58-
Usage: gtm commit [--yes]
58+
Usage: gtm commit [-yes]
5959
Save your logged time with the last commit
6060
This is automatically called from the postcommit hook
6161
Warning - any time logged will be cleared from your working directory

command/record.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package command
22

33
import (
4+
"flag"
45
"fmt"
6+
"os"
7+
"path/filepath"
58

69
"github.com/git-time-metric/gtm/event"
10+
"github.com/git-time-metric/gtm/metric"
11+
"github.com/git-time-metric/gtm/note"
712
"github.com/git-time-metric/gtm/project"
13+
"github.com/git-time-metric/gtm/report"
814

915
"github.com/mitchellh/cli"
1016
)
@@ -21,25 +27,61 @@ func (r RecordCmd) Help() string {
2127
}
2228

2329
func (r RecordCmd) Run(args []string) int {
24-
if len(args) == 0 {
30+
recordFlags := flag.NewFlagSet("record", flag.ExitOnError)
31+
status := recordFlags.Bool(
32+
"status",
33+
false,
34+
"After recording, return current total time spent [gtm status -total-only]")
35+
if err := recordFlags.Parse(os.Args[2:]); err != nil {
36+
fmt.Println(err)
37+
return 1
38+
}
39+
40+
if len(recordFlags.Args()) == 0 {
2541
fmt.Println("Unable to record, file not provided")
2642
return 1
2743
}
2844

29-
//TODO: add an option to turn off silencing ErrFileNotFound errors
30-
if err := event.Record(args[0]); err != nil && !(err == project.ErrNotInitialized || err == project.ErrFileNotFound) {
45+
if err := event.Record(recordFlags.Args()[0]); err != nil && !(err == project.ErrNotInitialized || err == project.ErrFileNotFound) {
3146
if err := project.Log(err); err != nil {
3247
fmt.Println(err)
3348
}
3449
return 1
50+
} else if err == nil && *status {
51+
var (
52+
err error
53+
commitNote note.CommitNote
54+
out string
55+
wd string
56+
)
57+
58+
wd, err = os.Getwd()
59+
if err != nil {
60+
fmt.Println(err)
61+
return 1
62+
}
63+
defer os.Chdir(wd)
64+
65+
os.Chdir(filepath.Dir(recordFlags.Args()[0]))
66+
67+
if commitNote, err = metric.Process(true); err != nil {
68+
fmt.Println(err)
69+
return 1
70+
}
71+
out, err = report.Status(commitNote, *status)
72+
if err != nil {
73+
fmt.Println(err)
74+
return 1
75+
}
76+
fmt.Printf(out)
3577
}
3678

3779
return 0
3880
}
3981

4082
func (r RecordCmd) Synopsis() string {
4183
return `
42-
Usage: gtm record <filepath>
84+
Usage: gtm record [-status] <path/file>
4385
Record a file event
4486
`
4587
}

0 commit comments

Comments
 (0)