mirror of
https://github.com/vlang/v.git
synced 2025-09-12 08:57:09 -04:00
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:
parent
e6f027dfa0
commit
7c42822f77
43
cmd/tools/vtimeout.v
Normal file
43
cmd/tools/vtimeout.v
Normal 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)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user