5 Commits

Author SHA1 Message Date
ge
8286af90eb mod: bump version 2026-01-29 04:10:09 +03:00
ge
14864ba992 examples: Remove io.string_reader 2026-01-29 04:09:33 +03:00
ge
c62722cba9 readme: upd 2026-01-29 04:08:44 +03:00
ge
7acab2165e cmd: Use absolute Command.dir 2026-01-29 03:40:42 +03:00
ge
8e7d757b3b breaking: revome ByteBuffer from API 2026-01-29 03:34:10 +03:00
7 changed files with 30 additions and 36 deletions

View File

@@ -9,7 +9,7 @@ but I don't like any of them. So let's overview.
* `os.execvp()`, `os.execve()` — cross-platform versions of the C functions of the same name. * `os.execvp()`, `os.execve()` — cross-platform versions of the C functions of the same name.
* `os.execute()`, `os.execute_opt()`, `os.execute_or_exit()`, `os.execute_or_panic()` — starts and waits for a command to completed. Under the hood, they perform a dirty hack by calling shell with stream redirection `'exec 2>&1;${cmd}'`. * `os.execute()`, `os.execute_opt()`, `os.execute_or_exit()`, `os.execute_or_panic()`, `os.raw_execute()` — starts and waits for a command to completed. Under the hood, they perform a dirty hack by calling shell with stream redirection `'exec 2>&1;${cmd}'`.
* `util.execute_with_timeout()` (from `os.util`) — just an `os.execute()` wrapper. * `util.execute_with_timeout()` (from `os.util`) — just an `os.execute()` wrapper.
@@ -55,7 +55,6 @@ directory. **Examples are very important**.
- [x] Basic implementation. - [x] Basic implementation.
- [x] Contexts support for creating cancelable commands, commands with timeouts, etc. - [x] Contexts support for creating cancelable commands, commands with timeouts, etc.
- [ ] Process groups support, pgkill().
- [ ] Better error handling and more tests... - [ ] Better error handling and more tests...
Send pull requests for additional features/bugfixes. Send pull requests for additional features/bugfixes.

View File

@@ -1,26 +0,0 @@
module runcmd
import io
// buffer creates simple bytes buffer that can be read through `io.Reader` interface.
pub fn buffer(data []u8) ByteBuffer {
return ByteBuffer{
bytes: data
}
}
struct ByteBuffer {
bytes []u8
mut:
pos int
}
// read reads `buf.len` bytes from internal bytes buffer and returns number of bytes read.
pub fn (mut b ByteBuffer) read(mut buf []u8) !int {
if b.pos >= b.bytes.len {
return io.Eof{}
}
n := copy(mut buf, b.bytes[b.pos..])
b.pos += n
return n
}

2
cmd.v
View File

@@ -268,7 +268,7 @@ pub fn (mut c Command) start() !int {
path: path path: path
argv: c.args argv: c.args
env: if c.env.len == 0 { os.environ() } else { c.env } env: if c.env.len == 0 { os.environ() } else { c.env }
dir: c.dir dir: os.abs_path(c.dir)
post_fork: [parent_pipes_hook] post_fork: [parent_pipes_hook]
pre_exec: pre_exec_hooks pre_exec: pre_exec_hooks
} }

View File

@@ -1,4 +1,4 @@
import io.string_reader import io
import rand import rand
import runcmd import runcmd
import time import time
@@ -20,7 +20,7 @@ fn main() {
mut child_stdout := cmd.stdout()! mut child_stdout := cmd.stdout()!
// Prepare reader to store command output. // Prepare reader to store command output.
mut output := string_reader.StringReader.new(reader: child_stdout) mut output := io.new_buffered_reader(reader: child_stdout)
// Start stdout reading in a coroutine. // Start stdout reading in a coroutine.
// //
@@ -56,4 +56,6 @@ fn main() {
// wait() will close the child stdout file desciptor by itself. // wait() will close the child stdout file desciptor by itself.
cmd.wait()! cmd.wait()!
println('Child state: ${cmd.state}')
} }

View File

@@ -1,4 +1,4 @@
import io.string_reader import io
import runcmd import runcmd
fn main() { fn main() {
@@ -14,7 +14,7 @@ fn main() {
// Setup StringReader with stdout input. Note the cmd.stdout()! call, it // Setup StringReader with stdout input. Note the cmd.stdout()! call, it
// returns the io.Reader interface and reads child process stdout file descriptor. // returns the io.Reader interface and reads child process stdout file descriptor.
mut reader := string_reader.StringReader.new(reader: cmd.stdout()!) mut reader := io.new_buffered_reader(reader: cmd.stdout()!)
// Read sdtout line by line until EOF. // Read sdtout line by line until EOF.
for { for {

View File

@@ -1,6 +1,23 @@
import io
import strings import strings
import runcmd import runcmd
struct ByteBuffer {
bytes []u8
mut:
pos int
}
// read reads `buf.len` bytes from internal bytes buffer and returns number of bytes read.
pub fn (mut b ByteBuffer) read(mut buf []u8) !int {
if b.pos >= b.bytes.len {
return io.Eof{}
}
n := copy(mut buf, b.bytes[b.pos..])
b.pos += n
return n
}
fn main() { fn main() {
input := 'Hello from parent process!' input := 'Hello from parent process!'
@@ -8,8 +25,10 @@ fn main() {
// * `reader` reads input from the parent process; it will be copied to the // * `reader` reads input from the parent process; it will be copied to the
// standard input of the child process. // standard input of the child process.
// * `writer` accepts data from the child process; it will be copied from the // * `writer` accepts data from the child process; it will be copied from the
// standard output of the child process. // standard output of the child process. This is optinal.
mut reader := runcmd.buffer(input.bytes()) mut reader := ByteBuffer{
bytes: input.bytes()
}
mut writer := strings.new_builder(4096) mut writer := strings.new_builder(4096)
// Prepare the command. // Prepare the command.

2
v.mod
View File

@@ -1,7 +1,7 @@
Module { Module {
name: 'runcmd' name: 'runcmd'
description: 'Run external commands' description: 'Run external commands'
version: '0.3.0' version: '0.4.0'
license: 'Unlicense' license: 'Unlicense'
repo_url: 'https://github.com/gechandesu/runcmd' repo_url: 'https://github.com/gechandesu/runcmd'
dependencies: [] dependencies: []