exec Module
The exec module allows Harneet programs to execute external commands in a controlled, cross-platform way. It returns stdout, stderr, exit code, and an error value.
Functions
Run(command, arg1, arg2, ...)
Executes the given program with arguments.
Parameters: - command: Path or name of the executable. - arg1, arg2, ...: Arguments to pass to the command.
Returns: - (string, string, int, error): (stdout, stderr, exitCode, error)
Example:
| Run Command | |
|---|---|
RunWithOptions(command, args, env, cwd)
Executes a program with explicit options.
Arguments: - command: Path or name of the program to run. - args: Array of strings passed as arguments. - env: Array of strings in KEY=VALUE format. These are appended to the current environment. - cwd: Working directory to run the command in. Use empty string to leave unchanged.
Returns: - (string, string, int, error): (stdout, stderr, exitCode, error)
Example (Unix):
Shell(command)
Executes a command string using the system shell. - On Unix-like systems, this uses /bin/sh -c. - On Windows, this uses cmd /C.
Parameters: - command: The full command string to execute.
Returns: - (string, string, int, error): (stdout, stderr, exitCode, error)
Example:
| Shell Command | |
|---|---|
Process Spawning and IPC
The exec module also supports spawning long-lived processes and interacting with them via stdin/stdout/stderr.
Functions
exec.Command(cmd string, args array) (map, error)exec.Start(proc map) (int, error)— returns pidexec.Wait(proc map) (int, error)— returns exit codeexec.Kill(proc map) (bool, error)exec.Write(proc map, data string) (int, error)— write to stdinexec.CloseStdin(proc map) (bool, error)— close stdin (signal EOF)exec.ReadStdout(proc map) (string, error)— read all stdout (until EOF)exec.ReadStderr(proc map) (string, error)— read all stderr (until EOF)