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

main
ts 57 lines 1.99 KB
Raw
1 import * as codeconnections from "@distilled.cloud/aws/codeconnections";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Shared tag plumbing for the CodeConnections resources (Connection, Host,
7 * RepositoryLink). All three share the same `TagResource`/`UntagResource`/
8 * `ListTagsForResource` wire shape keyed by ARN.
9 *
10 * NOT exported from `index.ts`.
11 */
12
13 /** Convert a CodeConnections wire tag list into a plain record. */
14 export const toTagRecord = (
15 tags: ReadonlyArray<{ Key?: string; Value?: string }> | undefined,
16 ): Record<string, string> =>
17 Object.fromEntries(
18 (tags ?? [])
19 .filter(
20 (tag): tag is { Key: string; Value: string } =>
21 typeof tag.Key === "string" && typeof tag.Value === "string",
22 )
23 .map((tag) => [tag.Key, tag.Value]),
24 );
25
26 /** Convert a plain record into the CodeConnections wire tag list. */
27 export const toTagList = (tags: Record<string, string>): { Key: string; Value: string }[] =>
28 Object.entries(tags).map(([Key, Value]) => ({ Key, Value }));
29
30 /** Read the observed cloud tags for a CodeConnections resource ARN. */
31 export const fetchObservedTags = Effect.fn(function* (arn: string) {
32 return yield* codeconnections.listTagsForResource({ ResourceArn: arn }).pipe(
33 Effect.map((res) => toTagRecord(res.Tags)),
34 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed({})),
35 );
36 });
37
38 /**
39 * Converge a CodeConnections resource's tags to `desiredTags`, diffing
40 * against the OBSERVED cloud tags (adoption may bring foreign tags).
41 */
42 export const syncResourceTags = Effect.fn(function* (
43 arn: string,
44 desiredTags: Record<string, string>,
45 ) {
46 const observed = yield* fetchObservedTags(arn);
47 const { removed, upsert } = diffTags(observed, desiredTags);
48 if (upsert.length > 0) {
49 yield* codeconnections.tagResource({ ResourceArn: arn, Tags: upsert });
50 }
51 if (removed.length > 0) {
52 yield* codeconnections.untagResource({
53 ResourceArn: arn,
54 TagKeys: removed,
55 });
56 }
57 });