Skip to content

Commit 7c42822

Browse files
committed
tools: add v timeout to be able to later use v timeout 5.1 git -C . fetch V_REPO in v doctor and other tools.
1 parent e6f027d commit 7c42822

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

cmd/tools/vtimeout.v

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import os
2+
import time
3+
import flag
4+
5+
struct Context {
6+
mut:
7+
show_help bool
8+
cmd_line_opts []string
9+
full_cmd string
10+
timeout f64
11+
}
12+
13+
fn main() {
14+
mut ctx := Context{}
15+
args := arguments()
16+
mut fp := flag.new_flag_parser(args#[1..])
17+
fp.application('v timeout')
18+
fp.version('0.0.1')
19+
fp.description('Run a command with a time limit. Example: `v timeout 0.3 v run examples/hello_world.v`')
20+
fp.arguments_description('timeout_in_seconds CMD [ARGS]')
21+
fp.skip_executable()
22+
fp.limit_free_args_to_at_least(2)!
23+
ctx.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
24+
if ctx.show_help {
25+
println(fp.usage())
26+
exit(0)
27+
}
28+
ctx.cmd_line_opts = fp.finalize() or {
29+
eprintln('> error: ${err}')
30+
exit(125) // mimic the exit codes of `timeout` in coreutils
31+
}
32+
ctx.timeout = ctx.cmd_line_opts[0].f64()
33+
ctx.cmd_line_opts = ctx.cmd_line_opts#[1..]
34+
ctx.full_cmd = ctx.cmd_line_opts.join(' ')
35+
spawn fn (ctx Context) {
36+
tperiod := time.Duration(i64(ctx.timeout * time.second))
37+
time.sleep(tperiod)
38+
// eprintln('> error: timeout of ${tperiod.seconds():5.3f}s reached, before command finished; command was: `${ctx.full_cmd}`')
39+
exit(124)
40+
}(ctx)
41+
ecode := os.system(ctx.full_cmd)
42+
exit(ecode)
43+
}

cmd/v/v.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ const external_tools = [
4949
'test-parser',
5050
'test-self',
5151
'time',
52+
'timeout',
5253
'tracev',
5354
'up',
5455
'vet',

0 commit comments

Comments
 (0)