Copy old macOS data directory if applicable

This commit is contained in:
Anmol Sethi 2020-05-14 06:08:37 -04:00
parent f475767c2b
commit 5651201643
No known key found for this signature in database
GPG Key ID: 8CEF1878FF10ADEB
3 changed files with 120 additions and 93 deletions

View File

@ -1,6 +1,7 @@
import { field, Level, logger } from "@coder/logger" import { field, Level, logger } from "@coder/logger"
import * as fs from "fs-extra" import * as fs from "fs-extra"
import yaml from "js-yaml" import yaml from "js-yaml"
import * as os from "os"
import * as path from "path" import * as path from "path"
import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc" import { Args as VsArgs } from "../../lib/vscode/src/vs/server/ipc"
import { AuthType } from "./http" import { AuthType } from "./http"
@ -151,12 +152,12 @@ export const optionDescriptions = (): string[] => {
) )
} }
export const parse = ( export const parse = async (
argv: string[], argv: string[],
opts?: { opts?: {
configFile: string configFile: string
}, },
): Args => { ): Promise<Args> => {
const error = (msg: string): Error => { const error = (msg: string): Error => {
if (opts?.configFile) { if (opts?.configFile) {
msg = `error reading ${opts.configFile}: ${msg}` msg = `error reading ${opts.configFile}: ${msg}`
@ -300,6 +301,7 @@ export const parse = (
} }
if (!args["user-data-dir"]) { if (!args["user-data-dir"]) {
await copyOldMacOSDataDir()
args["user-data-dir"] = paths.data args["user-data-dir"] = paths.data
} }
@ -351,7 +353,7 @@ export async function readConfigFile(configPath?: string): Promise<Args> {
} }
return `--${optName}=${opt}` return `--${optName}=${opt}`
}) })
const args = parse(configFileArgv, { const args = await parse(configFileArgv, {
configFile: configPath, configFile: configPath,
}) })
return { return {
@ -400,3 +402,18 @@ export function bindAddrFromAllSources(cliArgs: Args, configArgs: Args): [string
return [addr.host, addr.port] return [addr.host, addr.port]
} }
async function copyOldMacOSDataDir(): Promise<void> {
if (os.platform() !== "darwin") {
return
}
if (await fs.pathExists(paths.data)) {
return
}
// If the old data directory exists, we copy it in.
const oldDataDir = path.join(os.homedir(), "Library/Application Support", "code-server")
if (await fs.pathExists(oldDataDir)) {
await fs.copy(oldDataDir, paths.data)
}
}

View File

@ -125,16 +125,17 @@ const main = async (cliArgs: Args): Promise<void> => {
} }
} }
const tryParse = (): Args => { async function entry(): Promise<void> {
const tryParse = async (): Promise<Args> => {
try { try {
return parse(process.argv.slice(2)) return await parse(process.argv.slice(2))
} catch (error) { } catch (error) {
console.error(error.message) console.error(error.message)
process.exit(1) process.exit(1)
} }
} }
const args = tryParse() const args = await tryParse()
if (args.help) { if (args.help) {
console.log("code-server", version, commit) console.log("code-server", version, commit)
console.log("") console.log("")
@ -180,3 +181,6 @@ if (args.help) {
} else { } else {
wrap(() => main(args)) wrap(() => main(args))
} }
}
entry()

View File

@ -16,13 +16,13 @@ describe("cli", () => {
"user-data-dir": paths.data, "user-data-dir": paths.data,
} }
it("should set defaults", () => { it("should set defaults", async () => {
assert.deepEqual(parse([]), defaults) assert.deepEqual(await await parse([]), defaults)
}) })
it("should parse all available options", () => { it("should parse all available options", async () => {
assert.deepEqual( assert.deepEqual(
parse([ await await parse([
"--bind-addr=192.169.0.1:8080", "--bind-addr=192.169.0.1:8080",
"--auth", "--auth",
"none", "none",
@ -84,8 +84,8 @@ describe("cli", () => {
) )
}) })
it("should work with short options", () => { it("should work with short options", async () => {
assert.deepEqual(parse(["-vvv", "-v"]), { assert.deepEqual(await parse(["-vvv", "-v"]), {
...defaults, ...defaults,
log: "trace", log: "trace",
verbose: true, verbose: true,
@ -95,9 +95,9 @@ describe("cli", () => {
assert.equal(logger.level, Level.Trace) assert.equal(logger.level, Level.Trace)
}) })
it("should use log level env var", () => { it("should use log level env var", async () => {
process.env.LOG_LEVEL = "debug" process.env.LOG_LEVEL = "debug"
assert.deepEqual(parse([]), { assert.deepEqual(await parse([]), {
...defaults, ...defaults,
log: "debug", log: "debug",
}) })
@ -105,7 +105,7 @@ 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 parse([]), {
...defaults, ...defaults,
log: "trace", log: "trace",
verbose: true, verbose: true,
@ -114,9 +114,9 @@ describe("cli", () => {
assert.equal(logger.level, Level.Trace) assert.equal(logger.level, Level.Trace)
}) })
it("should prefer --log to env var and --verbose to --log", () => { it("should prefer --log to env var and --verbose to --log", async () => {
process.env.LOG_LEVEL = "debug" process.env.LOG_LEVEL = "debug"
assert.deepEqual(parse(["--log", "info"]), { assert.deepEqual(await parse(["--log", "info"]), {
...defaults, ...defaults,
log: "info", log: "info",
}) })
@ -124,7 +124,7 @@ 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 parse(["--log", "info"]), {
...defaults, ...defaults,
log: "info", log: "info",
}) })
@ -132,7 +132,7 @@ describe("cli", () => {
assert.equal(logger.level, Level.Info) assert.equal(logger.level, Level.Info)
process.env.LOG_LEVEL = "warn" process.env.LOG_LEVEL = "warn"
assert.deepEqual(parse(["--log", "info", "--verbose"]), { assert.deepEqual(await parse(["--log", "info", "--verbose"]), {
...defaults, ...defaults,
log: "trace", log: "trace",
verbose: true, verbose: true,
@ -141,31 +141,34 @@ 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 parse([]), defaults)
}) })
it("should error if value isn't provided", () => { it("should error if value isn't provided", async () => {
assert.throws(() => parse(["--auth"]), /--auth requires a value/) await assert.rejects(async () => await parse(["--auth"]), /--auth requires a value/)
assert.throws(() => parse(["--auth=", "--log=debug"]), /--auth requires a value/) await assert.rejects(async () => await parse(["--auth=", "--log=debug"]), /--auth requires a value/)
assert.throws(() => parse(["--auth", "--log"]), /--auth requires a value/) await assert.rejects(async () => await parse(["--auth", "--log"]), /--auth requires a value/)
assert.throws(() => parse(["--auth", "--invalid"]), /--auth requires a value/) await assert.rejects(async () => await parse(["--auth", "--invalid"]), /--auth requires a value/)
assert.throws(() => parse(["--bind-addr"]), /--bind-addr requires a value/) await assert.rejects(async () => await parse(["--bind-addr"]), /--bind-addr requires a value/)
}) })
it("should error if value is invalid", () => { it("should error if value is invalid", async () => {
assert.throws(() => parse(["--port", "foo"]), /--port must be a number/) await assert.rejects(async () => await parse(["--port", "foo"]), /--port must be a number/)
assert.throws(() => parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/) await assert.rejects(async () => await parse(["--auth", "invalid"]), /--auth valid values: \[password, none\]/)
assert.throws(() => parse(["--log", "invalid"]), /--log valid values: \[trace, debug, info, warn, error\]/) await assert.rejects(
async () => await parse(["--log", "invalid"]),
/--log valid values: \[trace, debug, info, warn, error\]/,
)
}) })
it("should error if the option doesn't exist", () => { it("should error if the option doesn't exist", async () => {
assert.throws(() => parse(["--foo"]), /Unknown option --foo/) await assert.rejects(async () => await parse(["--foo"]), /Unknown option --foo/)
}) })
it("should not error if the value is optional", () => { it("should not error if the value is optional", async () => {
assert.deepEqual(parse(["--cert"]), { assert.deepEqual(await parse(["--cert"]), {
...defaults, ...defaults,
cert: { cert: {
value: undefined, value: undefined,
@ -173,30 +176,33 @@ describe("cli", () => {
}) })
}) })
it("should not allow option-like values", () => { it("should not allow option-like values", async () => {
assert.throws(() => parse(["--socket", "--socket-path-value"]), /--socket requires a value/) await assert.rejects(async () => await 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(await parse(["--socket", "./--socket-path-value"]), {
...defaults, ...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/) await assert.rejects(
async () => await parse(["--cert", "--socket-path-value"]),
/Unknown option --socket-path-value/,
)
}) })
it("should allow positional arguments before options", () => { it("should allow positional arguments before options", async () => {
assert.deepEqual(parse(["foo", "test", "--auth", "none"]), { assert.deepEqual(await parse(["foo", "test", "--auth", "none"]), {
...defaults, ...defaults,
_: ["foo", "test"], _: ["foo", "test"],
auth: "none", auth: "none",
}) })
}) })
it("should support repeatable flags", () => { it("should support repeatable flags", async () => {
assert.deepEqual(parse(["--proxy-domain", "*.coder.com"]), { assert.deepEqual(await parse(["--proxy-domain", "*.coder.com"]), {
...defaults, ...defaults,
"proxy-domain": ["*.coder.com"], "proxy-domain": ["*.coder.com"],
}) })
assert.deepEqual(parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), { assert.deepEqual(await parse(["--proxy-domain", "*.coder.com", "--proxy-domain", "test.com"]), {
...defaults, ...defaults,
"proxy-domain": ["*.coder.com", "test.com"], "proxy-domain": ["*.coder.com", "test.com"],
}) })