The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { confirmOrDecline } from "alchemy/Cli/commands/confirm"; |
| 2 | import { envFile, yes } from "alchemy/Cli/commands/flags"; |
| 3 | import { instrumentCommand } from "alchemy/Cli/commands/instrument"; |
| 4 | import * as Interaction from "alchemy/Interaction"; |
| 5 | import * as Command from "effect/cli/Command"; |
| 6 | import * as Flag from "effect/cli/Flag"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Aws from "./Alchemist/index.ts"; |
| 10 | |
| 11 | const awsProfile = Flag.String("aws-profile").pipe( |
| 12 | Flag.withDescription("AWS CLI/SSO profile to use for bootstrap credentials"), |
| 13 | Flag.optional, |
| 14 | Flag.map(Option.getOrElse(() => "default")), |
| 15 | ); |
| 16 | |
| 17 | const awsRegion = Flag.String("region").pipe( |
| 18 | Flag.withDescription("AWS region to bootstrap (defaults to AWS_REGION env var)"), |
| 19 | Flag.optional, |
| 20 | Flag.map(Option.getOrUndefined), |
| 21 | ); |
| 22 | |
| 23 | const runBootstrap = Effect.fn(function* (args: { |
| 24 | envFile: Option.Option<string>; |
| 25 | profile: string; |
| 26 | region: string | undefined; |
| 27 | destroy: boolean; |
| 28 | }) { |
| 29 | const { envFile, profile, region, destroy } = args; |
| 30 | const prompt = yield* Interaction.Interaction; |
| 31 | const target = { |
| 32 | profile, |
| 33 | region, |
| 34 | envFile: Option.getOrUndefined(envFile), |
| 35 | }; |
| 36 | if (destroy) { |
| 37 | const result = yield* Aws.teardown(target); |
| 38 | yield* result.destroyed.length === 0 |
| 39 | ? prompt.output.success("No bootstrap buckets found to destroy") |
| 40 | : prompt.output.success( |
| 41 | `Destroyed ${result.destroyed.length} bootstrap bucket(s): ${result.destroyed.join(", ")}`, |
| 42 | ); |
| 43 | return; |
| 44 | } |
| 45 | const result = yield* Aws.bootstrap(target); |
| 46 | yield* result.created |
| 47 | ? prompt.output.success(`Created assets bucket: ${result.bucketName}`) |
| 48 | : prompt.output.success(`Assets bucket already exists: ${result.bucketName}`); |
| 49 | }); |
| 50 | |
| 51 | const teardownCommand = Command.make( |
| 52 | "teardown", |
| 53 | { envFile, profile: awsProfile, region: awsRegion, yes }, |
| 54 | instrumentCommand( |
| 55 | "provider.aws.teardown", |
| 56 | (a: { profile: string; region: string | undefined }) => ({ |
| 57 | "aws.profile": a.profile, |
| 58 | "alchemy.region": a.region ?? "", |
| 59 | }), |
| 60 | )( |
| 61 | Effect.fn(function* ({ yes: approved, ...args }) { |
| 62 | yield* confirmOrDecline({ |
| 63 | yes: approved, |
| 64 | message: "Destroy every Alchemy bootstrap bucket in this AWS region?", |
| 65 | confirmLabel: "Destroy", |
| 66 | cancelLabel: "Cancel", |
| 67 | }); |
| 68 | yield* runBootstrap({ ...args, destroy: true }); |
| 69 | }), |
| 70 | ), |
| 71 | ).pipe(Command.withDescription("Destroy the AWS deployment assets buckets")); |
| 72 | |
| 73 | const bootstrapCommand = Command.make( |
| 74 | "bootstrap", |
| 75 | { envFile, profile: awsProfile, region: awsRegion }, |
| 76 | instrumentCommand( |
| 77 | "provider.aws.bootstrap", |
| 78 | (a: { profile: string; region: string | undefined }) => ({ |
| 79 | "aws.profile": a.profile, |
| 80 | "alchemy.region": a.region ?? "", |
| 81 | }), |
| 82 | )( |
| 83 | Effect.fn(function* (args) { |
| 84 | yield* runBootstrap({ ...args, destroy: false }); |
| 85 | }), |
| 86 | ), |
| 87 | ).pipe(Command.withDescription("Provision the AWS deployment assets bucket")); |
| 88 | |
| 89 | export const awsCommand = Command.make("aws", {}).pipe( |
| 90 | Command.withDescription("Manage AWS provider prerequisites"), |
| 91 | Command.withSubcommands([bootstrapCommand, teardownCommand]), |
| 92 | ); |