Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,34 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
|
||||
// basic example which shows how to use the Command function
|
||||
|
||||
fn exec(path string) string {
|
||||
mut out := ''
|
||||
mut line := ''
|
||||
mut cmd := os.Command{
|
||||
path: path
|
||||
}
|
||||
cmd.start() or { panic(err) }
|
||||
|
||||
for {
|
||||
line = cmd.read_line()
|
||||
println(line)
|
||||
out += line
|
||||
if cmd.eof {
|
||||
return out
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut out := ''
|
||||
exec("bash -c 'find /tmp/'")
|
||||
out = exec('echo to stdout')
|
||||
out = exec('echo to stderr 1>&2')
|
||||
println("'${out}'")
|
||||
// THIS DOES NOT WORK, is error, it goes to stderror of the command I run
|
||||
assert out == 'to stderr'
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import os
|
||||
|
||||
// NOTE: `execve` executes a new child process, in place of the current process.
|
||||
// Therefore, only the topmost example will be executed when it's not commented out.
|
||||
fn main() {
|
||||
// Passes only an array of args.
|
||||
os.execve(os.find_abs_path_of_executable('ls')!, ['-lh', '-s'], [])!
|
||||
|
||||
// Considers args that would need to be passed within quotes. E.g.: `bash -c "ls -lh"`.
|
||||
os.execve(os.find_abs_path_of_executable('bash')!, ['-c', 'ls -lah -s'], [])!
|
||||
|
||||
// Passes an environment variable that affects the commands output.
|
||||
os.execve(os.find_abs_path_of_executable('man')!, ['true'], ['MANWIDTH=60'])!
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import os
|
||||
|
||||
os.find_abs_path_of_executable('inotifywait') or {
|
||||
eprintln('This program uses the inotifywait executable, which is missing.')
|
||||
exit(1)
|
||||
}
|
||||
mut cmd := os.start_new_command('inotifywait -q -r -m -e move,modify,create,delete .')!
|
||||
defer { cmd.close() or {} }
|
||||
for !cmd.eof {
|
||||
line := cmd.read_line()
|
||||
if line == '' && cmd.eof {
|
||||
continue
|
||||
}
|
||||
eprintln('> notification: ${line}')
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
module main
|
||||
|
||||
import os
|
||||
|
||||
// a test where we execute a bash script but work around where we put script in bash inside bash
|
||||
|
||||
fn exec(path string, redirect bool) {
|
||||
mut line := ''
|
||||
mut line_err := ''
|
||||
mut cmd := os.new_process('/bin/bash')
|
||||
|
||||
if redirect {
|
||||
cmd.set_args(['-c', '/bin/bash /tmp/test.sh 2>&1'])
|
||||
} else {
|
||||
cmd.set_args([path])
|
||||
}
|
||||
|
||||
cmd.set_redirect_stdio()
|
||||
cmd.run()
|
||||
for cmd.is_alive() {
|
||||
line = cmd.stdout_read()
|
||||
println('STDOUT: ${line}')
|
||||
|
||||
if !redirect {
|
||||
line_err = cmd.stderr_read()
|
||||
println('STDERR: ${line_err}')
|
||||
}
|
||||
}
|
||||
if cmd.code > 0 {
|
||||
println('ERROR:')
|
||||
println(cmd)
|
||||
// println(cmd.stderr_read())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
script := '
|
||||
echo line 1
|
||||
#will use some stderr now
|
||||
echo redirect 1 to 2 1>&2
|
||||
echo line 3
|
||||
'
|
||||
|
||||
os.write_file('/tmp/test.sh', script) or { panic(err) }
|
||||
// os.chmod("/tmp/test.sh",0o700) //make executable
|
||||
|
||||
// this will work because stderr/stdout are smaller than 4096 chars, once larger there can be deadlocks
|
||||
// in other words this can never work reliably without being able to check if there is data on stderr or stdout
|
||||
exec('/tmp/test.sh', false)
|
||||
|
||||
// this will always work
|
||||
exec('/tmp/test.sh', true)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
import time
|
||||
import term
|
||||
|
||||
python_exe := os.find_abs_path_of_executable('python') or {
|
||||
eprintln('This example needs a python executable in your PATH. Please install Python to see it in action.')
|
||||
exit(1)
|
||||
}
|
||||
mut p := os.new_process(python_exe)
|
||||
defer {
|
||||
dump(p.code)
|
||||
p.close()
|
||||
p.wait()
|
||||
}
|
||||
// The Python flags here, are needed to reduce clutter and buffering.
|
||||
// See https://docs.python.org/3/using/cmdline.html
|
||||
p.set_args(['-i', '-q', '-u'])
|
||||
p.set_redirect_stdio()
|
||||
p.run()
|
||||
dump(p.pid)
|
||||
println('This is a simple V wrapper/shell for the Python interpreter.')
|
||||
println('Try typing some python code here, or type `bye` to end your session:')
|
||||
for p.is_alive() {
|
||||
// check if there is any input from the user (it does not block, if there is not):
|
||||
if os.fd_is_pending(0) {
|
||||
cmd := os.get_raw_line()
|
||||
if cmd.len == 0 {
|
||||
println('closed stdin detected, perhaps due to Ctrl-D...exiting')
|
||||
break
|
||||
}
|
||||
if cmd.trim_space() == 'bye' {
|
||||
println('Goodbye...')
|
||||
break
|
||||
}
|
||||
p.stdin_write(cmd)
|
||||
}
|
||||
if oline := p.pipe_read(.stdout) {
|
||||
print(term.bright_yellow('python stdout: ') + term.bold(oline))
|
||||
}
|
||||
if eline := p.pipe_read(.stderr) {
|
||||
eprint(term.red('python stderr: ') + eline)
|
||||
}
|
||||
time.sleep(20 * time.millisecond)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
module main
|
||||
|
||||
// This example shows how to communicate with a child process (`bash` in this case), by sending
|
||||
// commands to its stdin pipe, and reading responses from its stdout and stderr pipes.
|
||||
// Note, you can use `if p.is_pending(.stdout) {` and `if p.is_pending(.stderr) {`, to check if
|
||||
// there is available data in the pipes, without having to block in your main loop, if the data
|
||||
// is missing or just not available yet.
|
||||
import os
|
||||
import time
|
||||
|
||||
const tmp_folder = os.join_path(os.temp_dir(), 'process_folder')
|
||||
|
||||
const max_txt_files = 20
|
||||
|
||||
fn exec(cmd string) (string, int, string) {
|
||||
mut out := []string{}
|
||||
mut er := []string{}
|
||||
mut rc := 0
|
||||
|
||||
mut p := os.new_process('/bin/bash')
|
||||
p.set_redirect_stdio()
|
||||
p.run()
|
||||
|
||||
p.stdin_write('echo "START " && sleep 0.1 && ${cmd};\n')
|
||||
p.stdin_write('ECODE=\$?;\n')
|
||||
p.stdin_write('sleep 0.1;\n')
|
||||
p.stdin_write('exit \$ECODE;\n')
|
||||
|
||||
// Note, that you can also ensure that `bash` will exit, when the command finishes,
|
||||
// by closing its stdin pipe. In the above example, that is not needed however, since
|
||||
// the last `exit` command, will make it quit as well.
|
||||
// os.fd_close(p.stdio_fd[0])
|
||||
|
||||
for p.is_alive() {
|
||||
if data := p.pipe_read(.stderr) {
|
||||
eprintln('p.pipe_read .stderr, len: ${data.len:4} | data: `${data#[0..10]}`...')
|
||||
er << data
|
||||
}
|
||||
if data := p.pipe_read(.stdout) {
|
||||
eprintln('p.pipe_read .stdout, len: ${data.len:4} | data: `${data#[0..10]}`...')
|
||||
out << data
|
||||
}
|
||||
// avoid a busy loop, by sleeping a bit between each iteration
|
||||
time.sleep(2 * time.millisecond)
|
||||
}
|
||||
|
||||
// the process finished, slurp all the remaining data in the pipes:
|
||||
out << p.stdout_slurp()
|
||||
er << p.stderr_slurp()
|
||||
p.close()
|
||||
p.wait()
|
||||
|
||||
if p.code > 0 {
|
||||
eprintln('----------------------------------------------------------')
|
||||
eprintln('COMMAND: ${cmd}')
|
||||
eprintln('STDOUT:\n${out}')
|
||||
eprintln('STDERR:\n${er}')
|
||||
eprintln('----------------------------------------------------------')
|
||||
rc = 1
|
||||
}
|
||||
|
||||
return out.join(''), rc, er.join('')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut out := ''
|
||||
mut er := ''
|
||||
mut ecode := 0
|
||||
|
||||
// prepare some files in a temporary folder
|
||||
defer {
|
||||
os.rmdir_all(tmp_folder) or {}
|
||||
}
|
||||
os.mkdir_all(tmp_folder) or {}
|
||||
for i in 0 .. max_txt_files {
|
||||
os.write_file(os.join_path(tmp_folder, '${i}.txt'), '${i}\n${i}\n')!
|
||||
}
|
||||
|
||||
out, ecode, er =
|
||||
exec("find ${os.quoted_path(tmp_folder)} ; sleep 0.1; find ${os.quoted_path(tmp_folder)} ; echo '******'")
|
||||
assert out.ends_with('******\n')
|
||||
assert er == ''
|
||||
|
||||
out, ecode, er = exec('echo to stdout')
|
||||
assert out.contains('to stdout')
|
||||
assert er == ''
|
||||
|
||||
out, ecode, er = exec('echo to stderr 1>&2')
|
||||
assert out.starts_with('START')
|
||||
assert er.contains('to stderr')
|
||||
|
||||
out, ecode, er = exec('ls /sssss')
|
||||
assert out.starts_with('START')
|
||||
assert er != ''
|
||||
assert ecode > 0 // THIS STILL GIVES AN ERROR !
|
||||
|
||||
println('test ok stderr & stdout is indeed redirected, ecode: ${ecode}')
|
||||
}
|
||||
Reference in new issue
Block a user