|
9 | 9 |
|
10 | 10 | "github.com/cli/cli/api" |
11 | 11 | "github.com/cli/cli/internal/ghinstance" |
| 12 | + "github.com/cli/cli/internal/httpunix" |
12 | 13 | "github.com/cli/cli/pkg/iostreams" |
13 | 14 | ) |
14 | 15 |
|
@@ -57,8 +58,31 @@ type configGetter interface { |
57 | 58 | } |
58 | 59 |
|
59 | 60 | // generic authenticated HTTP client for commands |
60 | | -func NewHTTPClient(io *iostreams.IOStreams, cfg configGetter, appVersion string, setAccept bool) *http.Client { |
| 61 | +func NewHTTPClient(io *iostreams.IOStreams, cfg configGetter, appVersion string, setAccept bool) (*http.Client, error) { |
61 | 62 | var opts []api.ClientOption |
| 63 | + |
| 64 | + // We need to check and potentially add the unix socket roundtripper option |
| 65 | + // before adding any other options, since if we are going to use the unix |
| 66 | + // socket transport, it needs to form the base of the transport chain |
| 67 | + // represented by invocations of opts... |
| 68 | + // |
| 69 | + // Another approach might be to change the signature of api.NewHTTPClient to |
| 70 | + // take an explicit base http.RoundTripper as its first parameter (it |
| 71 | + // currently defaults internally to http.DefaultTransport), or add another |
| 72 | + // variant like api.NewHTTPClientWithBaseRoundTripper. But, the only caller |
| 73 | + // which would use that non-default behavior is right here, and it doesn't |
| 74 | + // seem worth the cognitive overhead everywhere else just to serve this one |
| 75 | + // use case. |
| 76 | + unixSocket, err := cfg.Get("", "http_unix_socket") |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + if unixSocket != "" { |
| 81 | + opts = append(opts, api.ClientOption(func(http.RoundTripper) http.RoundTripper { |
| 82 | + return httpunix.NewRoundTripper(unixSocket) |
| 83 | + })) |
| 84 | + } |
| 85 | + |
62 | 86 | if verbose := os.Getenv("DEBUG"); verbose != "" { |
63 | 87 | logTraffic := strings.Contains(verbose, "api") |
64 | 88 | opts = append(opts, api.VerboseLog(io.ErrOut, logTraffic, io.IsStderrTTY())) |
@@ -98,7 +122,7 @@ func NewHTTPClient(io *iostreams.IOStreams, cfg configGetter, appVersion string, |
98 | 122 | ) |
99 | 123 | } |
100 | 124 |
|
101 | | - return api.NewHTTPClient(opts...) |
| 125 | + return api.NewHTTPClient(opts...), nil |
102 | 126 | } |
103 | 127 |
|
104 | 128 | func getHost(r *http.Request) string { |
|
0 commit comments