Skip to content

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
package main
import fmt
import exec

var out, errOut, code, err = exec.Run("echo", "hello")
if err != None {
    fmt.Println("error:", err)
} else {
    fmt.Println("stdout:", out)
    fmt.Println("stderr:", errOut)
    fmt.Println("exit:", code)
}


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):

RunWithOptions Command
package main
import fmt
import exec
import arrays

var a = arrays.New()
a.Push("-c")
a.Push("echo hello-$MYVAR; pwd")

var e = arrays.New()
e.Push("MYVAR=world")

var out, errOut, code, err =
  exec.RunWithOptions("/bin/sh", a, e, "/tmp")
if err != None {
    fmt.Println("error:", err)
} else {
    fmt.Println("stdout:", out)
    fmt.Println("stderr:", errOut)
    fmt.Println("exit:", code)
}


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
package main
import fmt
import exec

var out, errOut, code, err = exec.Shell("echo shell-works")
if err != None {
    fmt.Println("error:", err)
} else {
    fmt.Println("stdout:", out)
    fmt.Println("stderr:", errOut)
    fmt.Println("exit:", code)
}


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 pid
  • exec.Wait(proc map) (int, error) — returns exit code
  • exec.Kill(proc map) (bool, error)
  • exec.Write(proc map, data string) (int, error) — write to stdin
  • exec.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)

Example: Echo via cat

Echo via Cat
package main
import fmt
import exec

fmt.Println("=== exec spawn + IPC demo ===")

// Prepare a process that echoes stdin to stdout
var proc, err = exec.Command("cat", [])
if err != None {
    fmt.Println("Command error:", err)
} else {
    var pid, startErr = exec.Start(proc)
    if startErr != None {
        fmt.Println("Start error:", startErr)
    } else {
        // Write a couple of lines and close stdin to signal EOF
        var _, w1 = exec.Write(proc, "alpha\n")
        var _, w2 = exec.Write(proc, "beta\n")
        var _, cErr = exec.CloseStdin(proc)
        if w1 != None { fmt.Println("write1 error:", w1) }
        if w2 != None { fmt.Println("write2 error:", w2) }
        if cErr != None { fmt.Println("close stdin error:", cErr) }

        // Read stdout fully, then wait for exit
        var out, rErr = exec.ReadStdout(proc)
        var code, wErr = exec.Wait(proc)

        fmt.Println("pid:", pid, "exit:", code)
        if rErr == None { fmt.Println("stdout:\n" + out) } else { fmt.Println("read stdout error:", rErr) }
        if wErr != None { fmt.Println("wait error:", wErr) }
    }
}

fmt.Println("✅ spawn + IPC demo complete")