The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { movedCommand } from "alchemy/Cli/commands/compat"; |
| 2 | import { confirmOrDecline } from "alchemy/Cli/commands/confirm"; |
| 3 | import { envFile, yes } from "alchemy/Cli/commands/flags"; |
| 4 | import { instrumentCommand } from "alchemy/Cli/commands/instrument"; |
| 5 | import * as Interaction from "alchemy/Interaction"; |
| 6 | import { loadConfigProvider } from "alchemy/Util/ConfigProvider"; |
| 7 | import { Command, Flag } from "effect/cli"; |
| 8 | import * as Config from "effect/Config"; |
| 9 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 10 | import * as Console from "effect/Console"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Option from "effect/Option"; |
| 13 | import * as Redacted from "effect/Redacted"; |
| 14 | import * as CloudflareToken from "./Alchemist/Token.ts"; |
| 15 | import type { CreatedToken } from "./Alchemist/Token.ts"; |
| 16 | |
| 17 | const SELECTABLE_SCOPE_LABELS: Record<string, string> = { |
| 18 | "com.cloudflare.api.account": "account", |
| 19 | "com.cloudflare.api.account.zone": "zone", |
| 20 | "com.cloudflare.api.user": "user", |
| 21 | "com.cloudflare.edge.r2.bucket": "r2", |
| 22 | }; |
| 23 | |
| 24 | export const formatCreatedCloudflareToken = (result: CreatedToken, token: string) => |
| 25 | [ |
| 26 | "", |
| 27 | `Created Cloudflare API token "${result.name}" (${result.id}).`, |
| 28 | `Granted ${result.grantedPermissionGroups} permission group(s) across ${result.policies.length} policy(ies)${ |
| 29 | result.verificationStatus ? `; token status: ${result.verificationStatus}.` : "." |
| 30 | }`, |
| 31 | "", |
| 32 | token, |
| 33 | "", |
| 34 | "Store this value now — Cloudflare only shows it once. Use it as CLOUDFLARE_API_TOKEN.", |
| 35 | ...result.diagnostics.map( |
| 36 | (diagnostic) => `${diagnostic.severity.toUpperCase()}: ${diagnostic.message}`, |
| 37 | ), |
| 38 | ].join("\n"); |
| 39 | |
| 40 | const allPermissionsFlag = Flag.Boolean("all-permissions").pipe( |
| 41 | Flag.withDescription( |
| 42 | "Grant the token EVERY Cloudflare permission group (a 'god token'). " + |
| 43 | "Use with care — it has full access to your account.", |
| 44 | ), |
| 45 | Flag.withDefault(false), |
| 46 | ); |
| 47 | |
| 48 | const tokenNameFlag = Flag.String("name").pipe( |
| 49 | Flag.withDescription( |
| 50 | "Name for the API token. Defaults to 'alchemy' (or 'alchemy-all-permissions').", |
| 51 | ), |
| 52 | Flag.optional, |
| 53 | Flag.map(Option.getOrUndefined), |
| 54 | ); |
| 55 | |
| 56 | const tokenAccountIdFlag = Flag.String("account-id").pipe( |
| 57 | Flag.withDescription( |
| 58 | "Cloudflare account ID(s) to scope the token to (comma-separated for " + |
| 59 | "multiple). If omitted, you'll be prompted to select from your accounts.", |
| 60 | ), |
| 61 | Flag.optional, |
| 62 | Flag.map( |
| 63 | Option.match({ |
| 64 | onNone: () => undefined, |
| 65 | onSome: (v) => |
| 66 | v |
| 67 | .split(",") |
| 68 | .map((s) => s.trim()) |
| 69 | .filter((s) => s.length > 0), |
| 70 | }), |
| 71 | ), |
| 72 | ); |
| 73 | |
| 74 | /** |
| 75 | * `alchemy provider cloudflare token` — mint a Cloudflare API token |
| 76 | * (`POST /user/tokens`). |
| 77 | * |
| 78 | * This command is **standalone**: it does not use an Alchemy auth profile. |
| 79 | * Cloudflare only mints a token whose permissions the authenticating |
| 80 | * credential is allowed to grant — and OAuth/scoped tokens silently produce a |
| 81 | * token with zero permissions — so it always authenticates with the account's |
| 82 | * **Global API Key** (read from `CLOUDFLARE_API_KEY` / `CLOUDFLARE_EMAIL`, |
| 83 | * otherwise prompted). The key is used only to create the token and is never |
| 84 | * stored. |
| 85 | * |
| 86 | * With `--all-permissions` it builds a "superuser" token spanning every |
| 87 | * permission group (after a confirmation prompt). Otherwise it prompts the |
| 88 | * user to pick which permission groups to grant from the account's live set. |
| 89 | * |
| 90 | * The token can be scoped to more than one account: pass a comma-separated |
| 91 | * list to `--account-id`, or (when neither is supplied) pick multiple accounts |
| 92 | * from the interactive selection prompt. |
| 93 | */ |
| 94 | const createTokenCommand = Command.make( |
| 95 | "token", |
| 96 | { |
| 97 | envFile, |
| 98 | allPermissions: allPermissionsFlag, |
| 99 | name: tokenNameFlag, |
| 100 | accountId: tokenAccountIdFlag, |
| 101 | yes, |
| 102 | }, |
| 103 | instrumentCommand("provider.cloudflare.create-token", (a: { allPermissions: boolean }) => ({ |
| 104 | "alchemy.all_permissions": a.allPermissions, |
| 105 | }))( |
| 106 | Effect.fn(function* ({ envFile, allPermissions, name, accountId, yes: approved }) { |
| 107 | const prompt = yield* Interaction.Interaction; |
| 108 | const provider = yield* loadConfigProvider(envFile); |
| 109 | const read = <A>(config: Config.Config<Option.Option<A>>) => |
| 110 | config.pipe( |
| 111 | Effect.provide(ConfigProvider.layer(provider)), |
| 112 | Effect.map(Option.getOrUndefined), |
| 113 | ); |
| 114 | const apiKey = |
| 115 | (yield* read(Config.String("CLOUDFLARE_API_KEY").pipe(Config.option))) ?? |
| 116 | (yield* prompt.prompt.password({ |
| 117 | message: "Paste your Global API Key", |
| 118 | // Keep the URL free of adjacent punctuation: terminals that |
| 119 | // auto-link URLs otherwise fold a trailing ")" into the link. |
| 120 | description: |
| 121 | "Find it under Global API Key at https://dash.cloudflare.com/profile/api-tokens", |
| 122 | validate: (value) => (value.trim().length === 0 ? "Required" : undefined), |
| 123 | })); |
| 124 | const email = |
| 125 | (yield* read(Config.String("CLOUDFLARE_EMAIL").pipe(Config.option))) ?? |
| 126 | (yield* prompt.prompt.text({ |
| 127 | message: "Cloudflare account email", |
| 128 | validate: (value) => (value.trim().length === 0 ? "Required" : undefined), |
| 129 | })); |
| 130 | const credentials = { email, apiKey: Redacted.make(apiKey) }; |
| 131 | const catalog = yield* CloudflareToken.catalog(credentials); |
| 132 | const resolvedAccountIds = |
| 133 | accountId ?? |
| 134 | (catalog.accounts.length === 1 |
| 135 | ? [catalog.accounts[0]!.id] |
| 136 | : yield* prompt.prompt.multiSelect<string>({ |
| 137 | message: "Select the Cloudflare accounts to scope the token to", |
| 138 | searchable: true, |
| 139 | options: catalog.accounts.map((account) => ({ |
| 140 | value: account.id, |
| 141 | label: account.name, |
| 142 | description: account.id, |
| 143 | })), |
| 144 | required: true, |
| 145 | })); |
| 146 | const tokenName = |
| 147 | name ?? |
| 148 | (yield* prompt.prompt.text({ |
| 149 | message: "Token name", |
| 150 | defaultValue: allPermissions ? "alchemy-superuser" : "alchemy", |
| 151 | validate: (value) => (value.trim().length === 0 ? "Token name is required" : undefined), |
| 152 | })); |
| 153 | const permissionGroupIds = allPermissions |
| 154 | ? ("all" as const) |
| 155 | : yield* prompt.prompt.multiSelect<string>({ |
| 156 | message: "Select the permission groups to grant", |
| 157 | descriptionPlacement: "inline", |
| 158 | options: catalog.permissionGroups |
| 159 | .filter(({ selectable }) => selectable) |
| 160 | .sort( |
| 161 | (a, b) => |
| 162 | (a.category ?? "").localeCompare(b.category ?? "") || |
| 163 | a.name.localeCompare(b.name), |
| 164 | ) |
| 165 | .map((group) => ({ |
| 166 | value: group.id, |
| 167 | label: group.name, |
| 168 | description: SELECTABLE_SCOPE_LABELS[group.scopes[0]!] ?? group.scopes[0], |
| 169 | })), |
| 170 | required: true, |
| 171 | }); |
| 172 | const plan = yield* CloudflareToken.plan({ |
| 173 | credentials, |
| 174 | name: tokenName, |
| 175 | accountIds: resolvedAccountIds, |
| 176 | permissionGroupIds, |
| 177 | }); |
| 178 | if (plan.grantsFullAccess) { |
| 179 | yield* prompt.output.warning( |
| 180 | "This token will have FULL access to your Cloudflare account. Keep it secret.", |
| 181 | ); |
| 182 | // `--yes` is the non-interactive path: without it, a CI invocation |
| 183 | // of `--all-permissions` dies on the confirm prompt. |
| 184 | yield* confirmOrDecline({ |
| 185 | yes: approved, |
| 186 | message: "Create a superuser token with all permissions?", |
| 187 | confirmLabel: "Create", |
| 188 | cancelLabel: "Cancel", |
| 189 | abortMessage: "Cancelled.", |
| 190 | }); |
| 191 | } |
| 192 | const result = yield* CloudflareToken.create({ |
| 193 | credentials, |
| 194 | plan, |
| 195 | }); |
| 196 | yield* Console.log(formatCreatedCloudflareToken(result, Redacted.value(result.value))); |
| 197 | }), |
| 198 | ), |
| 199 | ).pipe(Command.withDescription("Create a scoped Cloudflare API token")); |
| 200 | |
| 201 | const moved = (name: string, replacement: string) => |
| 202 | movedCommand(name, `alchemy provider cloudflare ${name}`, replacement); |
| 203 | |
| 204 | export const cloudflareCommand = Command.make("cloudflare", {}).pipe( |
| 205 | Command.withDescription("Manage Cloudflare provider prerequisites"), |
| 206 | Command.withSubcommands([ |
| 207 | createTokenCommand, |
| 208 | moved("bootstrap", "alchemy state bootstrap --backend cloudflare"), |
| 209 | moved("teardown", "alchemy state teardown --backend cloudflare"), |
| 210 | Command.make("state", {}).pipe( |
| 211 | Command.withSubcommands([ |
| 212 | movedCommand( |
| 213 | "logs", |
| 214 | "alchemy provider cloudflare state logs", |
| 215 | "alchemy state logs --backend cloudflare", |
| 216 | ), |
| 217 | ]), |
| 218 | Command.unlisted, |
| 219 | ), |
| 220 | ]), |
| 221 | ); |