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

main
ts 47 lines 1.48 KB
Raw
1 import * as appflow from "@distilled.cloud/aws/appflow";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Coerce an AppFlow wire tag map (`Record<string, string | undefined>`) into
7 * a plain `Record<string, string>`, dropping undefined values.
8 */
9 export const toTagRecord = (
10 tags: { [key: string]: string | undefined } | undefined,
11 ): Record<string, string> =>
12 Object.fromEntries(
13 Object.entries(tags ?? {}).filter(
14 (entry): entry is [string, string] => typeof entry[1] === "string",
15 ),
16 );
17
18 /**
19 * Read the observed tags of an AppFlow resource. Tag reads are best-effort.
20 */
21 export const readAppFlowTags = Effect.fn(function* (arn: string) {
22 const response = yield* appflow
23 .listTagsForResource({ resourceArn: arn })
24 .pipe(Effect.orElseSucceed(() => undefined));
25 return toTagRecord(response?.tags);
26 });
27
28 /**
29 * Sync tags on an AppFlow resource: diff the OBSERVED cloud tags against the
30 * desired set and apply only the delta.
31 */
32 export const syncAppFlowTags = Effect.fn(function* (
33 arn: string,
34 desiredTags: Record<string, string>,
35 ) {
36 const observedTags = yield* readAppFlowTags(arn);
37 const { removed, upsert } = diffTags(observedTags, desiredTags);
38 if (upsert.length > 0) {
39 yield* appflow.tagResource({
40 resourceArn: arn,
41 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
42 });
43 }
44 if (removed.length > 0) {
45 yield* appflow.untagResource({ resourceArn: arn, tagKeys: removed });
46 }
47 });