From 5098334e65de41fefe7b57314d7472529c0b9e9a Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 20 Jul 2021 14:04:35 +0300 Subject: [PATCH] os: add execute_or_exit(cmd), use it consistently instead of execute_or_panic(cmd) --- cmd/tools/fast/fast.v | 4 ++-- cmd/tools/fast/fast_job.v | 2 +- cmd/tools/oldv.v | 2 +- cmd/tools/vcreate_test.v | 6 +++--- cmd/tools/vself.v | 2 +- cmd/tools/vup.v | 2 +- examples/dynamic_library_loading/use_test.v | 2 +- vlib/builtin/linux_bare/old/syscallwrapper_test.v | 2 +- vlib/os/os.v | 11 +++++++++++ vlib/v/builder/cc.v | 2 +- vlib/v/tests/repl/repl_test.v | 2 +- vlib/v/util/util.v | 2 +- 12 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cmd/tools/fast/fast.v b/cmd/tools/fast/fast.v index 3410de8771..ed5b17c351 100644 --- a/cmd/tools/fast/fast.v +++ b/cmd/tools/fast/fast.v @@ -107,7 +107,7 @@ fn main() { } fn exec(s string) string { - e := os.execute_or_panic(s) + e := os.execute_or_exit(s) return e.output.trim_right('\r\n') } @@ -137,7 +137,7 @@ fn measure(cmd string, description string) int { } fn measure_steps(vdir string) (int, int, int, int, int) { - resp := os.execute_or_panic('$vdir/vprod $voptions -o v.c cmd/v') + resp := os.execute_or_exit('$vdir/vprod $voptions -o v.c cmd/v') mut scan, mut parse, mut check, mut cgen, mut vlines := 0, 0, 0, 0, 0 lines := resp.output.split_into_lines() diff --git a/cmd/tools/fast/fast_job.v b/cmd/tools/fast/fast_job.v index c3eebde9b0..d4d8fc399e 100644 --- a/cmd/tools/fast/fast_job.v +++ b/cmd/tools/fast/fast_job.v @@ -32,7 +32,7 @@ fn main() { println('resp != 0, skipping') } else { os.chdir('website') - os.execute_or_panic('git checkout gh-pages') + os.execute_or_exit('git checkout gh-pages') os.cp('../index.html', 'index.html') ? os.system('git commit -am "update benchmark"') os.system('git push origin gh-pages') diff --git a/cmd/tools/oldv.v b/cmd/tools/oldv.v index 399d93679b..03eaf83ef5 100644 --- a/cmd/tools/oldv.v +++ b/cmd/tools/oldv.v @@ -165,7 +165,7 @@ fn main() { scripting.cprintln('# v commit hash: $shorter_hash | folder: $context.path_v') if context.cmd_to_run.len > 0 { scripting.cprintln_strong('# command: ${context.cmd_to_run:-34s}') - cmdres := os.execute_or_panic(context.cmd_to_run) + cmdres := os.execute_or_exit(context.cmd_to_run) if cmdres.exit_code != 0 { scripting.cprintln_strong('# exit code: ${cmdres.exit_code:-4d}') } diff --git a/cmd/tools/vcreate_test.v b/cmd/tools/vcreate_test.v index ac6f46bd37..fe829e6238 100644 --- a/cmd/tools/vcreate_test.v +++ b/cmd/tools/vcreate_test.v @@ -4,7 +4,7 @@ const test_path = 'vcreate_test' fn init_and_check() ? { vexe := @VEXE - os.execute_or_panic('$vexe init') + os.execute_or_exit('$vexe init') assert os.read_file('vcreate_test.v') ? == [ 'module main\n', @@ -58,7 +58,7 @@ fn test_v_init_in_git_dir() ? { os.rmdir_all(dir) or {} } os.chdir(dir) - os.execute_or_panic('git init .') + os.execute_or_exit('git init .') init_and_check() ? } @@ -73,7 +73,7 @@ fn test_v_init_no_overwrite_gitignore() ? { os.chdir(dir) vexe := @VEXE - os.execute_or_panic('$vexe init') + os.execute_or_exit('$vexe init') assert os.read_file('.gitignore') ? == 'blah' } diff --git a/cmd/tools/vself.v b/cmd/tools/vself.v index 2a66e4a8f2..06d218979f 100644 --- a/cmd/tools/vself.v +++ b/cmd/tools/vself.v @@ -32,7 +32,7 @@ fn main() { } fn compile(vroot string, cmd string) { - result := os.execute_or_panic(cmd) + result := os.execute_or_exit(cmd) if result.exit_code != 0 { eprintln('cannot compile to `$vroot`: \n$result.output') exit(1) diff --git a/cmd/tools/vup.v b/cmd/tools/vup.v index 7fd6d061de..c642132907 100644 --- a/cmd/tools/vup.v +++ b/cmd/tools/vup.v @@ -135,7 +135,7 @@ fn (app App) git_command(command string) { if git_result.exit_code < 0 { app.get_git() // Try it again with (maybe) git installed - os.execute_or_panic('git $command') + os.execute_or_exit('git $command') } if git_result.exit_code != 0 { eprintln(git_result.output) diff --git a/examples/dynamic_library_loading/use_test.v b/examples/dynamic_library_loading/use_test.v index 7d98d5362c..9a93d66b3c 100644 --- a/examples/dynamic_library_loading/use_test.v +++ b/examples/dynamic_library_loading/use_test.v @@ -49,7 +49,7 @@ fn test_can_compile_and_use_library_with_skip_unused() { fn v_compile(vopts string) os.Result { cmd := '"$vexe" -showcc $vopts' // dump(cmd) - res := os.execute_or_panic(cmd) + res := os.execute_or_exit(cmd) // dump(res) assert res.exit_code == 0 return res diff --git a/vlib/builtin/linux_bare/old/syscallwrapper_test.v b/vlib/builtin/linux_bare/old/syscallwrapper_test.v index 0f80b05f2f..cbaf355413 100644 --- a/vlib/builtin/linux_bare/old/syscallwrapper_test.v +++ b/vlib/builtin/linux_bare/old/syscallwrapper_test.v @@ -15,7 +15,7 @@ fn test_syscallwrappers() { os.chdir(dot_checks) checks_v := 'checks.v' assert os.exists(checks_v) - rc := os.execute_or_panic('v run $checks_v') + rc := os.execute_or_exit('v run $checks_v') assert rc.exit_code == 0 assert !rc.output.contains('V panic: An assertion failed.') assert !rc.output.contains('failed') diff --git a/vlib/os/os.v b/vlib/os/os.v index 2df87dc321..a87a25450f 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -622,6 +622,17 @@ pub fn execute_or_panic(cmd string) Result { return res } +pub fn execute_or_exit(cmd string) Result { + res := execute(cmd) + if res.exit_code != 0 { + eprintln('failed cmd: $cmd') + eprintln('failed code: $res.exit_code') + eprintln(res.output) + exit(1) + } + return res +} + // is_atty returns 1 if the `fd` file descriptor is open and refers to a terminal pub fn is_atty(fd int) int { $if windows { diff --git a/vlib/v/builder/cc.v b/vlib/v/builder/cc.v index 6f867f799a..b537e3ae76 100644 --- a/vlib/v/builder/cc.v +++ b/vlib/v/builder/cc.v @@ -524,7 +524,7 @@ fn (mut v Builder) cc() { mut ccompiler := v.pref.ccompiler if v.pref.os == .ios { ios_sdk := if v.pref.is_ios_simulator { 'iphonesimulator' } else { 'iphoneos' } - ios_sdk_path_res := os.execute_or_panic('xcrun --sdk $ios_sdk --show-sdk-path') + ios_sdk_path_res := os.execute_or_exit('xcrun --sdk $ios_sdk --show-sdk-path') mut isysroot := ios_sdk_path_res.output.replace('\n', '') arch := if v.pref.is_ios_simulator { '-arch x86_64' diff --git a/vlib/v/tests/repl/repl_test.v b/vlib/v/tests/repl/repl_test.v index ba798bd935..5b63532c8e 100644 --- a/vlib/v/tests/repl/repl_test.v +++ b/vlib/v/tests/repl/repl_test.v @@ -12,7 +12,7 @@ fn test_the_v_compiler_can_be_invoked() { println('vexecutable: $vexec') assert vexec != '' vcmd := '"$vexec" -version' - r := os.execute_or_panic(vcmd) + r := os.execute_or_exit(vcmd) assert r.exit_code == 0 // println('"$vcmd" exit_code: $r.exit_code | output: $r.output') vcmd_error := '"$vexec" nonexisting.v' diff --git a/vlib/v/util/util.v b/vlib/v/util/util.v index c983a2e16b..2f54051f6a 100644 --- a/vlib/v/util/util.v +++ b/vlib/v/util/util.v @@ -248,7 +248,7 @@ pub fn launch_tool(is_verbose bool, tool_name string, args []string) { if is_verbose { println('Compiling $tool_name with: "$compilation_command"') } - tool_compilation := os.execute_or_panic(compilation_command) + tool_compilation := os.execute_or_exit(compilation_command) if tool_compilation.exit_code != 0 { eprintln('cannot compile `$tool_source`: \n$tool_compilation.output') exit(1)