| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | package instance |
| 7 | |
| 8 | import ( |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "syscall" |
| 13 | |
| 14 | "github.com/mattn/go-isatty" |
| 15 | "github.com/sirupsen/logrus" |
| 16 | |
| 17 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 18 | ) |
| 19 | |
| 20 | func execHostAgentForeground(limactl string, haCmd *exec.Cmd) error { |
| 21 | haStdoutW, ok := haCmd.Stdout.(*os.File) |
| 22 | if !ok { |
| 23 | return fmt.Errorf("expected haCmd.Stdout to be *os.File, got %T", haCmd.Stdout) |
| 24 | } |
| 25 | haStderrW, ok := haCmd.Stderr.(*os.File) |
| 26 | if !ok { |
| 27 | return fmt.Errorf("expected haCmd.Stderr to be *os.File, got %T", haCmd.Stderr) |
| 28 | } |
| 29 | logrus.Info("Running the host agent in the foreground") |
| 30 | if isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) { |
| 31 | |
| 32 | message := "This log file is not used because `limactl start` was launched in the terminal with the `--foreground` option." |
| 33 | if _, err := haStdoutW.WriteString(message); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if _, err := haStderrW.WriteString(message); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | } else { |
| 40 | if err := osutil.Dup2(int(haStdoutW.Fd()), syscall.Stdout); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | if err := osutil.Dup2(int(haStderrW.Fd()), syscall.Stderr); err != nil { |
| 44 | return err |
| 45 | } |
| 46 | } |
| 47 | return syscall.Exec(limactl, haCmd.Args, haCmd.Environ()) |
| 48 | } |