2-1. Command Execution
Let's make a low-leveled container runtime!
go
package main
import (
"log"
"os"
"os/exec"
"github.com/k1LoW/errors"
"golang.org/x/sys/unix"
)
func main() {
if err := run(os.Args[1:]); err != nil {
log.Fatalln(errors.StackTraces(err))
}
}
func run(command []string) error {
path, err := exec.LookPath(command[0])
if err != nil {
return errors.WithStack(err)
}
if err := unix.Exec(path, command, os.Environ()); err != nil {
return errors.WithStack(err)
}
return nil
}go
package main
import (
"log"
"os"
"os/exec"
"github.com/k1LoW/errors"
"golang.org/x/sys/unix"
)
func main() {
if err := run(os.Args[1:]); err != nil {
log.Fatalln(errors.StackTraces(err))
switch os.Args[1] {
case "run":
if err := run(os.Args[2:]); err != nil {
log.Fatalln(errors.StackTraces(err))
}
default:
log.Fatalf("unknown command: %s", os.Args[1])
}
}
func run(command []string) error {
path, err := exec.LookPath(command[0])
if err != nil {
return errors.WithStack(err)
}
if err := unix.Exec(path, command, os.Environ()); err != nil {
return errors.WithStack(err)
}
return nil
}