@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 47 lines 1.69 KB
Raw
1 import * as mediaconvert from "@distilled.cloud/aws/mediaconvert";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Coerce a MediaConvert wire tag map (values are `string | undefined`) into a
7 * plain `Record<string, string>`, dropping any 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 a MediaConvert resource by ARN. Tag reads are
20 * best-effort — a failure (e.g. a race with deletion) reports no tags.
21 */
22 export const readMcTags = Effect.fn(function* (arn: string) {
23 const response = yield* mediaconvert
24 .listTagsForResource({ Arn: arn })
25 .pipe(Effect.orElseSucceed(() => undefined));
26 return toTagRecord(response?.ResourceTags?.Tags);
27 });
28
29 /**
30 * Sync tags on a MediaConvert resource: diff the OBSERVED cloud tags against
31 * the desired set and apply only the delta. MediaConvert's `tagResource`
32 * takes a tag map keyed by ARN in the body; `untagResource` takes an ARN path
33 * label plus the keys to remove.
34 */
35 export const syncMcTags = Effect.fn(function* (arn: string, desiredTags: Record<string, string>) {
36 const observedTags = yield* readMcTags(arn);
37 const { removed, upsert } = diffTags(observedTags, desiredTags);
38 if (upsert.length > 0) {
39 yield* mediaconvert.tagResource({
40 Arn: arn,
41 Tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
42 });
43 }
44 if (removed.length > 0) {
45 yield* mediaconvert.untagResource({ Arn: arn, TagKeys: removed });
46 }
47 });