Skip to content

Commit 15c7cba

Browse files
authored
feat: fall back to CODER_HEADER_COMMAND environment variable if not set (#160)
1 parent feb4971 commit 15c7cba

File tree

5 files changed

+65
-7
lines changed

5 files changed

+65
-7
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"default": ""
6565
},
6666
"coder.headerCommand": {
67-
"markdownDescription": "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. The following environment variables will be available to the process: `CODER_URL`.",
67+
"markdownDescription": "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. The following environment variables will be available to the process: `CODER_URL`. Defaults to the value of `CODER_HEADER_COMMAND` if not set.",
6868
"type": "string",
6969
"default": ""
7070
},
@@ -291,4 +291,4 @@
291291
"trim": "0.0.3",
292292
"word-wrap": "1.2.5"
293293
}
294-
}
294+
}

src/headers.test.ts

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as os from "os"
2-
import { it, expect } from "vitest"
3-
import { getHeaders } from "./headers"
2+
import { it, expect, describe, beforeEach, afterEach, vi } from "vitest"
3+
import { WorkspaceConfiguration } from "vscode"
4+
import { getHeaderCommand, getHeaders } from "./headers"
45

56
const logger = {
67
writeToCoderOutputChannel() {
@@ -55,3 +56,49 @@ it("should have access to environment variables", async () => {
5556
it("should error on non-zero exit", async () => {
5657
await expect(getHeaders("localhost", "exit 10", logger)).rejects.toMatch(/exited unexpectedly with code 10/)
5758
})
59+
60+
describe("getHeaderCommand", () => {
61+
beforeEach(() => {
62+
vi.stubEnv("CODER_HEADER_COMMAND", "")
63+
})
64+
65+
afterEach(() => {
66+
vi.unstubAllEnvs()
67+
})
68+
69+
it("should return undefined if coder.headerCommand is not set in config", () => {
70+
const config = {
71+
get: () => undefined,
72+
} as unknown as WorkspaceConfiguration
73+
74+
expect(getHeaderCommand(config)).toBeUndefined()
75+
})
76+
77+
it("should return undefined if coder.headerCommand is not a string", () => {
78+
const config = {
79+
get: () => 1234,
80+
} as unknown as WorkspaceConfiguration
81+
82+
expect(getHeaderCommand(config)).toBeUndefined()
83+
})
84+
85+
it("should return coder.headerCommand if set in config", () => {
86+
vi.stubEnv("CODER_HEADER_COMMAND", "printf 'x=y'")
87+
88+
const config = {
89+
get: () => "printf 'foo=bar'",
90+
} as unknown as WorkspaceConfiguration
91+
92+
expect(getHeaderCommand(config)).toBe("printf 'foo=bar'")
93+
})
94+
95+
it("should return CODER_HEADER_COMMAND if coder.headerCommand is not set in config and CODER_HEADER_COMMAND is set in environment", () => {
96+
vi.stubEnv("CODER_HEADER_COMMAND", "printf 'x=y'")
97+
98+
const config = {
99+
get: () => undefined,
100+
} as unknown as WorkspaceConfiguration
101+
102+
expect(getHeaderCommand(config)).toBe("printf 'x=y'")
103+
})
104+
})

src/headers.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as cp from "child_process"
22
import * as util from "util"
33

4+
import { WorkspaceConfiguration } from "vscode"
5+
46
export interface Logger {
57
writeToCoderOutputChannel(message: string): void
68
}
@@ -15,6 +17,14 @@ function isExecException(err: unknown): err is ExecException {
1517
return typeof (err as ExecException).code !== "undefined"
1618
}
1719

20+
export function getHeaderCommand(config: WorkspaceConfiguration): string | undefined {
21+
const cmd = config.get("coder.headerCommand") || process.env.CODER_HEADER_COMMAND
22+
if (!cmd || typeof cmd !== "string") {
23+
return undefined
24+
}
25+
return cmd
26+
}
27+
1828
// TODO: getHeaders might make more sense to directly implement on Storage
1929
// but it is difficult to test Storage right now since we use vitest instead of
2030
// the standard extension testing framework which would give us access to vscode

src/remote.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import prettyBytes from "pretty-bytes"
2020
import * as semver from "semver"
2121
import * as vscode from "vscode"
2222
import * as ws from "ws"
23+
import { getHeaderCommand } from "./headers"
2324
import { SSHConfig, SSHValues, defaultSSHConfigResponse, mergeSSHConfigValues } from "./sshConfig"
2425
import { computeSSHProperties, sshSupportsSetEnv } from "./sshSupport"
2526
import { Storage } from "./storage"
@@ -537,7 +538,7 @@ export class Remote {
537538

538539
// Add headers from the header command.
539540
let headerArg = ""
540-
const headerCommand = vscode.workspace.getConfiguration().get("coder.headerCommand")
541+
const headerCommand = getHeaderCommand(vscode.workspace.getConfiguration())
541542
if (typeof headerCommand === "string" && headerCommand.trim().length > 0) {
542543
headerArg = ` --header-command ${escape(headerCommand)}`
543544
}

src/storage.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import os from "os"
1111
import path from "path"
1212
import prettyBytes from "pretty-bytes"
1313
import * as vscode from "vscode"
14-
import { getHeaders } from "./headers"
14+
import { getHeaderCommand, getHeaders } from "./headers"
1515

1616
export class Storage {
1717
public workspace?: Workspace
@@ -397,7 +397,7 @@ export class Storage {
397397
}
398398

399399
public async getHeaders(url = this.getURL()): Promise<Record<string, string>> {
400-
return getHeaders(url, vscode.workspace.getConfiguration().get("coder.headerCommand"), this)
400+
return getHeaders(url, getHeaderCommand(vscode.workspace.getConfiguration()), this)
401401
}
402402
}
403403

0 commit comments

Comments
 (0)