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