File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ const external_tools = [
49
49
'test-parser' ,
50
50
'test-self' ,
51
51
'time' ,
52
+ 'timeout' ,
52
53
'tracev' ,
53
54
'up' ,
54
55
'vet' ,
You can’t perform that action at this time.
0 commit comments