Skip to content

Commit df3089f

Browse files
committed
coder-cloud: Use consolidated bind command
1 parent 7cc16ce commit df3089f

File tree

3 files changed

+9
-46
lines changed

3 files changed

+9
-46
lines changed

src/node/cli.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export interface Args extends VsArgs {
4848
readonly "reuse-window"?: boolean
4949
readonly "new-window"?: boolean
5050

51-
readonly "coder-bind"?: string
51+
readonly "coder-bind"?: OptionalString
5252
}
5353

5454
interface Option<T> {
@@ -160,7 +160,7 @@ const options: Options<Required<Args>> = {
160160
verbose: { type: "boolean", short: "vvv", description: "Enable verbose logging." },
161161

162162
"coder-bind": {
163-
type: "string",
163+
type: OptionalString,
164164
description: `
165165
Securely bind code-server via Coder Cloud with the passed name. You'll get a URL like
166166
https://myname.coder-cloud.com at which you can easily access your code-server instance.

src/node/coder-cloud.ts

+5-39
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import split2 from "split2"
55

66
const coderCloudAgent = path.resolve(__dirname, "../../lib/coder-cloud-agent")
77

8-
export async function coderCloudBind(serverName: string): Promise<void> {
9-
const agent = spawn(coderCloudAgent, ["link", serverName], {
8+
function runAgent(...args: string[]): Promise<void> {
9+
const agent = spawn(coderCloudAgent, args, {
1010
stdio: ["inherit", "inherit", "pipe"],
1111
})
1212

@@ -30,43 +30,9 @@ export async function coderCloudBind(serverName: string): Promise<void> {
3030
})
3131
}
3232

33-
export function coderCloudProxy(addr: string) {
33+
export function coderCloudBind(csAddr: string, serverName = ""): Promise<void> {
3434
// addr needs to be in host:port format.
3535
// So we trim the protocol.
36-
addr = addr.replace(/^https?:\/\//, "")
37-
38-
const _proxy = async () => {
39-
const agent = spawn(coderCloudAgent, ["proxy", "--code-server-addr", addr], {
40-
stdio: ["inherit", "inherit", "pipe"],
41-
})
42-
43-
agent.stderr.pipe(split2()).on("data", (line) => {
44-
line = line.replace(/^[0-9-]+ [0-9:]+ [^ ]+\t/, "")
45-
logger.info(line)
46-
})
47-
48-
return new Promise((res, rej) => {
49-
agent.on("error", rej)
50-
51-
agent.on("close", (code) => {
52-
if (code !== 0) {
53-
rej({
54-
message: `coder cloud agent exited with ${code}`,
55-
})
56-
return
57-
}
58-
res()
59-
})
60-
})
61-
}
62-
63-
const proxy = async () => {
64-
try {
65-
await _proxy()
66-
} catch (err) {
67-
logger.error(err.message)
68-
}
69-
setTimeout(proxy, 3000)
70-
}
71-
proxy()
36+
csAddr = csAddr.replace(/^https?:\/\//, "")
37+
return runAgent("bind", `--code-server-addr=${csAddr}`, serverName)
7238
}

src/node/entry.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { StaticHttpProvider } from "./app/static"
1212
import { UpdateHttpProvider } from "./app/update"
1313
import { VscodeHttpProvider } from "./app/vscode"
1414
import { Args, bindAddrFromAllSources, optionDescriptions, parse, readConfigFile, setDefaults } from "./cli"
15-
import { coderCloudBind, coderCloudProxy } from "./coder-cloud"
15+
import { coderCloudBind } from "./coder-cloud"
1616
import { AuthType, HttpServer, HttpServerOptions } from "./http"
1717
import { loadPlugins } from "./plugin"
1818
import { generateCertificate, hash, humanPath, open } from "./util"
@@ -143,11 +143,8 @@ const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise<void>
143143
}
144144

145145
if (args["coder-bind"]) {
146-
147146
try {
148-
logger.info(`binding code-server to the cloud with name ${args["coder-bind"]}`)
149-
await coderCloudBind(args["coder-bind"])
150-
coderCloudProxy(serverAddress!)
147+
await coderCloudBind(serverAddress!, args["coder-bind"].value)
151148
} catch (err) {
152149
logger.error(err.message)
153150
ipcMain().exit(1)

0 commit comments

Comments
 (0)