Skip to content

Commit 8a9e61d

Browse files
committed
Use Addr interface everywhere and loop over arg sources
1 parent 1067507 commit 8a9e61d

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/node/cli.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ export async function setDefaults(cliArgs: Args, configArgs?: ConfigArgs): Promi
414414
args.auth = AuthType.Password
415415
}
416416

417-
const [host, port] = bindAddrFromAllSources(args, configArgs || { _: [] })
418-
args.host = host
419-
args.port = port
417+
const addr = bindAddrFromAllSources(args, configArgs || { _: [] })
418+
args.host = addr.host
419+
args.port = addr.port
420420

421421
// If we're being exposed to the cloud, we listen on a random address and
422422
// disable auth.
@@ -512,12 +512,15 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
512512
}
513513
}
514514

515-
function parseBindAddr(bindAddr: string): [string, number] {
515+
function parseBindAddr(bindAddr: string): Addr {
516516
const u = new URL(`http://${bindAddr}`)
517-
// With the http scheme 80 will be dropped so assume it's 80 if missing. This
518-
// means --bind-addr <addr> without a port will default to 80 as well and not
519-
// the code-server default.
520-
return [u.hostname, u.port ? parseInt(u.port, 10) : 80]
517+
return {
518+
host: u.hostname,
519+
// With the http scheme 80 will be dropped so assume it's 80 if missing.
520+
// This means --bind-addr <addr> without a port will default to 80 as well
521+
// and not the code-server default.
522+
port: u.port ? parseInt(u.port, 10) : 80,
523+
}
521524
}
522525

523526
interface Addr {
@@ -528,7 +531,7 @@ interface Addr {
528531
function bindAddrFromArgs(addr: Addr, args: Args): Addr {
529532
addr = { ...addr }
530533
if (args["bind-addr"]) {
531-
;[addr.host, addr.port] = parseBindAddr(args["bind-addr"])
534+
addr = parseBindAddr(args["bind-addr"])
532535
}
533536
if (args.host) {
534537
addr.host = args.host
@@ -543,16 +546,17 @@ function bindAddrFromArgs(addr: Addr, args: Args): Addr {
543546
return addr
544547
}
545548

546-
function bindAddrFromAllSources(cliArgs: Args, configArgs: Args): [string, number] {
549+
function bindAddrFromAllSources(...argsConfig: Args[]): Addr {
547550
let addr: Addr = {
548551
host: "localhost",
549552
port: 8080,
550553
}
551554

552-
addr = bindAddrFromArgs(addr, configArgs)
553-
addr = bindAddrFromArgs(addr, cliArgs)
555+
for (const args of argsConfig) {
556+
addr = bindAddrFromArgs(addr, args)
557+
}
554558

555-
return [addr.host, addr.port]
559+
return addr
556560
}
557561

558562
async function copyOldMacOSDataDir(): Promise<void> {

0 commit comments

Comments
 (0)