Use Addr interface everywhere and loop over arg sources

This commit is contained in:
Asher 2020-11-03 14:26:25 -06:00
parent 1067507c41
commit 8a9e61defb
No known key found for this signature in database
GPG Key ID: D63C1EF81242354A
1 changed files with 17 additions and 13 deletions

View File

@ -414,9 +414,9 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
args.auth = AuthType.Password args.auth = AuthType.Password
} }
const [host, port] = bindAddrFromAllSources(args, configArgs || { _: [] }) const addr = bindAddrFromAllSources(args, configArgs || { _: [] })
args.host = host args.host = addr.host
args.port = port args.port = addr.port
// If we're being exposed to the cloud, we listen on a random address and // If we're being exposed to the cloud, we listen on a random address and
// disable auth. // disable auth.
@ -512,12 +512,15 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
} }
} }
function parseBindAddr(bindAddr: string): [string, number] { function parseBindAddr(bindAddr: string): Addr {
const u = new URL(`http://${bindAddr}`) const u = new URL(`http://${bindAddr}`)
// With the http scheme 80 will be dropped so assume it's 80 if missing. This return {
// means --bind-addr <addr> without a port will default to 80 as well and not host: u.hostname,
// the code-server default. // With the http scheme 80 will be dropped so assume it's 80 if missing.
return [u.hostname, u.port ? parseInt(u.port, 10) : 80] // This means --bind-addr <addr> without a port will default to 80 as well
// and not the code-server default.
port: u.port ? parseInt(u.port, 10) : 80,
}
} }
interface Addr { interface Addr {
@ -528,7 +531,7 @@ interface Addr {
function bindAddrFromArgs(addr: Addr, args: Args): Addr { function bindAddrFromArgs(addr: Addr, args: Args): Addr {
addr = { ...addr } addr = { ...addr }
if (args["bind-addr"]) { if (args["bind-addr"]) {
;[addr.host, addr.port] = parseBindAddr(args["bind-addr"]) addr = parseBindAddr(args["bind-addr"])
} }
if (args.host) { if (args.host) {
addr.host = args.host addr.host = args.host
@ -543,16 +546,17 @@ function bindAddrFromArgs(addr: Addr, args: Args): Addr {
return addr return addr
} }
function bindAddrFromAllSources(cliArgs: Args, configArgs: Args): [string, number] { function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
let addr: Addr = { let addr: Addr = {
host: "localhost", host: "localhost",
port: 8080, port: 8080,
} }
addr = bindAddrFromArgs(addr, configArgs) for (const args of argsConfig) {
addr = bindAddrFromArgs(addr, cliArgs) addr = bindAddrFromArgs(addr, args)
}
return [addr.host, addr.port] return addr
} }
async function copyOldMacOSDataDir(): Promise<void> { async function copyOldMacOSDataDir(): Promise<void> {