I'm trying to use os.StartProcess
to start a process with arguments on a FreeBSD machine. After trying several different ways of adding the correct arguments and always getting errors I have set up this simple proof of concept.
package main
import (
"log"
"os"
)
func main() {
command := "/usr/local/sbin/pkg"
args := []string{"install"}
procAttr := new(os.ProcAttr)
procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
if process, err := os.StartProcess(command, args, procAttr); err != nil {
log.Println(err.Error())
} else {
log.Printf("%d", process.Pid)
}
}
The idea here is that if the system starts just pkg
it will complain about missing arguments, but if it starts pkg install
it will suggest the help for the install
command. See below:
pkg: not enough arguments
Usage: pkg [-v] [-d] [-l] [-N] [-j <jail name or id>|-c <chroot path>|-r <rootdir>] [-C <configuration file>] [-R <repo config dir>] [-o var=value] [-4|-6] <command> [<args>]
For more information on available commands and options see 'pkg help'.
Or
Usage: pkg install [-AfInFMqRUy] [-r reponame] [-Cgix] <pkg-name> ...
For more information see 'pkg help install'.
When I use the go code above it will complaint about missing arguments, so why is my argument 'install' not added to the command, and how to correct my code?