|
| 1 | +import { EOL } from "os" |
| 2 | +import * as Effect from "effect/Effect" |
| 3 | +import * as NodeServices from "@effect/platform-node/NodeServices" |
| 4 | +import { Option, Redacted } from "effect" |
| 5 | +import { Prompt } from "effect/unstable/cli" |
| 6 | +import { Commands } from "../commands" |
| 7 | +import { Runtime } from "../../framework/runtime" |
| 8 | +import { ServerMode } from "../../services/server-mode" |
| 9 | + |
| 10 | +export default Runtime.handler( |
| 11 | + Commands.commands.login, |
| 12 | + Effect.fn("cli.login")(function* (input) { |
| 13 | + const serverMode = yield* ServerMode.Service |
| 14 | + const gateway = Option.getOrUndefined(input.gateway) ?? process.env.DEEPAGENT_GATEWAY_URL |
| 15 | + if (gateway === undefined) |
| 16 | + return yield* Effect.fail( |
| 17 | + new Error("Gateway URL required. Pass it as an argument or set DEEPAGENT_GATEWAY_URL."), |
| 18 | + ) |
| 19 | + const url = gateway.replace(/\/+$/, "") |
| 20 | + const email = yield* Option.match(input.email, { |
| 21 | + onNone: () => |
| 22 | + Prompt.run(Prompt.text({ message: "Email" })).pipe( |
| 23 | + Effect.provide(NodeServices.layer), |
| 24 | + Effect.mapError(() => new Error("Login cancelled")), |
| 25 | + ), |
| 26 | + onSome: Effect.succeed, |
| 27 | + }) |
| 28 | + const password = yield* Option.match(input.password, { |
| 29 | + onNone: () => |
| 30 | + Prompt.run(Prompt.password({ message: "Password" })).pipe( |
| 31 | + Effect.provide(NodeServices.layer), |
| 32 | + Effect.map(Redacted.value), |
| 33 | + Effect.mapError(() => new Error("Login cancelled")), |
| 34 | + ), |
| 35 | + onSome: Effect.succeed, |
| 36 | + }) |
| 37 | + const state = yield* serverMode.login(url, email, password) |
| 38 | + process.stdout.write( |
| 39 | + `Logged in to ${state.gatewayUrl} as ${email}.${state.workspaceId ? "" : " Run `dacode workspace list` to pick a workspace."}` + |
| 40 | + EOL, |
| 41 | + ) |
| 42 | + }), |
| 43 | +) |
0 commit comments