@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, 04:45 UTC

main
ts 58 lines 1.94 KB
Raw
1 import * as redshiftserverless from "@distilled.cloud/aws/redshift-serverless";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Convert a Redshift Serverless wire tag list (`{ key, value }`) into a plain
7 * record, dropping malformed entries.
8 */
9 export const toTagRecord = (
10 tags: ReadonlyArray<{ key?: string; value?: string }> | undefined,
11 ): Record<string, string> =>
12 Object.fromEntries(
13 (tags ?? [])
14 .filter(
15 (tag): tag is { key: string; value: string } =>
16 typeof tag.key === "string" && typeof tag.value === "string",
17 )
18 .map((tag) => [tag.key, tag.value]),
19 );
20
21 /**
22 * Convert a desired tag record into the wire tag list (`{ key, value }`) used
23 * by create/tag calls.
24 */
25 export const toWireTags = (tags: Record<string, string>): redshiftserverless.Tag[] =>
26 Object.entries(tags).map(([key, value]) => ({ key, value }));
27
28 /**
29 * Read the observed tags of a Redshift Serverless resource. Tag reads are
30 * best-effort — a failure (e.g. a race with deletion) reports no tags.
31 */
32 export const readTags = Effect.fn(function* (arn: string) {
33 const response = yield* redshiftserverless
34 .listTagsForResource({ resourceArn: arn })
35 .pipe(Effect.orElseSucceed(() => undefined));
36 return toTagRecord(response?.tags);
37 });
38
39 /**
40 * Sync tags on a Redshift Serverless resource: diff the OBSERVED cloud tags
41 * against the desired set and apply only the delta.
42 */
43 export const syncTags = Effect.fn(function* (arn: string, desiredTags: Record<string, string>) {
44 const observedTags = yield* readTags(arn);
45 const { removed, upsert } = diffTags(observedTags, desiredTags);
46 if (upsert.length > 0) {
47 yield* redshiftserverless.tagResource({
48 resourceArn: arn,
49 tags: upsert.map(({ Key, Value }) => ({ key: Key, value: Value })),
50 });
51 }
52 if (removed.length > 0) {
53 yield* redshiftserverless.untagResource({
54 resourceArn: arn,
55 tagKeys: removed,
56 });
57 }
58 });