Skip to content

Commit 47b619f

Browse files
committed
Fix windows build
1 parent 22975a4 commit 47b619f

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

pkg/cmd/cmdutil.go

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"os"
1010
"os/exec"
1111
"os/signal"
12-
"runtime"
1312
"strings"
1413
"syscall"
1514

@@ -20,7 +19,6 @@ import (
2019
"github.com/tidwall/gjson"
2120
"github.com/tidwall/pretty"
2221
"github.com/urfave/cli/v3"
23-
"golang.org/x/sys/unix"
2422
"golang.org/x/term"
2523
)
2624

@@ -117,46 +115,19 @@ func streamToStdout(generateOutput func(w *os.File) error) error {
117115
}
118116

119117
func createPagerFiles() (*os.File, *os.File, bool, error) {
120-
// Windows lacks UNIX socket APIs, so we fall back to pipes there or if
121-
// socket creation fails. We prefer sockets when available because they
122-
// allow for smaller buffer sizes, preventing unnecessary data streaming
123-
// from the backend. Pipes typically have large buffers but serve as a
124-
// decent alternative when sockets aren't available.
125-
if runtime.GOOS != "windows" {
126-
pagerInput, outputFile, isSocketPair, err := createSocketPair()
127-
if err == nil {
128-
return pagerInput, outputFile, isSocketPair, nil
129-
}
118+
// We prefer sockets when available because they allow for smaller buffer
119+
// sizes, preventing unnecessary data streaming from the backend. Pipes
120+
// typically have large buffers but serve as a decent alternative when
121+
// sockets aren't available (e.g., on Windows).
122+
pagerInput, outputFile, isSocketPair, err := createSocketPair()
123+
if err == nil {
124+
return pagerInput, outputFile, isSocketPair, nil
130125
}
131126

132127
r, w, err := os.Pipe()
133128
return r, w, false, err
134129
}
135130

136-
// In order to avoid large buffers on pipes, this function create a pair of
137-
// files for reading and writing through a barely buffered socket.
138-
func createSocketPair() (*os.File, *os.File, bool, error) {
139-
fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0)
140-
if err != nil {
141-
return nil, nil, false, err
142-
}
143-
144-
parentSock, childSock := fds[0], fds[1]
145-
146-
// Use small buffer sizes so we don't ask the server for more paginated
147-
// values than we actually need.
148-
if err := unix.SetsockoptInt(parentSock, unix.SOL_SOCKET, unix.SO_SNDBUF, 128); err != nil {
149-
return nil, nil, false, err
150-
}
151-
if err := unix.SetsockoptInt(childSock, unix.SOL_SOCKET, unix.SO_RCVBUF, 128); err != nil {
152-
return nil, nil, false, err
153-
}
154-
155-
pagerInput := os.NewFile(uintptr(childSock), "child_socket")
156-
outputFile := os.NewFile(uintptr(parentSock), "parent_socket")
157-
return pagerInput, outputFile, true, nil
158-
}
159-
160131
// Start a subprocess running the user's preferred pager (or `less` if `$PAGER` is unset)
161132
func startPagerCommand(pagerInput *os.File, label string, useSocketpair bool) (*exec.Cmd, error) {
162133
pagerProgram := os.Getenv("PAGER")

pkg/cmd/cmdutil_unix.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !windows
2+
3+
package cmd
4+
5+
import (
6+
"os"
7+
8+
"golang.org/x/sys/unix"
9+
)
10+
11+
// In order to avoid large buffers on pipes, this function create a pair of
12+
// files for reading and writing through a barely buffered socket.
13+
func createSocketPair() (*os.File, *os.File, bool, error) {
14+
fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0)
15+
if err != nil {
16+
return nil, nil, false, err
17+
}
18+
19+
parentSock, childSock := fds[0], fds[1]
20+
21+
// Use small buffer sizes so we don't ask the server for more paginated
22+
// values than we actually need.
23+
if err := unix.SetsockoptInt(parentSock, unix.SOL_SOCKET, unix.SO_SNDBUF, 128); err != nil {
24+
return nil, nil, false, err
25+
}
26+
if err := unix.SetsockoptInt(childSock, unix.SOL_SOCKET, unix.SO_RCVBUF, 128); err != nil {
27+
return nil, nil, false, err
28+
}
29+
30+
pagerInput := os.NewFile(uintptr(childSock), "child_socket")
31+
outputFile := os.NewFile(uintptr(parentSock), "parent_socket")
32+
return pagerInput, outputFile, true, nil
33+
}

pkg/cmd/cmdutil_windows.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build windows
2+
3+
package cmd
4+
5+
import (
6+
"errors"
7+
"os"
8+
)
9+
10+
// createSocketPair is not supported on Windows, so we return an error
11+
// which causes createPagerFiles to fall back to using pipes.
12+
func createSocketPair() (*os.File, *os.File, bool, error) {
13+
return nil, nil, false, errors.New("socket pairs not supported on Windows")
14+
}

0 commit comments

Comments
 (0)