Skip to content

Commit d7049ae

Browse files
authored
os: deprecate os.exec (returning ?os.Result), in favour of os.execute, which returns os.Result (vlang#8974)
1 parent 10c9f61 commit d7049ae

52 files changed

Lines changed: 423 additions & 344 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,5 +1015,5 @@ jobs:
10151015
run: git clone --depth 1 https://github.com/vlang/vab
10161016
- name: Build vab
10171017
run: cd vab; ../v ./vab.v ; cd ..
1018-
- name: Build vab with -prod
1019-
run: cd vab; ../v -prod ./vab.v ; cd ..
1018+
- name: Build vab with
1019+
run: cd vab; ../v ./vab.v ; cd ..

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ from local variables.
2929
- Support for compile time environment variables via `$env('ENV_VAR')`.
3030
- Allow method declaration of `==` and `<` operators and auto generate `!=`, `>`, `<=` and `>=`.
3131
- support `dump(expr)`, i.e. tracing of both the location, name and value of an expression
32+
- deprecate os.exec in favour of os.executable() which does *NOT* return an option, when the command was not found
3233

3334
## V 0.2.1
3435
*30 Dec 2020*

cmd/tools/fast/fast.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn main() {
5050
date := time.unix(commit_date.int())
5151
mut out := os.create('table.html') ?
5252
// Place the new row on top
53-
table =
53+
table =
5454
'<tr>
5555
<td>$date.format()</td>
5656
<td><a target=_blank href="https://github.com/vlang/v/commit/$commit">$commit</a></td>
@@ -81,7 +81,7 @@ fn main() {
8181
}
8282

8383
fn exec(s string) string {
84-
e := os.exec(s) or { panic(err) }
84+
e := os.execute_or_panic(s)
8585
return e.output.trim_right('\r\n')
8686
}
8787

@@ -111,7 +111,7 @@ fn measure(cmd string, description string) int {
111111
}
112112

113113
fn measure_steps(vdir string) (int, int, int) {
114-
resp := os.exec('$vdir/vprod -o v.c -show-timings $vdir/cmd/v') or { panic(err) }
114+
resp := os.execute_or_panic('$vdir/vprod -o v.c -show-timings $vdir/cmd/v')
115115
lines := resp.output.split_into_lines()
116116
if lines.len != 3 {
117117
return 0, 0, 0

cmd/tools/fast/fast_job.v

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ fn main() {
1717
return
1818
}
1919
for {
20-
os.exec('git pull --rebase') or {
20+
res_pull := os.execute('git pull --rebase')
21+
if res_pull.exit_code != 0 {
2122
println('failed to git pull. uncommitted changes?')
2223
return
2324
}
2425
// println('running fast')
25-
resp := os.exec('./fast') or {
26-
println(err)
26+
resp := os.execute('./fast')
27+
if resp.exit_code < 0 {
28+
println(resp.output)
2729
return
2830
}
2931
if resp.exit_code != 0 {
3032
println('resp != 0, skipping')
3133
} else {
3234
os.chdir('website')
33-
os.exec('git checkout gh-pages') ?
35+
os.execute_or_panic('git checkout gh-pages')
3436
os.cp('../index.html', 'index.html') ?
3537
os.system('git commit -am "update benchmark"')
3638
os.system('git push origin gh-pages')

cmd/tools/gen_vc.v

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ const (
4343
// version
4444
app_version = '0.1.2'
4545
// description
46-
app_description = "This tool regenerates V\'s bootstrap .c files every time the V master branch is updated."
46+
app_description = "This tool regenerates V's bootstrap .c files every time the V master branch is updated."
4747
// assume something went wrong if file size less than this
4848
too_short_file_limit = 5000
4949
// create a .c file for these os's
5050
vc_build_oses = [
5151
'nix',
52-
/* all nix based os */
52+
// all nix based os
5353
'windows',
5454
]
5555
)
@@ -188,8 +188,8 @@ fn parse_flags(mut fp flag.FlagParser) FlagOptions {
188188
work_dir: fp.string('work-dir', 0, work_dir, 'gen_vc working directory')
189189
purge: fp.bool('purge', 0, false, 'force purge the local repositories')
190190
port: fp.int('port', 0, server_port, 'port for web server to listen on')
191-
log_to: fp.string('log-to', 0, log_to, "log to is \'file\' or \'terminal\'")
192-
log_file: fp.string('log-file', 0, log_file, "log file to use when log-to is \'file\'")
191+
log_to: fp.string('log-to', 0, log_to, "log to is 'file' or 'terminal'")
192+
log_file: fp.string('log-file', 0, log_file, "log file to use when log-to is 'file'")
193193
dry_run: fp.bool('dry-run', 0, dry_run, 'when specified dont push anything to remote repo')
194194
force: fp.bool('force', 0, false, 'force update even if already up to date')
195195
}
@@ -318,9 +318,10 @@ fn (mut gen_vc GenVC) command_execute(cmd string, dry bool) string {
318318
return gen_vc.command_execute_dry(cmd)
319319
}
320320
gen_vc.logger.info('cmd: $cmd')
321-
r := os.exec(cmd) or {
321+
r := os.execute(cmd)
322+
if r.exit_code < 0 {
322323
gen_vc.logger.error('$err_msg_cmd_x: "$cmd" could not start.')
323-
gen_vc.logger.error(err.msg)
324+
gen_vc.logger.error(r.output)
324325
// something went wrong, better start fresh next time
325326
gen_vc.purge_repos()
326327
gen_vc.gen_error = true

cmd/tools/modules/scripting/scripting.v

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,23 @@ pub fn rmrf(path string) {
6060
}
6161
}
6262

63+
// execute a command, and return a result, or an error, if it failed in any way.
6364
pub fn exec(cmd string) ?os.Result {
6465
verbose_trace(@FN, cmd)
65-
x := os.exec(cmd) or {
66+
x := os.execute(cmd)
67+
if x.exit_code != 0 {
6668
verbose_trace(@FN, '## failed.')
67-
return err
69+
return error(x.output)
6870
}
6971
verbose_trace_exec_result(x)
7072
return x
7173
}
7274

75+
// run a command, tracing its results, and returning ONLY its output
7376
pub fn run(cmd string) string {
7477
verbose_trace(@FN, cmd)
75-
x := os.exec(cmd) or {
78+
x := os.execute(cmd)
79+
if x.exit_code < 0 {
7680
verbose_trace(@FN, '## failed.')
7781
return ''
7882
}
@@ -85,7 +89,8 @@ pub fn run(cmd string) string {
8589

8690
pub fn exit_0_status(cmd string) bool {
8791
verbose_trace(@FN, cmd)
88-
x := os.exec(cmd) or {
92+
x := os.execute(cmd)
93+
if x.exit_code < 0 {
8994
verbose_trace(@FN, '## failed.')
9095
return false
9196
}

cmd/tools/modules/testing/common.v

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
287287
if testing.show_start {
288288
ts.append_message(.info, ' starting $relative_file ...')
289289
}
290-
r := os.exec(cmd) or {
290+
r := os.execute(cmd)
291+
if r.exit_code < 0 {
291292
ts.failed = true
292293
ts.benchmark.fail()
293294
tls_bench.fail()
@@ -387,7 +388,10 @@ pub fn v_build_failing_skipped(zargs string, folder string, oskipped []string) b
387388
}
388389

389390
pub fn build_v_cmd_failed(cmd string) bool {
390-
res := os.exec(cmd) or { return true }
391+
res := os.execute(cmd)
392+
if res.exit_code < 0 {
393+
return true
394+
}
391395
if res.exit_code != 0 {
392396
eprintln('')
393397
eprintln(res.output)

cmd/tools/oldv.v

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ const (
2929

3030
struct Context {
3131
mut:
32-
vgo vgit.VGitOptions
33-
commit_v string = 'master'
32+
vgo vgit.VGitOptions
33+
commit_v string = 'master'
3434
// the commit from which you want to produce a working v compiler (this may be a commit-ish too)
35-
commit_vc string = 'master'
35+
commit_vc string = 'master'
3636
// this will be derived from commit_v
3737
commit_v_hash string // this will be filled from the commit-ish commit_v using rev-list. It IS a commit hash.
3838
path_v string // the full path to the v folder inside workdir.
3939
path_vc string // the full path to the vc folder inside workdir.
4040
cmd_to_run string // the command that you want to run *in* the oldv repo
4141
cc string = 'cc'
4242
// the C compiler to use for bootstrapping.
43-
cleanup bool // should the tool run a cleanup first
43+
cleanup bool // should the tool run a cleanup first
4444
}
4545

4646
fn (mut c Context) compile_oldv_if_needed() {
@@ -100,9 +100,7 @@ fn main() {
100100
scripting.cprintln('# v commit hash: $context.commit_v_hash')
101101
scripting.cprintln('# checkout folder: $context.path_v')
102102
if context.cmd_to_run.len > 0 {
103-
cmdres := os.exec(context.cmd_to_run) or {
104-
panic(err)
105-
}
103+
cmdres := os.execute_or_panic(context.cmd_to_run)
106104
scripting.cprintln('# command: ${context.cmd_to_run:-34s} exit code: ${cmdres.exit_code:-4d} result:')
107105
println(cmdres.output)
108106
exit(cmdres.exit_code)

cmd/tools/repeat.v

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ mut:
4242
}
4343

4444
struct Aints {
45-
values []int
45+
values []int
4646
mut:
4747
imin int
4848
imax int
@@ -206,7 +206,10 @@ fn (mut context Context) run() {
206206
for i in 1 .. context.warmup + 1 {
207207
print('${context.cgoback}warming up run: ${i:4}/${context.warmup:-4} for ${cmd:-50s} took ${duration:6} ms ...')
208208
mut sw := time.new_stopwatch({})
209-
os.exec(cmd) or { continue }
209+
res := os.execute(cmd)
210+
if res.exit_code != 0 {
211+
continue
212+
}
210213
duration = int(sw.elapsed().milliseconds())
211214
}
212215
run_warmups++
@@ -225,8 +228,8 @@ fn (mut context Context) run() {
225228
eprintln('${i:10} non 0 exit code for cmd: $cmd')
226229
continue
227230
}
228-
context.results[icmd].outputs <<
229-
res.output.trim_right('\r\n').replace('\r\n', '\n').split('\n')
231+
context.results[icmd].outputs << res.output.trim_right('\r\n').replace('\r\n',
232+
'\n').split('\n')
230233
context.results[icmd].timings << duration
231234
sum += duration
232235
runs++

cmd/tools/test_if_v_test_system_works.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555

5656
fn check_ok(cmd string) string {
5757
println('> check_ok cmd: $cmd')
58-
res := os.exec(cmd) or { panic(err) }
58+
res := os.execute(cmd)
5959
if res.exit_code != 0 {
6060
eprintln('> check_ok failed.\n$res.output')
6161
exit(1)
@@ -65,7 +65,7 @@ fn check_ok(cmd string) string {
6565

6666
fn check_fail(cmd string) string {
6767
println('> check_fail cmd: $cmd')
68-
res := os.exec(cmd) or { panic(err) }
68+
res := os.execute(cmd)
6969
if res.exit_code == 0 {
7070
eprintln('> check_fail succeeded, but it should have failed.\n$res.output')
7171
exit(1)

0 commit comments

Comments
 (0)