Skip to content

Commit 22c4a7e

Browse files
committed
Make linking and starting code-server to the cloud a single command
1 parent 607444c commit 22c4a7e

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/node/cli.ts

Lines changed: 3 additions & 3 deletions
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 expose?: OptionalString
51+
readonly "coder-link"?: OptionalString
5252
}
5353

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

162-
expose: {
162+
"coder-link": {
163163
type: OptionalString,
164164
description: `
165-
Securely expose code-server via Coder Cloud with the passed name. You'll get a URL like
165+
Securely link 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.
167167
Authorization is done via GitHub. Only the first code-server spawned with the current
168168
configuration will be accessible.`,

src/node/coder-cloud.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import xdgBasedir from "xdg-basedir"
99

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

12-
export async function coderCloudExpose(serverName: string): Promise<void> {
12+
export async function coderCloudLink(serverName: string): Promise<void> {
1313
const agent = spawn(coderCloudAgent, ["link", serverName], {
1414
stdio: ["inherit", "inherit", "pipe"],
1515
})

src/node/entry.ts

Lines changed: 26 additions & 15 deletions
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 { coderCloudExpose, coderCloudProxy } from "./coder-cloud"
15+
import { coderCloudLink, coderCloudProxy } from "./coder-cloud"
1616
import { AuthType, HttpServer, HttpServerOptions } from "./http"
1717
import { loadPlugins } from "./plugin"
1818
import { generateCertificate, hash, humanPath, open } from "./util"
@@ -36,6 +36,15 @@ const version = pkg.version || "development"
3636
const commit = pkg.commit || "development"
3737

3838
const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise<void> => {
39+
if (args["coder-link"]) {
40+
// If we're being exposed to the cloud, we listen on a random address.
41+
args = {
42+
...args,
43+
host: "localhost",
44+
port: 0,
45+
}
46+
}
47+
3948
if (!args.auth) {
4049
args = {
4150
...args,
@@ -131,6 +140,22 @@ const main = async (args: Args, cliArgs: Args, configArgs: Args): Promise<void>
131140
await open(openAddress).catch(console.error)
132141
logger.info(`Opened ${openAddress}`)
133142
}
143+
144+
if (args["coder-link"]) {
145+
if (!args["coder-link"].value) {
146+
logger.error("You must pass a name to link with coder cloud. See --help")
147+
process.exit(1)
148+
}
149+
150+
logger.info(`linking code-server to the cloud with name ${args["coder-link"].value}`)
151+
152+
try {
153+
await coderCloudLink(args["coder-link"].value)
154+
} catch (err) {
155+
logger.error(err.message)
156+
process.exit(1)
157+
}
158+
}
134159
}
135160

136161
async function entry(): Promise<void> {
@@ -191,20 +216,6 @@ async function entry(): Promise<void> {
191216
process.exit(1)
192217
})
193218
vscode.on("exit", (code) => process.exit(code || 0))
194-
} else if (args["expose"]) {
195-
logger.debug("exposing code-server via the coder-cloud agent")
196-
197-
if (!args["expose"].value) {
198-
logger.error("You must pass a name to expose with coder cloud. See --help")
199-
process.exit(1)
200-
}
201-
202-
try {
203-
await coderCloudExpose(args["expose"].value)
204-
} catch (err) {
205-
logger.error(err.message)
206-
process.exit(1)
207-
}
208219
} else if (process.env.VSCODE_IPC_HOOK_CLI) {
209220
const pipeArgs: OpenCommandPipeArgs = {
210221
type: "open",

0 commit comments

Comments
 (0)