Skip to content

Commit 5cd6bd1

Browse files
authored
Merge pull request #98 from desaroger/feature/support-tracking-apps
Support tracking apps
2 parents 8e616f8 + 6e8c12b commit 5cd6bd1

16 files changed

Lines changed: 316 additions & 38 deletions

command/clean.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,20 @@ Options:
3434
3535
-yes Delete time data without asking for confirmation.
3636
-terminal-only Only delete terminal time data
37+
-app-only Only delete apps time data
3738
-days=0 Delete starting from n days in the past
3839
`
3940
return strings.TrimSpace(helpText)
4041
}
4142

4243
// Run executes clean command with args
4344
func (c CleanCmd) Run(args []string) int {
44-
var yes, terminalOnly bool
45+
var yes, terminalOnly, appOnly bool
4546
var days int
4647
cmdFlags := flag.NewFlagSet("clean", flag.ContinueOnError)
4748
cmdFlags.BoolVar(&yes, "yes", false, "")
4849
cmdFlags.BoolVar(&terminalOnly, "terminal-only", false, "")
50+
cmdFlags.BoolVar(&appOnly, "app-only", false, "")
4951
cmdFlags.IntVar(&days, "days", 0, "")
5052
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
5153
if err := cmdFlags.Parse(args); err != nil {
@@ -62,7 +64,7 @@ func (c CleanCmd) Run(args []string) int {
6264
}
6365

6466
if confirm {
65-
if err := project.Clean(util.AfterNow(days), terminalOnly); err != nil {
67+
if err := project.Clean(util.AfterNow(days), terminalOnly, appOnly); err != nil {
6668
c.UI.Error(err.Error())
6769
return 1
6870
}

command/clean_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package command
66

77
import (
88
"os"
9+
"path/filepath"
910
"strings"
1011
"testing"
1112

13+
"github.com/git-time-metric/gtm/project"
1214
"github.com/git-time-metric/gtm/util"
1315
"github.com/mitchellh/cli"
1416
)
@@ -51,6 +53,37 @@ func TestTerminalOnly(t *testing.T) {
5153
}
5254
}
5355

56+
func TestAppOnly(t *testing.T) {
57+
repo := util.NewTestRepo(t, false)
58+
defer repo.Remove()
59+
repo.Seed()
60+
os.Chdir(repo.Workdir())
61+
62+
repo.SaveFile("browser.app", project.GTMDir, "")
63+
repo.SaveFile("1458496803.event", project.GTMDir, filepath.Join("event", "event.go"))
64+
repo.SaveFile("1458497804.event", project.GTMDir, filepath.Join(project.GTMDir, "browser.app"))
65+
66+
(InitCmd{UI: new(cli.MockUi)}).Run([]string{})
67+
68+
ui := new(cli.MockUi)
69+
c := CleanCmd{UI: ui}
70+
71+
args := []string{"-app-only", "-yes"}
72+
rc := c.Run(args)
73+
74+
if rc != 0 {
75+
t.Errorf("gtm clean(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
76+
}
77+
78+
if !repo.FileExists("1458496803.event", project.GTMDir) {
79+
t.Errorf("gtm clean(%+v), want non-app event to not be deleted, but was deleted", args)
80+
}
81+
82+
if repo.FileExists("1458497804.event", project.GTMDir) {
83+
t.Errorf("gtm clean(%+v), want app event to be deleted, but was found", args)
84+
}
85+
}
86+
5487
func TestCleanInvalidOption(t *testing.T) {
5588
ui := new(cli.MockUi)
5689
c := CleanCmd{UI: ui}

command/record.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bytes"
99
"flag"
1010
"fmt"
11+
"io/ioutil"
1112
"os"
1213
"path/filepath"
1314
"strings"
@@ -50,26 +51,29 @@ func (c RecordCmd) Help() string {
5051
helpText := `
5152
Usage: gtm record [options] [/path/file]
5253
53-
Record file or terminal events.
54+
Record file or app events.
5455
5556
Options:
5657
5758
-terminal=false Record a terminal event.
5859
5960
-status=false Return total time recorded for event.
6061
61-
-long-duration=false Return total time recorded in long duration format
62+
-long-duration=false Return total time recorded in long duration format.
63+
64+
-app=false Record an app event.
6265
`
6366
return strings.TrimSpace(helpText)
6467
}
6568

6669
// Run executes record command with args
6770
func (c RecordCmd) Run(args []string) int {
68-
var status, terminal, longDuration bool
71+
var status, terminal, longDuration, app bool
6972
cmdFlags := flag.NewFlagSet("record", flag.ContinueOnError)
7073
cmdFlags.BoolVar(&status, "status", false, "")
7174
cmdFlags.BoolVar(&terminal, "terminal", false, "")
7275
cmdFlags.BoolVar(&longDuration, "long-duration", false, "")
76+
cmdFlags.BoolVar(&app, "app", false, "")
7377
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
7478
if err := cmdFlags.Parse(args); err != nil {
7579
return 1
@@ -82,21 +86,20 @@ func (c RecordCmd) Run(args []string) int {
8286

8387
var fileToRecord string
8488
if terminal {
85-
projPath, err := scm.GitRepoPath()
86-
if err != nil {
87-
// if not found, ignore error
88-
return 0
89-
}
90-
projPath, err = scm.Workdir(projPath)
91-
if err != nil {
92-
// if not found, ignore error
93-
return 0
94-
}
95-
fileToRecord = filepath.Join(projPath, ".gtm", "terminal.app")
89+
fileToRecord = "terminal"
90+
app = true
9691
} else {
9792
fileToRecord = cmdFlags.Args()[0]
9893
}
9994

95+
if app {
96+
fileToRecord = c.appToFile(fileToRecord)
97+
}
98+
99+
if !(0 <= len(fileToRecord)) {
100+
return 0
101+
}
102+
100103
if err := event.Record(fileToRecord); err != nil && !(err == project.ErrNotInitialized || err == project.ErrFileNotFound) {
101104
return 1
102105
} else if err == nil && status {
@@ -139,6 +142,33 @@ func (c RecordCmd) Run(args []string) int {
139142
return 0
140143
}
141144

145+
// Given an app name creates (if it not was already created) the file ".gtm/{name}.app"
146+
// that we use to track events, and returns the full path
147+
func (c RecordCmd) appToFile(appName string) string {
148+
if !(len(appName) > 0) {
149+
return ""
150+
}
151+
projPath, err := scm.GitRepoPath()
152+
if err != nil {
153+
return ""
154+
}
155+
projPath, err = scm.Workdir(projPath)
156+
if err != nil {
157+
return ""
158+
}
159+
160+
var file = filepath.Join(projPath, ".gtm", appName+".app")
161+
162+
if _, err := os.Stat(file); os.IsNotExist(err) {
163+
ioutil.WriteFile(
164+
file,
165+
[]byte{},
166+
0644)
167+
}
168+
169+
return file
170+
}
171+
142172
// Synopsis returns help
143173
func (c RecordCmd) Synopsis() string {
144174
return "Record file and terminal events"

command/record_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,45 @@ func TestRecordTerminalWithStatus(t *testing.T) {
235235
t.Errorf("gtm record(%+v), want 1 event file got %d, %s", args, cnt, ui.ErrorWriter.String())
236236
}
237237
}
238+
239+
func TestRecordApp(t *testing.T) {
240+
repo := util.NewTestRepo(t, false)
241+
defer repo.Remove()
242+
repo.Seed()
243+
workdir := repo.Workdir()
244+
os.Chdir(workdir)
245+
246+
(InitCmd{UI: new(cli.MockUi)}).Run([]string{})
247+
248+
ui := new(cli.MockUi)
249+
c := RecordCmd{UI: ui}
250+
251+
args := []string{"-app", "browser"}
252+
rc := c.Run(args)
253+
254+
if rc != 0 {
255+
t.Errorf("gtm record(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter)
256+
}
257+
258+
if _, err := os.Stat(filepath.Join(workdir, ".gtm", "browser.app")); os.IsNotExist(err) {
259+
t.Errorf("gtm record(%+v), want .app file to be created, it was not created", args)
260+
}
261+
262+
files, err := ioutil.ReadDir(filepath.Join(workdir, ".gtm"))
263+
if err != nil {
264+
t.Fatalf("gtm record(%+v), want error nil got %s", args, err)
265+
}
266+
cnt := 1
267+
for _, f := range files {
268+
if filepath.Base(f.Name()) == ".event" {
269+
cnt++
270+
}
271+
}
272+
if cnt != 1 {
273+
t.Errorf("gtm record(%+v), want 1 event file got %d, %s", args, cnt, ui.ErrorWriter.String())
274+
}
275+
}
276+
238277
func TestRecordInvalidOption(t *testing.T) {
239278
ui := new(cli.MockUi)
240279
c := RecordCmd{UI: ui}

command/report.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Options:
4646
-format=commits Specify report format [summary|project|commits|files|timeline-hours|timeline-commits] (default commits)
4747
-full-message=false Include full commit message
4848
-terminal-off=false Exclude time spent in terminal (Terminal plug-in is required)
49+
-app-off=false Exclude time spent in apps
4950
-force-color=false Always output color even if no terminal is detected, i.e 'gtm report -color | less -R'
5051
-testing=false This is used for automated testing to force default test path
5152
@@ -76,12 +77,13 @@ Options:
7677
// Run executes report command with args
7778
func (c ReportCmd) Run(args []string) int {
7879
var limit int
79-
var color, terminalOff, fullMessage, testing bool
80+
var color, terminalOff, appOff, fullMessage, testing bool
8081
var today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth, thisYear, lastYear, all bool
8182
var fromDate, toDate, message, author, tags, format string
8283
cmdFlags := flag.NewFlagSet("report", flag.ContinueOnError)
8384
cmdFlags.BoolVar(&color, "force-color", false, "")
8485
cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "")
86+
cmdFlags.BoolVar(&appOff, "app-off", false, "")
8587
cmdFlags.StringVar(&format, "format", "commits", "")
8688
cmdFlags.IntVar(&limit, "n", 0, "")
8789
cmdFlags.BoolVar(&fullMessage, "full-message", false, "")
@@ -222,6 +224,7 @@ func (c ReportCmd) Run(args []string) int {
222224
options := report.OutputOptions{
223225
FullMessage: fullMessage,
224226
TerminalOff: terminalOff,
227+
AppOff: appOff,
225228
Color: color,
226229
Limit: limit}
227230

command/report_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,49 @@ func TestReportFiles(t *testing.T) {
256256
}
257257
}
258258

259+
func TestReportAppsOff(t *testing.T) {
260+
repo := util.NewTestRepo(t, false)
261+
defer repo.Remove()
262+
os.Chdir(repo.Workdir())
263+
264+
(InitCmd{UI: new(cli.MockUi)}).Run([]string{})
265+
266+
repo.SaveFile("event.go", "event", "")
267+
repo.SaveFile("browser.app", project.GTMDir, "")
268+
repo.SaveFile("1458496803.event", project.GTMDir, filepath.Join("event", "event.go"))
269+
repo.SaveFile("1458496818.event", project.GTMDir, filepath.Join(project.GTMDir, "browser.app"))
270+
271+
repo.Commit(repo.Stage(filepath.Join("event", "event.go")))
272+
273+
// save notes to git repository
274+
(CommitCmd{UI: new(cli.MockUi)}).Run([]string{"-yes"})
275+
276+
ui := new(cli.MockUi)
277+
c := ReportCmd{UI: ui}
278+
279+
// Including apps
280+
args := []string{"-format", "files", "-testing=true"}
281+
rc := c.Run(args)
282+
if rc != 0 {
283+
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
284+
}
285+
if !strings.Contains(ui.OutputWriter.String(), "Browser") {
286+
t.Errorf("gtm report(%+v), want 'Browser' got %s, %s", args, ui.OutputWriter.String(), ui.ErrorWriter.String())
287+
}
288+
289+
// Excluding apps
290+
ui.OutputWriter.Reset()
291+
ui.ErrorWriter.Reset()
292+
args = []string{"-app-off", "-format", "files", "-testing=true"}
293+
rc = c.Run(args)
294+
if rc != 0 {
295+
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
296+
}
297+
if strings.Contains(ui.OutputWriter.String(), "Browser") {
298+
t.Errorf("gtm report(%+v), want not 'Browser' got %s, %s", args, ui.OutputWriter.String(), ui.ErrorWriter.String())
299+
}
300+
}
301+
259302
func TestReportInvalidOption(t *testing.T) {
260303
ui := new(cli.MockUi)
261304
c := ReportCmd{UI: ui}

command/status.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Options:
3838
3939
-terminal-off=false Exclude time spent in terminal (Terminal plug-in is required)
4040
41+
-app-off=false Exclude time spent in apps
42+
4143
-color=false Always output color even if no terminal is detected, i.e 'gtm status -color | less -R'
4244
4345
-total-only=false Only display total pending time
@@ -53,11 +55,12 @@ Options:
5355

5456
// Run executes status command with args
5557
func (c StatusCmd) Run(args []string) int {
56-
var color, terminalOff, totalOnly, all, profile, longDuration bool
58+
var color, terminalOff, appOff, totalOnly, all, profile, longDuration bool
5759
var tags string
5860
cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
5961
cmdFlags.BoolVar(&color, "color", false, "Always output color even if no terminal is detected. Use this with pagers i.e 'less -R' or 'more -R'")
6062
cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "Exclude time spent in terminal (Terminal plugin is required)")
63+
cmdFlags.BoolVar(&appOff, "app-off", false, "Exclude time spent in apps")
6164
cmdFlags.BoolVar(&totalOnly, "total-only", false, "Only display total time")
6265
cmdFlags.BoolVar(&longDuration, "long-duration", false, "Display total time in long duration format")
6366
cmdFlags.StringVar(&tags, "tags", "", "Project tags to show status on")
@@ -100,6 +103,7 @@ func (c StatusCmd) Run(args []string) int {
100103
TotalOnly: totalOnly,
101104
LongDuration: longDuration,
102105
TerminalOff: terminalOff,
106+
AppOff: appOff,
103107
Color: color}
104108

105109
for _, projPath := range projects {

0 commit comments

Comments
 (0)