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 := `
5152Usage: gtm record [options] [/path/file]
5253
53- Record file or terminal events.
54+ Record file or app events.
5455
5556Options:
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
6770func (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
143173func (c RecordCmd ) Synopsis () string {
144174 return "Record file and terminal events"
0 commit comments