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.

This commit is contained in:
Delyan Angelov 2025-03-18 10:57:36 +02:00
parent e6f027dfa0
commit 7c42822f77
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 44 additions and 0 deletions

43
cmd/tools/vtimeout.v Normal file
View File

@ -0,0 +1,43 @@
import os
import time
import flag
struct Context {
mut:
show_help bool
cmd_line_opts []string
full_cmd string
timeout f64
}
fn main() {
mut ctx := Context{}
args := arguments()
mut fp := flag.new_flag_parser(args#[1..])
fp.application('v timeout')
fp.version('0.0.1')
fp.description('Run a command with a time limit. Example: `v timeout 0.3 v run examples/hello_world.v`')
fp.arguments_description('timeout_in_seconds CMD [ARGS]')
fp.skip_executable()
fp.limit_free_args_to_at_least(2)!
ctx.show_help = fp.bool('help', `h`, false, 'Show this help screen.')
if ctx.show_help {
println(fp.usage())
exit(0)
}
ctx.cmd_line_opts = fp.finalize() or {
eprintln('> error: ${err}')
exit(125) // mimic the exit codes of `timeout` in coreutils
}
ctx.timeout = ctx.cmd_line_opts[0].f64()
ctx.cmd_line_opts = ctx.cmd_line_opts#[1..]
ctx.full_cmd = ctx.cmd_line_opts.join(' ')
spawn fn (ctx Context) {
tperiod := time.Duration(i64(ctx.timeout * time.second))
time.sleep(tperiod)
// eprintln('> error: timeout of ${tperiod.seconds():5.3f}s reached, before command finished; command was: `${ctx.full_cmd}`')
exit(124)
}(ctx)
ecode := os.system(ctx.full_cmd)
exit(ecode)
}

View File

@ -49,6 +49,7 @@ const external_tools = [
'test-parser',
'test-self',
'time',
'timeout',
'tracev',
'up',
'vet',