Skip to content

Commit 1bc3af1

Browse files
committed
add timing and --time
1 parent 893dd2c commit 1bc3af1

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎cmd/tsk/tsk.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Options struct {
3030
pure bool
3131
taskFile string
3232
tasks []string
33+
time bool
3334
which bool
3435
}
3536

@@ -50,6 +51,7 @@ func main() {
5051
flag.BoolVar(&opts.prefix, "prefix", false, "prefix task output with the task name")
5152
flag.BoolVarP(&opts.pure, "pure", "", false, "don't inherit the parent env")
5253
flag.StringVarP(&opts.taskFile, "file", "f", "", "taskfile to use")
54+
flag.BoolVar(&opts.time, "time", false, "show task duration after completion")
5355
flag.BoolVar(&opts.which, "which", false, "print the path to the found tasks.toml, or an error")
5456
flag.BoolVarP(&help, "help", "h", false, "")
5557
flag.Parse()
@@ -94,6 +96,7 @@ func main() {
9496
Stderr: os.Stderr,
9597
Config: cfg,
9698
Prefix: opts.prefix,
99+
Time: opts.time,
97100
}
98101

99102
if opts.listTasks {

‎internal/task/task.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"regexp"
1212
"strings"
1313
"sync"
14+
"time"
1415

1516
output "github.com/notnmeyer/tsk/internal/outputformat"
1617

@@ -48,6 +49,7 @@ type Executor struct {
4849
Stderr io.Writer
4950
Config *Config
5051
Prefix bool
52+
Time bool
5153
colorIndex int
5254
taskColors map[string]string
5355
}
@@ -131,6 +133,8 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
131133
taskConfig.Dir = config.TaskFileDir
132134
}
133135

136+
taskStart := time.Now()
137+
134138
if len(taskConfig.Deps) > 0 {
135139
for _, depGroup := range taskConfig.Deps {
136140
var wg sync.WaitGroup
@@ -162,6 +166,7 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
162166
Stdin: exec.Stdin,
163167
Config: exec.Config,
164168
Prefix: exec.Prefix,
169+
Time: exec.Time,
165170
}
166171

167172
// if a task contains cmds, run them
@@ -184,6 +189,10 @@ func (exec *Executor) RunTasks(config *Config, tasks *[]string) error {
184189
os.Exit(1)
185190
}
186191
}
192+
193+
if exec.Time && exec.Stderr != nil {
194+
fmt.Fprintf(exec.Stderr, "::%s:: duration %s\n", task, formatDuration(time.Since(taskStart)))
195+
}
187196
}
188197
return nil
189198
}
@@ -314,6 +323,18 @@ func (exec *Executor) VerifyTasks(tasks []string) error {
314323
return nil
315324
}
316325

326+
func formatDuration(d time.Duration) string {
327+
if d < time.Second {
328+
return fmt.Sprintf("%dms", d.Milliseconds())
329+
}
330+
if d < time.Minute {
331+
return fmt.Sprintf("%.2fs", d.Seconds())
332+
}
333+
m := int(d.Minutes())
334+
s := d.Seconds() - float64(m*60)
335+
return fmt.Sprintf("%dm%.2fs", m, s)
336+
}
337+
317338
func filterTasks(tasks *map[string]Task, regex *regexp.Regexp) map[string]Task {
318339
filtered := make(map[string]Task)
319340
for k, v := range *tasks {

0 commit comments

Comments
 (0)