|
| 1 | +<<<<<<< HEAD |
1 | 2 | //go:build !windows |
2 | 3 |
|
3 | 4 | package cmd |
@@ -31,3 +32,122 @@ func createSocketPair() (*os.File, *os.File, bool, error) { |
31 | 32 | outputFile := os.NewFile(uintptr(parentSock), "parent_socket") |
32 | 33 | return pagerInput, outputFile, true, nil |
33 | 34 | } |
| 35 | +||||||| parent of fe526a1 (fix(cli): fix compilation on Windows) |
| 36 | +======= |
| 37 | +//go:build !windows |
| 38 | + |
| 39 | +package cmd |
| 40 | + |
| 41 | +import ( |
| 42 | + "fmt" |
| 43 | + "os" |
| 44 | + "os/exec" |
| 45 | + "strings" |
| 46 | + "syscall" |
| 47 | + |
| 48 | + "golang.org/x/sys/unix" |
| 49 | +) |
| 50 | + |
| 51 | +func streamOutputOSSpecific(label string, generateOutput func(w *os.File) error) error { |
| 52 | + // Try to use socket pair for better buffer control |
| 53 | + pagerInput, pid, err := openSocketPairPager(label) |
| 54 | + if err != nil || pagerInput == nil { |
| 55 | + // Fall back to pipe if socket setup fails |
| 56 | + return streamToPagerWithPipe(label, generateOutput) |
| 57 | + } |
| 58 | + defer pagerInput.Close() |
| 59 | + |
| 60 | + // If we would be streaming to a terminal and aren't forcing color one way |
| 61 | + // or the other, we should configure things to use color so the pager gets |
| 62 | + // colorized input. |
| 63 | + if isTerminal(os.Stdout) && os.Getenv("FORCE_COLOR") == "" { |
| 64 | + os.Setenv("FORCE_COLOR", "1") |
| 65 | + } |
| 66 | + |
| 67 | + // If the pager exits before reading all input, then generateOutput() will |
| 68 | + // produce a broken pipe error, which is fine and we don't want to propagate it. |
| 69 | + if err := generateOutput(pagerInput); err != nil && |
| 70 | + !strings.Contains(err.Error(), "broken pipe") { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + // Close the file NOW before we wait for the child process to terminate. |
| 75 | + // This way, the child will receive the end-of-file signal and know that |
| 76 | + // there is no more input. Otherwise the child process may block |
| 77 | + // indefinitely waiting for another line (this can happen when streaming |
| 78 | + // less than a screenful of data to a pager). |
| 79 | + pagerInput.Close() |
| 80 | + |
| 81 | + // Wait for child process to exit |
| 82 | + var wstatus syscall.WaitStatus |
| 83 | + _, err = syscall.Wait4(pid, &wstatus, 0, nil) |
| 84 | + if wstatus.ExitStatus() != 0 { |
| 85 | + return fmt.Errorf("Pager exited with non-zero exit status: %d", wstatus.ExitStatus()) |
| 86 | + } |
| 87 | + return err |
| 88 | +} |
| 89 | + |
| 90 | +func openSocketPairPager(label string) (*os.File, int, error) { |
| 91 | + fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0) |
| 92 | + if err != nil { |
| 93 | + return nil, 0, err |
| 94 | + } |
| 95 | + |
| 96 | + // The child file descriptor will be sent to the child process through |
| 97 | + // ProcAttr and ForkExec(), while the parent process will always close the |
| 98 | + // child file descriptor. |
| 99 | + // The parent file descriptor will be wrapped in an os.File wrapper and |
| 100 | + // returned from this function, or closed if something goes wrong. |
| 101 | + parentFd, childFd := fds[0], fds[1] |
| 102 | + defer unix.Close(childFd) |
| 103 | + |
| 104 | + // Use small buffer sizes so we don't ask the server for more paginated |
| 105 | + // values than we actually need. |
| 106 | + if err := unix.SetsockoptInt(parentFd, unix.SOL_SOCKET, unix.SO_SNDBUF, 128); err != nil { |
| 107 | + unix.Close(parentFd) |
| 108 | + return nil, 0, err |
| 109 | + } |
| 110 | + if err := unix.SetsockoptInt(childFd, unix.SOL_SOCKET, unix.SO_RCVBUF, 128); err != nil { |
| 111 | + unix.Close(parentFd) |
| 112 | + return nil, 0, err |
| 113 | + } |
| 114 | + |
| 115 | + // Set CLOEXEC on the parent file descriptor so it doesn't leak to child |
| 116 | + syscall.CloseOnExec(parentFd) |
| 117 | + |
| 118 | + parentConn := os.NewFile(uintptr(parentFd), "parent-socket") |
| 119 | + |
| 120 | + pagerProgram := os.Getenv("PAGER") |
| 121 | + if pagerProgram == "" { |
| 122 | + pagerProgram = "less" |
| 123 | + } |
| 124 | + |
| 125 | + pagerPath, err := exec.LookPath(pagerProgram) |
| 126 | + if err != nil { |
| 127 | + unix.Close(parentFd) |
| 128 | + return nil, 0, err |
| 129 | + } |
| 130 | + |
| 131 | + env := os.Environ() |
| 132 | + env = append(env, "LESS=-r -P "+label) |
| 133 | + env = append(env, "MORE=-r -P "+label) |
| 134 | + |
| 135 | + procAttr := &syscall.ProcAttr{ |
| 136 | + Dir: "", |
| 137 | + Env: env, |
| 138 | + Files: []uintptr{ |
| 139 | + uintptr(childFd), // stdin (fd 0) |
| 140 | + uintptr(syscall.Stdout), // stdout (fd 1) |
| 141 | + uintptr(syscall.Stderr), // stderr (fd 2) |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + pid, err := syscall.ForkExec(pagerPath, []string{pagerProgram}, procAttr) |
| 146 | + if err != nil { |
| 147 | + unix.Close(parentFd) |
| 148 | + return nil, 0, err |
| 149 | + } |
| 150 | + |
| 151 | + return parentConn, pid, nil |
| 152 | +} |
| 153 | +>>>>>>> fe526a1 (fix(cli): fix compilation on Windows) |
0 commit comments