1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: deprecate os.exec (returning ?os.Result), in favour of os.execute, which returns os.Result (#8974)

This commit is contained in:
Delyan Angelov
2021-03-08 20:52:13 +02:00
committed by GitHub
parent 10c9f61d61
commit d7049ae2da
52 changed files with 423 additions and 344 deletions

View File

@@ -596,3 +596,22 @@ pub mut:
version string
machine string
}
[deprecated: 'use os.execute or os.execute_or_panic instead']
pub fn exec(cmd string) ?Result {
res := execute(cmd)
if res.exit_code < 0 {
return error_with_code(res.output, -1)
}
return res
}
pub fn execute_or_panic(cmd string) Result {
res := execute(cmd)
if res.exit_code != 0 {
eprintln('failed cmd: $cmd')
eprintln('failed code: $res.exit_code')
panic(res.output)
}
return res
}

View File

@@ -156,15 +156,18 @@ pub fn mkdir(path string) ?bool {
return true
}
// exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(cmd string) ?Result {
// execute starts the specified command, waits for it to complete, and returns its output.
pub fn execute(cmd string) Result {
// if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
// return error(';, &&, || and \\n are not allowed in shell commands')
// return Result{ exit_code: -1, output: ';, &&, || and \\n are not allowed in shell commands' }
// }
pcmd := '$cmd 2>&1'
f := vpopen(pcmd)
if isnil(f) {
return error('exec("$cmd") failed')
return Result{
exit_code: -1
output: 'exec("$cmd") failed'
}
}
buf := [4096]byte{}
mut res := strings.new_builder(1024)
@@ -177,9 +180,6 @@ pub fn exec(cmd string) ?Result {
soutput := res.str()
// res.free()
exit_code := vpclose(f)
if exit_code == 127 {
return error_with_code(soutput, 127)
}
return Result{
exit_code: exit_code
output: soutput

View File

@@ -221,10 +221,13 @@ pub fn get_error_msg(code int) string {
return unsafe { string_from_wide(ptr_text) }
}
// exec starts the specified command, waits for it to complete, and returns its output.
pub fn exec(cmd string) ?Result {
// execute starts the specified command, waits for it to complete, and returns its output.
pub fn execute(cmd string) Result {
if cmd.contains(';') || cmd.contains('&&') || cmd.contains('||') || cmd.contains('\n') {
return error(';, &&, || and \\n are not allowed in shell commands')
return Result{
exit_code: -1
output: ';, &&, || and \\n are not allowed in shell commands'
}
}
mut child_stdin := &u32(0)
mut child_stdout_read := &u32(0)
@@ -237,14 +240,20 @@ pub fn exec(cmd string) ?Result {
if !create_pipe_ok {
error_num := int(C.GetLastError())
error_msg := get_error_msg(error_num)
return error_with_code('exec failed (CreatePipe): $error_msg', error_num)
return Result{
exit_code: error_num
output: 'exec failed (CreatePipe): $error_msg'
}
}
set_handle_info_ok := C.SetHandleInformation(child_stdout_read, C.HANDLE_FLAG_INHERIT,
0)
if !set_handle_info_ok {
error_num := int(C.GetLastError())
error_msg := get_error_msg(error_num)
return error_with_code('exec failed (SetHandleInformation): $error_msg', error_num)
return Result{
exit_code: error_num
output: 'exec failed (SetHandleInformation): $error_msg'
}
}
proc_info := ProcessInformation{}
start_info := StartupInfo{
@@ -264,8 +273,10 @@ pub fn exec(cmd string) ?Result {
if !create_process_ok {
error_num := int(C.GetLastError())
error_msg := get_error_msg(error_num)
return error_with_code('exec failed (CreateProcess) with code $error_num: $error_msg cmd: $cmd',
error_num)
return Result{
exit_code: error_num
output: 'exec failed (CreateProcess) with code $error_num: $error_msg cmd: $cmd'
}
}
C.CloseHandle(child_stdin)
C.CloseHandle(child_stdout_write)