Skip to content
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
6 changes: 4 additions & 2 deletions command/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ Options:

-yes Delete time data without asking for confirmation.
-terminal-only Only delete terminal time data
-app-only Only delete apps time data
-days=0 Delete starting from n days in the past
`
return strings.TrimSpace(helpText)
}

// Run executes clean command with args
func (c CleanCmd) Run(args []string) int {
var yes, terminalOnly bool
var yes, terminalOnly, appOnly bool
var days int
cmdFlags := flag.NewFlagSet("clean", flag.ContinueOnError)
cmdFlags.BoolVar(&yes, "yes", false, "")
cmdFlags.BoolVar(&terminalOnly, "terminal-only", false, "")
cmdFlags.BoolVar(&appOnly, "app-only", false, "")
cmdFlags.IntVar(&days, "days", 0, "")
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
Expand All @@ -62,7 +64,7 @@ func (c CleanCmd) Run(args []string) int {
}

if confirm {
if err := project.Clean(util.AfterNow(days), terminalOnly); err != nil {
if err := project.Clean(util.AfterNow(days), terminalOnly, appOnly); err != nil {
c.UI.Error(err.Error())
return 1
}
Expand Down
33 changes: 33 additions & 0 deletions command/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package command

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/git-time-metric/gtm/project"
"github.com/git-time-metric/gtm/util"
"github.com/mitchellh/cli"
)
Expand Down Expand Up @@ -51,6 +53,37 @@ func TestTerminalOnly(t *testing.T) {
}
}

func TestAppOnly(t *testing.T) {
repo := util.NewTestRepo(t, false)
defer repo.Remove()
repo.Seed()
os.Chdir(repo.Workdir())

repo.SaveFile("browser.app", project.GTMDir, "")
repo.SaveFile("1458496803.event", project.GTMDir, filepath.Join("event", "event.go"))
repo.SaveFile("1458497804.event", project.GTMDir, filepath.Join(project.GTMDir, "browser.app"))

(InitCmd{UI: new(cli.MockUi)}).Run([]string{})

ui := new(cli.MockUi)
c := CleanCmd{UI: ui}

args := []string{"-app-only", "-yes"}
rc := c.Run(args)

if rc != 0 {
t.Errorf("gtm clean(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
}

if !repo.FileExists("1458496803.event", project.GTMDir) {
t.Errorf("gtm clean(%+v), want non-app event to not be deleted, but was deleted", args)
}

if repo.FileExists("1458497804.event", project.GTMDir) {
t.Errorf("gtm clean(%+v), want app event to be deleted, but was found", args)
}
}

func TestCleanInvalidOption(t *testing.T) {
ui := new(cli.MockUi)
c := CleanCmd{UI: ui}
Expand Down
58 changes: 44 additions & 14 deletions command/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -50,26 +51,29 @@ func (c RecordCmd) Help() string {
helpText := `
Usage: gtm record [options] [/path/file]

Record file or terminal events.
Record file or app events.

Options:

-terminal=false Record a terminal event.

-status=false Return total time recorded for event.

-long-duration=false Return total time recorded in long duration format
-long-duration=false Return total time recorded in long duration format.

-app=false Record an app event.
`
return strings.TrimSpace(helpText)
}

// Run executes record command with args
func (c RecordCmd) Run(args []string) int {
var status, terminal, longDuration bool
var status, terminal, longDuration, app bool
cmdFlags := flag.NewFlagSet("record", flag.ContinueOnError)
cmdFlags.BoolVar(&status, "status", false, "")
cmdFlags.BoolVar(&terminal, "terminal", false, "")
cmdFlags.BoolVar(&longDuration, "long-duration", false, "")
cmdFlags.BoolVar(&app, "app", false, "")
cmdFlags.Usage = func() { c.UI.Output(c.Help()) }
if err := cmdFlags.Parse(args); err != nil {
return 1
Expand All @@ -82,21 +86,20 @@ func (c RecordCmd) Run(args []string) int {

var fileToRecord string
if terminal {
projPath, err := scm.GitRepoPath()
if err != nil {
// if not found, ignore error
return 0
}
projPath, err = scm.Workdir(projPath)
if err != nil {
// if not found, ignore error
return 0
}
fileToRecord = filepath.Join(projPath, ".gtm", "terminal.app")
fileToRecord = "terminal"
app = true
} else {
fileToRecord = cmdFlags.Args()[0]
}

if app {
fileToRecord = c.appToFile(fileToRecord)
}

if !(0 <= len(fileToRecord)) {
return 0
}

if err := event.Record(fileToRecord); err != nil && !(err == project.ErrNotInitialized || err == project.ErrFileNotFound) {
return 1
} else if err == nil && status {
Expand Down Expand Up @@ -139,6 +142,33 @@ func (c RecordCmd) Run(args []string) int {
return 0
}

// Given an app name creates (if it not was already created) the file ".gtm/{name}.app"
// that we use to track events, and returns the full path
func (c RecordCmd) appToFile(appName string) string {
if !(len(appName) > 0) {
return ""
}
projPath, err := scm.GitRepoPath()
if err != nil {
return ""
}
projPath, err = scm.Workdir(projPath)
if err != nil {
return ""
}

var file = filepath.Join(projPath, ".gtm", appName+".app")

if _, err := os.Stat(file); os.IsNotExist(err) {
ioutil.WriteFile(
file,
[]byte{},
0644)
}

return file
}

// Synopsis returns help
func (c RecordCmd) Synopsis() string {
return "Record file and terminal events"
Expand Down
39 changes: 39 additions & 0 deletions command/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,45 @@ func TestRecordTerminalWithStatus(t *testing.T) {
t.Errorf("gtm record(%+v), want 1 event file got %d, %s", args, cnt, ui.ErrorWriter.String())
}
}

func TestRecordApp(t *testing.T) {
repo := util.NewTestRepo(t, false)
defer repo.Remove()
repo.Seed()
workdir := repo.Workdir()
os.Chdir(workdir)

(InitCmd{UI: new(cli.MockUi)}).Run([]string{})

ui := new(cli.MockUi)
c := RecordCmd{UI: ui}

args := []string{"-app", "browser"}
rc := c.Run(args)

if rc != 0 {
t.Errorf("gtm record(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter)
}

if _, err := os.Stat(filepath.Join(workdir, ".gtm", "browser.app")); os.IsNotExist(err) {
t.Errorf("gtm record(%+v), want .app file to be created, it was not created", args)
}

files, err := ioutil.ReadDir(filepath.Join(workdir, ".gtm"))
if err != nil {
t.Fatalf("gtm record(%+v), want error nil got %s", args, err)
}
cnt := 1
for _, f := range files {
if filepath.Base(f.Name()) == ".event" {
cnt++
}
}
if cnt != 1 {
t.Errorf("gtm record(%+v), want 1 event file got %d, %s", args, cnt, ui.ErrorWriter.String())
}
}

func TestRecordInvalidOption(t *testing.T) {
ui := new(cli.MockUi)
c := RecordCmd{UI: ui}
Expand Down
5 changes: 4 additions & 1 deletion command/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Options:
-format=commits Specify report format [summary|project|commits|files|timeline-hours|timeline-commits] (default commits)
-full-message=false Include full commit message
-terminal-off=false Exclude time spent in terminal (Terminal plug-in is required)
-app-off=false Exclude time spent in apps
-force-color=false Always output color even if no terminal is detected, i.e 'gtm report -color | less -R'
-testing=false This is used for automated testing to force default test path

Expand Down Expand Up @@ -76,12 +77,13 @@ Options:
// Run executes report command with args
func (c ReportCmd) Run(args []string) int {
var limit int
var color, terminalOff, fullMessage, testing bool
var color, terminalOff, appOff, fullMessage, testing bool
var today, yesterday, thisWeek, lastWeek, thisMonth, lastMonth, thisYear, lastYear, all bool
var fromDate, toDate, message, author, tags, format string
cmdFlags := flag.NewFlagSet("report", flag.ContinueOnError)
cmdFlags.BoolVar(&color, "force-color", false, "")
cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "")
cmdFlags.BoolVar(&appOff, "app-off", false, "")
cmdFlags.StringVar(&format, "format", "commits", "")
cmdFlags.IntVar(&limit, "n", 0, "")
cmdFlags.BoolVar(&fullMessage, "full-message", false, "")
Expand Down Expand Up @@ -222,6 +224,7 @@ func (c ReportCmd) Run(args []string) int {
options := report.OutputOptions{
FullMessage: fullMessage,
TerminalOff: terminalOff,
AppOff: appOff,
Color: color,
Limit: limit}

Expand Down
43 changes: 43 additions & 0 deletions command/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,49 @@ func TestReportFiles(t *testing.T) {
}
}

func TestReportAppsOff(t *testing.T) {
repo := util.NewTestRepo(t, false)
defer repo.Remove()
os.Chdir(repo.Workdir())

(InitCmd{UI: new(cli.MockUi)}).Run([]string{})

repo.SaveFile("event.go", "event", "")
repo.SaveFile("browser.app", project.GTMDir, "")
repo.SaveFile("1458496803.event", project.GTMDir, filepath.Join("event", "event.go"))
repo.SaveFile("1458496818.event", project.GTMDir, filepath.Join(project.GTMDir, "browser.app"))

repo.Commit(repo.Stage(filepath.Join("event", "event.go")))

// save notes to git repository
(CommitCmd{UI: new(cli.MockUi)}).Run([]string{"-yes"})

ui := new(cli.MockUi)
c := ReportCmd{UI: ui}

// Including apps
args := []string{"-format", "files", "-testing=true"}
rc := c.Run(args)
if rc != 0 {
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
}
if !strings.Contains(ui.OutputWriter.String(), "Browser") {
t.Errorf("gtm report(%+v), want 'Browser' got %s, %s", args, ui.OutputWriter.String(), ui.ErrorWriter.String())
}

// Excluding apps
ui.OutputWriter.Reset()
ui.ErrorWriter.Reset()
args = []string{"-app-off", "-format", "files", "-testing=true"}
rc = c.Run(args)
if rc != 0 {
t.Errorf("gtm report(%+v), want 0 got %d, %s", args, rc, ui.ErrorWriter.String())
}
if strings.Contains(ui.OutputWriter.String(), "Browser") {
t.Errorf("gtm report(%+v), want not 'Browser' got %s, %s", args, ui.OutputWriter.String(), ui.ErrorWriter.String())
}
}

func TestReportInvalidOption(t *testing.T) {
ui := new(cli.MockUi)
c := ReportCmd{UI: ui}
Expand Down
6 changes: 5 additions & 1 deletion command/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Options:

-terminal-off=false Exclude time spent in terminal (Terminal plug-in is required)

-app-off=false Exclude time spent in apps

-color=false Always output color even if no terminal is detected, i.e 'gtm status -color | less -R'

-total-only=false Only display total pending time
Expand All @@ -53,11 +55,12 @@ Options:

// Run executes status command with args
func (c StatusCmd) Run(args []string) int {
var color, terminalOff, totalOnly, all, profile, longDuration bool
var color, terminalOff, appOff, totalOnly, all, profile, longDuration bool
var tags string
cmdFlags := flag.NewFlagSet("status", flag.ContinueOnError)
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'")
cmdFlags.BoolVar(&terminalOff, "terminal-off", false, "Exclude time spent in terminal (Terminal plugin is required)")
cmdFlags.BoolVar(&appOff, "app-off", false, "Exclude time spent in apps")
cmdFlags.BoolVar(&totalOnly, "total-only", false, "Only display total time")
cmdFlags.BoolVar(&longDuration, "long-duration", false, "Display total time in long duration format")
cmdFlags.StringVar(&tags, "tags", "", "Project tags to show status on")
Expand Down Expand Up @@ -100,6 +103,7 @@ func (c StatusCmd) Run(args []string) int {
TotalOnly: totalOnly,
LongDuration: longDuration,
TerminalOff: terminalOff,
AppOff: appOff,
Color: color}

for _, projPath := range projects {
Expand Down
Loading