| 1 | module util |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | // TODO `select` doesn't work with time.Duration for some reason |
| 6 | pub fn execute_with_timeout(cmd string, timeout i64) ?os.Result { |
| 7 | ch := chan os.Result{cap: 1} |
| 8 | spawn fn [cmd] (c chan os.Result) { |
| 9 | res := os.execute(cmd) |
| 10 | c <- res |
| 11 | }(ch) |
| 12 | select { |
| 13 | a := <-ch { |
| 14 | return a |
| 15 | } |
| 16 | // timeout { |
| 17 | // 1000 * time.millisecond { |
| 18 | // timeout * time.millisecond { |
| 19 | timeout * 1_000_000 { |
| 20 | return none |
| 21 | } |
| 22 | } |
| 23 | return os.Result{} |
| 24 | } |
| 25 |