@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 42 lines 1.43 KB
Raw
1 import * as translate from "@distilled.cloud/aws/translate";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Coerce a Translate wire tag list (`{ Key, Value }[]`) into a plain
7 * `Record<string, string>`.
8 */
9 export const toTagRecord = (tags: readonly translate.Tag[] | undefined): Record<string, string> =>
10 Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value] as const));
11
12 /**
13 * Read the observed tags of a Translate resource by ARN. Tag reads are
14 * best-effort — a failure (e.g. a race with deletion) reports no tags.
15 */
16 export const readTranslateTags = Effect.fn(function* (arn: string) {
17 const response = yield* translate
18 .listTagsForResource({ ResourceArn: arn })
19 .pipe(Effect.orElseSucceed(() => undefined));
20 return toTagRecord(response?.Tags);
21 });
22
23 /**
24 * Sync tags on a Translate resource: diff the OBSERVED cloud tags against the
25 * desired set and apply only the delta.
26 */
27 export const syncTranslateTags = Effect.fn(function* (
28 arn: string,
29 desiredTags: Record<string, string>,
30 ) {
31 const observedTags = yield* readTranslateTags(arn);
32 const { removed, upsert } = diffTags(observedTags, desiredTags);
33 if (upsert.length > 0) {
34 yield* translate.tagResource({
35 ResourceArn: arn,
36 Tags: upsert.map((t) => ({ Key: t.Key, Value: t.Value })),
37 });
38 }
39 if (removed.length > 0) {
40 yield* translate.untagResource({ ResourceArn: arn, TagKeys: removed });
41 }
42 });