Skip to content

Commit 183d9f9

Browse files
committed
Allow user-data-dir and extension-dir in config.yaml
Closes #1676
1 parent aa87270 commit 183d9f9

File tree

5 files changed

+60
-52
lines changed

5 files changed

+60
-52
lines changed

doc/FAQ.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
34
# FAQ
45

56
- [Questions?](#questions)

doc/guide.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
22
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
3+
34
# Setup Guide
45

56
- [1. Acquire a remote machine](#1-acquire-a-remote-machine)

src/node/cli.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ export const optionDescriptions = (): string[] => {
152152
)
153153
}
154154

155-
export const parse = async (
155+
export const parse = (
156156
argv: string[],
157157
opts?: {
158158
configFile: string
159159
},
160-
): Promise<Args> => {
160+
): Args => {
161161
const error = (msg: string): Error => {
162162
if (opts?.configFile) {
163163
msg = `error reading ${opts.configFile}: ${msg}`
@@ -288,18 +288,28 @@ export const parse = async (
288288
break
289289
case LogLevel.Debug:
290290
logger.level = Level.Debug
291+
args.verbose = false
291292
break
292293
case LogLevel.Info:
293294
logger.level = Level.Info
295+
args.verbose = false
294296
break
295297
case LogLevel.Warn:
296298
logger.level = Level.Warning
299+
args.verbose = false
297300
break
298301
case LogLevel.Error:
299302
logger.level = Level.Error
303+
args.verbose = false
300304
break
301305
}
302306

307+
return args
308+
}
309+
310+
export async function setDefaults(args: Args): Promise<Args> {
311+
args = { ...args }
312+
303313
if (!args["user-data-dir"]) {
304314
await copyOldMacOSDataDir()
305315
args["user-data-dir"] = paths.data
@@ -353,7 +363,7 @@ export async function readConfigFile(configPath?: string): Promise<Args> {
353363
}
354364
return `--${optName}=${opt}`
355365
})
356-
const args = await parse(configFileArgv, {
366+
const args = parse(configFileArgv, {
357367
configFile: configPath,
358368
})
359369
return {

src/node/entry.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ProxyHttpProvider } from "./app/proxy"
99
import { StaticHttpProvider } from "./app/static"
1010
import { UpdateHttpProvider } from "./app/update"
1111
import { VscodeHttpProvider } from "./app/vscode"
12-
import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile } from "./cli"
12+
import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli"
1313
import { AuthType, HttpServer, HttpServerOptions } from "./http"
1414
import { generateCertificate, hash, open, humanPath } from "./util"
1515
import { ipcMain, wrap } from "./wrapper"
@@ -43,6 +43,8 @@ const main = async (cliArgs: Args): Promise<void> => {
4343
}
4444
}
4545

46+
args = await setDefaults(args)
47+
4648
logger.info(`Using user-data-dir ${humanPath(args["user-data-dir"])}`)
4749

4850
logger.trace(`Using extensions-dir ${humanPath(args["extensions-dir"])}`)
@@ -127,9 +129,9 @@ const main = async (cliArgs: Args): Promise<void> => {
127129
}
128130

129131
async function entry(): Promise<void> {
130-
const tryParse = async (): Promise<Args> => {
132+
const tryParse = (): Args => {
131133
try {
132-
return await parse(process.argv.slice(2))
134+
return parse(process.argv.slice(2))
133135
} catch (error) {
134136
console.error(error.message)
135137
process.exit(1)

test/cli.test.ts

+40-46
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { logger, Level } from "@coder/logger"
22
import * as assert from "assert"
33
import * as path from "path"
44
import { parse } from "../src/node/cli"
5-
import { paths } from "../src/node/util"
65

76
describe("cli", () => {
87
beforeEach(() => {
@@ -12,17 +11,15 @@ describe("cli", () => {
1211
// The parser will always fill these out.
1312
const defaults = {
1413
_: [],
15-
"extensions-dir": path.join(paths.data, "extensions"),
16-
"user-data-dir": paths.data,
1714
}
1815

19-
it("should set defaults", async () => {
20-
assert.deepEqual(await await parse([]), defaults)
16+
it("should set defaults", () => {
17+
assert.deepEqual(parse([]), defaults)
2118
})
2219

23-
it("should parse all available options", async () => {
20+
it("should parse all available options", () => {
2421
assert.deepEqual(
25-
await await parse([
22+
parse([
2623
"--bind-addr=192.169.0.1:8080",
2724
"--auth",
2825
"none",
@@ -84,8 +81,8 @@ describe("cli", () => {
8481
)
8582
})
8683

87-
it("should work with short options", async () => {
88-
assert.deepEqual(await parse(["-vvv", "-v"]), {
84+
it("should work with short options", () => {
85+
assert.deepEqual(parse(["-vvv", "-v"]), {
8986
...defaults,
9087
log: "trace",
9188
verbose: true,
@@ -95,17 +92,18 @@ describe("cli", () => {
9592
assert.equal(logger.level, Level.Trace)
9693
})
9794

98-
it("should use log level env var", async () => {
95+
it("should use log level env var", () => {
9996
process.env.LOG_LEVEL = "debug"
100-
assert.deepEqual(await parse([]), {
97+
assert.deepEqual(parse([]), {
10198
...defaults,
10299
log: "debug",
100+
verbose: false,
103101
})
104102
assert.equal(process.env.LOG_LEVEL, "debug")
105103
assert.equal(logger.level, Level.Debug)
106104

107105
process.env.LOG_LEVEL = "trace"
108-
assert.deepEqual(await parse([]), {
106+
assert.deepEqual(parse([]), {
109107
...defaults,
110108
log: "trace",
111109
verbose: true,
@@ -116,23 +114,25 @@ describe("cli", () => {
116114

117115
it("should prefer --log to env var and --verbose to --log", async () => {
118116
process.env.LOG_LEVEL = "debug"
119-
assert.deepEqual(await parse(["--log", "info"]), {
117+
assert.deepEqual(parse(["--log", "info"]), {
120118
...defaults,
121119
log: "info",
120+
verbose: false,
122121
})
123122
assert.equal(process.env.LOG_LEVEL, "info")
124123
assert.equal(logger.level, Level.Info)
125124

126125
process.env.LOG_LEVEL = "trace"
127-
assert.deepEqual(await parse(["--log", "info"]), {
126+
assert.deepEqual(parse(["--log", "info"]), {
128127
...defaults,
129128
log: "info",
129+
verbose: false,
130130
})
131131
assert.equal(process.env.LOG_LEVEL, "info")
132132
assert.equal(logger.level, Level.Info)
133133

134134
process.env.LOG_LEVEL = "warn"
135-
assert.deepEqual(await parse(["--log", "info", "--verbose"]), {
135+
assert.deepEqual(parse(["--log", "info", "--verbose"]), {
136136
...defaults,
137137
log: "trace",
138138
verbose: true,
@@ -141,68 +141,62 @@ describe("cli", () => {
141141
assert.equal(logger.level, Level.Trace)
142142
})
143143

144-
it("should ignore invalid log level env var", async () => {
144+
it("should ignore invalid log level env var", () => {
145145
process.env.LOG_LEVEL = "bogus"
146-
assert.deepEqual(await parse([]), defaults)
146+
assert.deepEqual(parse([]), defaults)
147147
})
148148

149-
it("should error if value isn't provided", async () => {
150-
await assert.rejects(async () => await parse(["--auth"]), /--auth requires a value/)
151-
await assert.rejects(async () => await parse(["--auth=", "--log=debug"]), /--auth requires a value/)
152-
await assert.rejects(async () => await parse(["--auth", "--log"]), /--auth requires a value/)
153-
await assert.rejects(async () => await parse(["--auth", "--invalid"]), /--auth requires a value/)
154-
await assert.rejects(async () => await parse(["--bind-addr"]), /--bind-addr requires a value/)
149+
it("should error if value isn't provided", () => {
150+
assert.throws(() => parse(["--auth"]), /--auth requires a value/)
151+
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/)
152+
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/)
153+
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/)
154+
assert.throws(() => parse(["--bind-addr"]), /--bind-addr requires a value/)
155155
})
156156

157-
it("should error if value is invalid", async () => {
158-
await assert.rejects(async () => await parse(["--port", "foo"]), /--port must be a number/)
159-
await assert.rejects(async () => await parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
160-
await assert.rejects(
161-
async () => await parse(["--log", "invalid"]),
162-
/--log valid values: \[trace, debug, info, warn, error\]/,
163-
)
157+
it("should error if value is invalid", () => {
158+
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/)
159+
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
160+
assert.throws(() => parse(["--log", "invalid"]), /--log valid values: \[trace, debug, info, warn, error\]/)
164161
})
165162

166-
it("should error if the option doesn't exist", async () => {
167-
await assert.rejects(async () => await parse(["--foo"]), /Unknown option --foo/)
163+
it("should error if the option doesn't exist", () => {
164+
assert.throws(() => parse(["--foo"]), /Unknown option --foo/)
168165
})
169166

170-
it("should not error if the value is optional", async () => {
171-
assert.deepEqual(await parse(["--cert"]), {
167+
it("should not error if the value is optional", () => {
168+
assert.deepEqual(parse(["--cert"]), {
172169
...defaults,
173170
cert: {
174171
value: undefined,
175172
},
176173
})
177174
})
178175

179-
it("should not allow option-like values", async () => {
180-
await assert.rejects(async () => await parse(["--socket", "--socket-path-value"]), /--socket requires a value/)
176+
it("should not allow option-like values", () => {
177+
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/)
181178
// If you actually had a path like this you would do this instead:
182-
assert.deepEqual(await parse(["--socket", "./--socket-path-value"]), {
179+
assert.deepEqual(parse(["--socket", "./--socket-path-value"]), {
183180
...defaults,
184181
socket: path.resolve("--socket-path-value"),
185182
})
186-
await assert.rejects(
187-
async () => await parse(["--cert", "--socket-path-value"]),
188-
/Unknown option --socket-path-value/,
189-
)
183+
assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/)
190184
})
191185

192-
it("should allow positional arguments before options", async () => {
193-
assert.deepEqual(await parse(["foo", "test", "--auth", "none"]), {
186+
it("should allow positional arguments before options", () => {
187+
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), {
194188
...defaults,
195189
_: ["foo", "test"],
196190
auth: "none",
197191
})
198192
})
199193

200-
it("should support repeatable flags", async () => {
201-
assert.deepEqual(await parse(["--proxy-domain", "*.coder.com"]), {
194+
it("should support repeatable flags", () => {
195+
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
202196
...defaults,
203197
"proxy-domain": ["*.coder.com"],
204198
})
205-
assert.deepEqual(await parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
199+
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
206200
...defaults,
207201
"proxy-domain": ["*.coder.com", "test.com"],
208202
})

0 commit comments

Comments
 (0)