Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit958c6c4

Browse files
committed
Make header-command newline-delimited
Instead of JSON.
1 parent4a439ce commit958c6c4

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 externalprocess that outputsJSON-encoded key-value pairstobe used as additional HTTP headers added to all requests.",
366+
Description:"An externalcommand that outputsadditional HTTP headers addedtoall 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:=ranger.header {
609-
parts:=strings.SplitN(header,"=",2)
610-
iflen(parts)<2 {
611-
returnxerrors.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
ifr.headerCommand!="" {
616612
shell:="sh"
617613
caller:="-c"
618614
ifruntime.GOOS=="windows" {
619615
shell="cmd.exe"
620616
caller="/c"
621617
}
618+
varoutBuf 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
iferr!=nil {
627-
returnxerrors.Errorf("failed to run %v (out: %q): %w",cmd.Args,out,err)
626+
returnxerrors.Errorf("failed to run %v: %w",cmd.Args,err)
628627
}
629-
varheadersmap[string]string
630-
err=json.Unmarshal(out,&headers)
631-
iferr!=nil {
632-
returnxerrors.Errorf("failed to parse json from %v (out: %q): %w",cmd.Args,out,err)
628+
scanner:=bufio.NewScanner(&outBuf)
629+
forscanner.Scan() {
630+
headers=append(headers,scanner.Text())
633631
}
634-
forkey,value:=rangeheaders {
635-
transport.header.Add(key,value)
632+
iferr:=scanner.Err();err!=nil {
633+
returnxerrors.Errorf("scan %v: %w",cmd.Args,err)
636634
}
637635
}
636+
for_,header:=rangeheaders {
637+
parts:=strings.SplitN(header,"=",2)
638+
iflen(parts)<2 {
639+
returnxerrors.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
defersrv.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
fork,v:=rangeexpectedHeaders {
175176
ifk!="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 outputsJSON-encoded key-value pairstobe used as additional HTTP headers added to all requests.
106+
An external command that outputsadditional HTTP headers addedtoall 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)

[8]ページ先頭

©2009-2025 Movatter.jp