Archived
1
0

Move log level defaults into setDefaults

This will allow cliArgs to be only the actual arguments the user passed
which will be used for some logic around opening in existing instances.
This commit is contained in:
Asher 2020-09-15 13:43:07 -05:00
parent 1902296702
commit 021c084e43
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
2 changed files with 61 additions and 38 deletions

View File

@ -327,6 +327,21 @@ export const parse = (
logger.debug("parsed command line", field("args", args)) logger.debug("parsed command line", field("args", args))
return args
}
export async function setDefaults(args: Args): Promise<Args> {
args = { ...args }
if (!args["user-data-dir"]) {
await copyOldMacOSDataDir()
args["user-data-dir"] = paths.data
}
if (!args["extensions-dir"]) {
args["extensions-dir"] = path.join(args["user-data-dir"], "extensions")
}
// --verbose takes priority over --log and --log takes priority over the // --verbose takes priority over --log and --log takes priority over the
// environment variable. // environment variable.
if (args.verbose) { if (args.verbose) {
@ -369,21 +384,6 @@ export const parse = (
return args return args
} }
export async function setDefaults(args: Args): Promise<Args> {
args = { ...args }
if (!args["user-data-dir"]) {
await copyOldMacOSDataDir()
args["user-data-dir"] = paths.data
}
if (!args["extensions-dir"]) {
args["extensions-dir"] = path.join(args["user-data-dir"], "extensions")
}
return args
}
async function defaultConfigFile(): Promise<string> { async function defaultConfigFile(): Promise<string> {
return `bind-addr: 127.0.0.1:8080 return `bind-addr: 127.0.0.1:8080
auth: password auth: password

View File

@ -1,20 +1,23 @@
import { logger, Level } from "@coder/logger" import { Level, logger } from "@coder/logger"
import * as assert from "assert" import * as assert from "assert"
import * as path from "path" import * as path from "path"
import { parse } from "../src/node/cli" import { parse, setDefaults } from "../src/node/cli"
import { paths } from "../src/node/util"
describe("cli", () => { describe("cli", () => {
beforeEach(() => { beforeEach(() => {
delete process.env.LOG_LEVEL delete process.env.LOG_LEVEL
}) })
// The parser will always fill these out. // The parser should not set any defaults so the caller can determine what
// values the user actually set. These are set after calling `setDefaults`.
const defaults = { const defaults = {
_: [], "extensions-dir": path.join(paths.data, "extensions"),
"user-data-dir": paths.data,
} }
it("should set defaults", () => { it("should set defaults", () => {
assert.deepEqual(parse([]), defaults) assert.deepEqual(parse([]), { _: [] })
}) })
it("should parse all available options", () => { it("should parse all available options", () => {
@ -69,7 +72,7 @@ describe("cli", () => {
help: true, help: true,
host: "0.0.0.0", host: "0.0.0.0",
json: true, json: true,
log: "trace", log: "error",
open: true, open: true,
port: 8081, port: 8081,
socket: path.resolve("mumble"), socket: path.resolve("mumble"),
@ -83,19 +86,20 @@ describe("cli", () => {
it("should work with short options", () => { it("should work with short options", () => {
assert.deepEqual(parse(["-vvv", "-v"]), { assert.deepEqual(parse(["-vvv", "-v"]), {
...defaults, _: [],
log: "trace",
verbose: true, verbose: true,
version: true, version: true,
}) })
assert.equal(process.env.LOG_LEVEL, "trace")
assert.equal(logger.level, Level.Trace)
}) })
it("should use log level env var", () => { it("should use log level env var", async () => {
const args = parse([])
assert.deepEqual(args, { _: [] })
process.env.LOG_LEVEL = "debug" process.env.LOG_LEVEL = "debug"
assert.deepEqual(parse([]), { assert.deepEqual(await setDefaults(args), {
...defaults, ...defaults,
_: [],
log: "debug", log: "debug",
verbose: false, verbose: false,
}) })
@ -103,8 +107,9 @@ describe("cli", () => {
assert.equal(logger.level, Level.Debug) assert.equal(logger.level, Level.Debug)
process.env.LOG_LEVEL = "trace" process.env.LOG_LEVEL = "trace"
assert.deepEqual(parse([]), { assert.deepEqual(await setDefaults(args), {
...defaults, ...defaults,
_: [],
log: "trace", log: "trace",
verbose: true, verbose: true,
}) })
@ -113,9 +118,16 @@ describe("cli", () => {
}) })
it("should prefer --log to env var and --verbose to --log", async () => { it("should prefer --log to env var and --verbose to --log", async () => {
let args = parse(["--log", "info"])
assert.deepEqual(args, {
_: [],
log: "info",
})
process.env.LOG_LEVEL = "debug" process.env.LOG_LEVEL = "debug"
assert.deepEqual(parse(["--log", "info"]), { assert.deepEqual(await setDefaults(args), {
...defaults, ...defaults,
_: [],
log: "info", log: "info",
verbose: false, verbose: false,
}) })
@ -123,17 +135,26 @@ describe("cli", () => {
assert.equal(logger.level, Level.Info) assert.equal(logger.level, Level.Info)
process.env.LOG_LEVEL = "trace" process.env.LOG_LEVEL = "trace"
assert.deepEqual(parse(["--log", "info"]), { assert.deepEqual(await setDefaults(args), {
...defaults, ...defaults,
_: [],
log: "info", log: "info",
verbose: false, verbose: false,
}) })
assert.equal(process.env.LOG_LEVEL, "info") assert.equal(process.env.LOG_LEVEL, "info")
assert.equal(logger.level, Level.Info) assert.equal(logger.level, Level.Info)
args = parse(["--log", "info", "--verbose"])
assert.deepEqual(args, {
_: [],
log: "info",
verbose: true,
})
process.env.LOG_LEVEL = "warn" process.env.LOG_LEVEL = "warn"
assert.deepEqual(parse(["--log", "info", "--verbose"]), { assert.deepEqual(await setDefaults(args), {
...defaults, ...defaults,
_: [],
log: "trace", log: "trace",
verbose: true, verbose: true,
}) })
@ -141,9 +162,12 @@ describe("cli", () => {
assert.equal(logger.level, Level.Trace) assert.equal(logger.level, Level.Trace)
}) })
it("should ignore invalid log level env var", () => { it("should ignore invalid log level env var", async () => {
process.env.LOG_LEVEL = "bogus" process.env.LOG_LEVEL = "bogus"
assert.deepEqual(parse([]), defaults) assert.deepEqual(await setDefaults(parse([])), {
_: [],
...defaults,
})
}) })
it("should error if value isn't provided", () => { it("should error if value isn't provided", () => {
@ -166,7 +190,7 @@ describe("cli", () => {
it("should not error if the value is optional", () => { it("should not error if the value is optional", () => {
assert.deepEqual(parse(["--cert"]), { assert.deepEqual(parse(["--cert"]), {
...defaults, _: [],
cert: { cert: {
value: undefined, value: undefined,
}, },
@ -177,7 +201,7 @@ describe("cli", () => {
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/) assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/)
// If you actually had a path like this you would do this instead: // If you actually had a path like this you would do this instead:
assert.deepEqual(parse(["--socket", "./--socket-path-value"]), { assert.deepEqual(parse(["--socket", "./--socket-path-value"]), {
...defaults, _: [],
socket: path.resolve("--socket-path-value"), socket: path.resolve("--socket-path-value"),
}) })
assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/) assert.throws(() => parse(["--cert", "--socket-path-value"]), /Unknown option --socket-path-value/)
@ -185,7 +209,6 @@ describe("cli", () => {
it("should allow positional arguments before options", () => { it("should allow positional arguments before options", () => {
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), { assert.deepEqual(parse(["foo", "test", "--auth", "none"]), {
...defaults,
_: ["foo", "test"], _: ["foo", "test"],
auth: "none", auth: "none",
}) })
@ -193,11 +216,11 @@ describe("cli", () => {
it("should support repeatable flags", () => { it("should support repeatable flags", () => {
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), { assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), {
...defaults, _: [],
"proxy-domain": ["*.coder.com"], "proxy-domain": ["*.coder.com"],
}) })
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), { assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
...defaults, _: [],
"proxy-domain": ["*.coder.com", "test.com"], "proxy-domain": ["*.coder.com", "test.com"],
}) })
}) })