Skip to content

Commit

Permalink
feat: fall back to CODER_HEADER_COMMAND environment variable if not s…
Browse files Browse the repository at this point in the history
…et (#160)
  • Loading branch information
JoshVee authored Nov 3, 2023
1 parent feb4971 commit 15c7cba
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"default": ""
},
"coder.headerCommand": {
"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`.",
"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.",
"type": "string",
"default": ""
},
Expand Down Expand Up @@ -291,4 +291,4 @@
"trim": "0.0.3",
"word-wrap": "1.2.5"
}
}
}
51 changes: 49 additions & 2 deletions src/headers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as os from "os"
import { it, expect } from "vitest"
import { getHeaders } from "./headers"
import { it, expect, describe, beforeEach, afterEach, vi } from "vitest"
import { WorkspaceConfiguration } from "vscode"
import { getHeaderCommand, getHeaders } from "./headers"

const logger = {
writeToCoderOutputChannel() {
Expand Down Expand Up @@ -55,3 +56,49 @@ it("should have access to environment variables", async () => {
it("should error on non-zero exit", async () => {
await expect(getHeaders("localhost", "exit 10", logger)).rejects.toMatch(/exited unexpectedly with code 10/)
})

describe("getHeaderCommand", () => {
beforeEach(() => {
vi.stubEnv("CODER_HEADER_COMMAND", "")
})

afterEach(() => {
vi.unstubAllEnvs()
})

it("should return undefined if coder.headerCommand is not set in config", () => {
const config = {
get: () => undefined,
} as unknown as WorkspaceConfiguration

expect(getHeaderCommand(config)).toBeUndefined()
})

it("should return undefined if coder.headerCommand is not a string", () => {
const config = {
get: () => 1234,
} as unknown as WorkspaceConfiguration

expect(getHeaderCommand(config)).toBeUndefined()
})

it("should return coder.headerCommand if set in config", () => {
vi.stubEnv("CODER_HEADER_COMMAND", "printf 'x=y'")

const config = {
get: () => "printf 'foo=bar'",
} as unknown as WorkspaceConfiguration

expect(getHeaderCommand(config)).toBe("printf 'foo=bar'")
})

it("should return CODER_HEADER_COMMAND if coder.headerCommand is not set in config and CODER_HEADER_COMMAND is set in environment", () => {
vi.stubEnv("CODER_HEADER_COMMAND", "printf 'x=y'")

const config = {
get: () => undefined,
} as unknown as WorkspaceConfiguration

expect(getHeaderCommand(config)).toBe("printf 'x=y'")
})
})
10 changes: 10 additions & 0 deletions src/headers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as cp from "child_process"
import * as util from "util"

import { WorkspaceConfiguration } from "vscode"

export interface Logger {
writeToCoderOutputChannel(message: string): void
}
Expand All @@ -15,6 +17,14 @@ function isExecException(err: unknown): err is ExecException {
return typeof (err as ExecException).code !== "undefined"
}

export function getHeaderCommand(config: WorkspaceConfiguration): string | undefined {
const cmd = config.get("coder.headerCommand") || process.env.CODER_HEADER_COMMAND
if (!cmd || typeof cmd !== "string") {
return undefined
}
return cmd
}

// TODO: getHeaders might make more sense to directly implement on Storage
// but it is difficult to test Storage right now since we use vitest instead of
// the standard extension testing framework which would give us access to vscode
Expand Down
3 changes: 2 additions & 1 deletion src/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import prettyBytes from "pretty-bytes"
import * as semver from "semver"
import * as vscode from "vscode"
import * as ws from "ws"
import { getHeaderCommand } from "./headers"
import { SSHConfig, SSHValues, defaultSSHConfigResponse, mergeSSHConfigValues } from "./sshConfig"
import { computeSSHProperties, sshSupportsSetEnv } from "./sshSupport"
import { Storage } from "./storage"
Expand Down Expand Up @@ -537,7 +538,7 @@ export class Remote {

// Add headers from the header command.
let headerArg = ""
const headerCommand = vscode.workspace.getConfiguration().get("coder.headerCommand")
const headerCommand = getHeaderCommand(vscode.workspace.getConfiguration())
if (typeof headerCommand === "string" && headerCommand.trim().length > 0) {
headerArg = ` --header-command ${escape(headerCommand)}`
}
Expand Down
4 changes: 2 additions & 2 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import os from "os"
import path from "path"
import prettyBytes from "pretty-bytes"
import * as vscode from "vscode"
import { getHeaders } from "./headers"
import { getHeaderCommand, getHeaders } from "./headers"

export class Storage {
public workspace?: Workspace
Expand Down Expand Up @@ -397,7 +397,7 @@ export class Storage {
}

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

Expand Down

0 comments on commit 15c7cba

Please sign in to comment.