The standard librarySlide 60 of 86
60execThe standard library
Running other programs
Three calls run another program, and none goes through a shell: the arguments are a vector, not a line of text, so nothing in one can be read as a command. Each takes the command, its arguments, and the folder to run it in — "" for the current one.
exec(command, arguments, directory)waits for the program and answers with everything it wrote, standard output and standard error together, as written and untrimmed. An exit status other than zero is aResult.Error.execWith(command, arguments, directory, environment)isexecwith ahive.map.Map<Str, Str>of variables set on top of this program's own environment — an overlay, never a replacement, soPATHandHOMEare still there, and an empty map is exactlyexec.attach(command, arguments, directory)hands the command this program's own terminal and answers with its exit status, capturing nothing: the command talks to whoever started this program. A command that could not start answers-1.
A failure is a hive.term.ExecError carrying the command; the code it exited with, -1 when it could not start at all, as for a folder that does not exist; the output it wrote before failing; and a message.
Running another program
proc main(): void {
result := hive.term.exec("hostname", [], "")
if result is Result.Ok(output) {
echo "this machine is called: {output}"
} else if result is Result.Error(why) {
echo "could not run hostname: {why.message}"
}
}Running one with an environment
proc main(): void {
mut hive.map.Map<Str, Str> asked = hive.map.new()
hive.map.set(asked, "GOOS", "linux")
hive.map.set(asked, "GOARCH", "arm64")
// An overlay, not a replacement: these two on top of everything this
// program was started with, so `go` is still found on the PATH.
result := hive.term.execWith("go", ["env", "GOOS", "GOARCH"], "", asked)
if result is Result.Ok(output) {
echo output
} else if result is Result.Error(why) {
echo "no Go on this machine: {why.message}"
}
}