@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 105 lines 2.91 KB
Raw
1 import * as Command from "effect/cli/Command";
2 import * as Flag from "effect/cli/Flag";
3 import * as Effect from "effect/Effect";
4 import * as Option from "effect/Option";
5 import * as Alchemist from "../../Alchemist/index.ts";
6 import * as Interaction from "../../Interaction.ts";
7 import { Cli } from "../../Report.ts";
8 import { renderApply } from "../../Report.ts";
9 import { planDecisionScreen } from "../components/view/PlanDecision.tsx";
10 import { config, envFile, profile, resolveStage, stage } from "./flags.ts";
11 import { instrumentCommand } from "./instrument.ts";
12 import { renderPlanning } from "./render.ts";
13
14 const repairFlag = Flag.Boolean("repair").pipe(
15 Flag.withDescription("Repair detected drift without prompting"),
16 Flag.withDefault(false),
17 );
18
19 interface SyncArgs {
20 main: string;
21 stage: string;
22 envFile: Option.Option<string>;
23 profile?: string;
24 repair?: boolean;
25 }
26
27 const routeDrift = Effect.fn(function* ({
28 main,
29 stage,
30 envFile,
31 profile,
32 repair = false,
33 }: SyncArgs) {
34 const cli = yield* Cli;
35 const snapshot = yield* Alchemist.importStack(main).pipe(
36 Effect.flatMap((stack) =>
37 Alchemist.Drift.scan({
38 stack,
39 stage,
40 profile,
41 envFile: Option.getOrUndefined(envFile),
42 }),
43 ),
44 renderPlanning({
45 operation: "Drift",
46 stage,
47 computingLabel: "Checking drift",
48 readyLabel: "Drift check complete",
49 }),
50 );
51 if (!Alchemist.Drift.hasDrift(snapshot)) {
52 return yield* cli.displayPlan(snapshot.native);
53 }
54
55 if (!repair) {
56 const terminal = yield* Interaction.Interaction;
57 if (!terminal.terminal.input) {
58 return yield* cli.displayPlan(snapshot.native);
59 }
60 const decision = yield* terminal.prompt
61 .screen(
62 planDecisionScreen({
63 plan: snapshot.native,
64 message: "Drift detected",
65 choices: [
66 {
67 value: "repair" as const,
68 label: "Repair",
69 },
70 {
71 value: "cancel" as const,
72 label: "Cancel",
73 },
74 ],
75 initialValue: "cancel" as const,
76 }),
77 )
78 .pipe(Effect.catchTag("TerminalCancelled", () => Effect.succeed("cancel" as const)));
79 if (decision === "cancel") return;
80 }
81
82 yield* Alchemist.Drift.execute(snapshot).pipe(renderApply(snapshot.native));
83 });
84
85 export const driftCommand = Command.make(
86 "drift",
87 {
88 repair: repairFlag,
89 main: config,
90 envFile,
91 stage,
92 profile,
93 },
94 (args) =>
95 resolveStage("live", args.stage, args.envFile).pipe(
96 Effect.flatMap((stage) =>
97 instrumentCommand("drift", (a: SyncArgs & { repair: boolean }) => ({
98 "alchemy.stage": a.stage,
99 "alchemy.profile": a.profile,
100 "alchemy.main": a.main,
101 "alchemy.repair": a.repair,
102 }))((resolved) => routeDrift(resolved))({ ...args, stage }),
103 ),
104 ),
105 ).pipe(Command.withDescription("Detect infrastructure drift"));