Skip to content

Commit 86e702b

Browse files
committed
Make header-command newline-delimited
Instead of JSON.
1 parent 4a439ce commit 86e702b

File tree

5 files changed

+31
-23
lines changed

5 files changed

+31
-23
lines changed

cli/root.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cli
22

33
import (
4+
"bufio"
5+
"bytes"
46
"context"
57
"encoding/base64"
68
"encoding/json"
@@ -361,7 +363,7 @@ func (r *RootCmd) Command(subcommands []*clibase.Cmd) (*clibase.Cmd, error) {
361363
{
362364
Flag: varHeaderCommand,
363365
Env: "CODER_HEADER_COMMAND",
364-
Description: "An external process that outputs JSON-encoded key-value pairs to be used as additional HTTP headers added to all requests.",
366+
Description: "An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line.",
365367
Value: clibase.StringOf(&r.headerCommand),
366368
Group: globalGroup,
367369
},
@@ -605,36 +607,39 @@ func (r *RootCmd) setClient(ctx context.Context, client *codersdk.Client, server
605607
transport: http.DefaultTransport,
606608
header: http.Header{},
607609
}
608-
for _, header := range r.header {
609-
parts := strings.SplitN(header, "=", 2)
610-
if len(parts) < 2 {
611-
return xerrors.Errorf("split header %q had less than two parts", header)
612-
}
613-
transport.header.Add(parts[0], parts[1])
614-
}
610+
headers := r.header
615611
if r.headerCommand != "" {
616612
shell := "sh"
617613
caller := "-c"
618614
if runtime.GOOS == "windows" {
619615
shell = "cmd.exe"
620616
caller = "/c"
621617
}
618+
var outBuf bytes.Buffer
622619
// #nosec
623620
cmd := exec.CommandContext(ctx, shell, caller, r.headerCommand)
624621
cmd.Env = append(os.Environ(), "CODER_URL="+serverURL.String())
625-
out, err := cmd.Output()
622+
cmd.Stdout = &outBuf
623+
cmd.Stderr = io.Discard
624+
err := cmd.Run()
626625
if err != nil {
627-
return xerrors.Errorf("failed to run %v (out: %q): %w", cmd.Args, out, err)
626+
return xerrors.Errorf("failed to run %v: %w", cmd.Args, err)
628627
}
629-
var headers map[string]string
630-
err = json.Unmarshal(out, &headers)
631-
if err != nil {
632-
return xerrors.Errorf("failed to parse json from %v (out: %q): %w", cmd.Args, out, err)
628+
scanner := bufio.NewScanner(&outBuf)
629+
for scanner.Scan() {
630+
headers = append(headers, scanner.Text())
633631
}
634-
for key, value := range headers {
635-
transport.header.Add(key, value)
632+
if err := scanner.Err(); err != nil {
633+
return xerrors.Errorf("scan %v: %w", cmd.Args, err)
636634
}
637635
}
636+
for _, header := range headers {
637+
parts := strings.SplitN(header, "=", 2)
638+
if len(parts) < 2 {
639+
return xerrors.Errorf("split header %q had less than two parts", header)
640+
}
641+
transport.header.Add(parts[0], parts[1])
642+
}
638643
client.URL = serverURL
639644
client.HTTPClient = &http.Client{
640645
Transport: transport,

cli/root_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func TestRoot(t *testing.T) {
8080
assert.Equal(t, "wow", r.Header.Get("X-Testing"))
8181
assert.Equal(t, "Dean was Here!", r.Header.Get("Cool-Header"))
8282
assert.Equal(t, "very-wow-"+url, r.Header.Get("X-Process-Testing"))
83+
assert.Equal(t, "more-wow", r.Header.Get("X-Process-Testing2"))
8384
w.WriteHeader(http.StatusGone)
8485
}))
8586
defer srv.Close()
@@ -94,7 +95,7 @@ func TestRoot(t *testing.T) {
9495
"--no-version-warning",
9596
"--header", "X-Testing=wow",
9697
"--header", "Cool-Header=Dean was Here!",
97-
"--header-command", "printf '{\"X-Process-Testing\": \"very-wow-'"+coderURLEnv+"'\"}'",
98+
"--header-command", "printf X-Process-Testing=very-wow-"+coderURLEnv+"'\\r\\n'X-Process-Testing2=more-wow",
9899
"login", srv.URL,
99100
)
100101
inv.Stdout = buf
@@ -169,7 +170,7 @@ func TestDERPHeaders(t *testing.T) {
169170
"--no-version-warning",
170171
"ping", workspace.Name,
171172
"-n", "1",
172-
"--header-command", "printf '{\"X-Process-Testing\": \"very-wow\"}'",
173+
"--header-command", "printf X-Process-Testing=very-wow",
173174
}
174175
for k, v := range expectedHeaders {
175176
if k != "X-Process-Testing" {

cli/testdata/coder_--help.golden

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ variables or flags.
6363
Can be specified multiple times.
6464

6565
--header-command string, $CODER_HEADER_COMMAND
66-
An external command that outputs JSON-encoded key-value pairs to be
67-
used as additional HTTP headers added to all requests.
66+
An external command that outputs additional HTTP headers added to all
67+
requests. The command must output each header as `key=value` on its
68+
own line.
6869

6970
--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
7071
Suppress warnings about unlicensed features.

docs/cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Additional HTTP headers added to all requests. Provide as key=value. Can be spec
103103
| Type | <code>string</code> |
104104
| Environment | <code>$CODER_HEADER_COMMAND</code> |
105105

106-
An external command that outputs JSON-encoded key-value pairs to be used as additional HTTP headers added to all requests.
106+
An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line.
107107

108108
### --no-feature-warning
109109

enterprise/cli/testdata/coder_--help.golden

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ variables or flags.
3434
Can be specified multiple times.
3535

3636
--header-command string, $CODER_HEADER_COMMAND
37-
An external command that outputs JSON-encoded key-value pairs to be
38-
used as additional HTTP headers added to all requests.
37+
An external command that outputs additional HTTP headers added to all
38+
requests. The command must output each header as `key=value` on its
39+
own line.
3940

4041
--no-feature-warning bool, $CODER_NO_FEATURE_WARNING
4142
Suppress warnings about unlicensed features.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy