@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 86 lines 2.93 KB
Raw
1 import { createPhysicalName } from "alchemy/PhysicalName";
2 import { tagRecord } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import { stripInternalLabels } from "../Labels.ts";
5
6 export const DEFAULT_LOCATION = "global";
7 export const MAX_NAME_LENGTH = 63;
8
9 export const lastSegment = (value: string) => {
10 const trimmed = value.replace(/\/+$/, "");
11 const parts = trimmed.split("/");
12 return parts[parts.length - 1] || trimmed;
13 };
14
15 export const rfc1035 = (name: string, fallback: string): string => {
16 let next = name
17 .toLowerCase()
18 .replace(/[^a-z0-9-]/g, "-")
19 .replace(/-+/g, "-")
20 .replace(/^-+/, "")
21 .replace(/-+$/, "");
22 if (!/^[a-z]/.test(next)) {
23 next = `${fallback[0] ?? "n"}${next}`;
24 }
25 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/, "");
26 return next.length > 0 ? next : fallback;
27 };
28
29 export const normalizeLocation = (location: string | undefined, fallback = DEFAULT_LOCATION) =>
30 lastSegment(location ?? fallback).toLowerCase();
31
32 export const parseResourceName = (name: string, collection: string) => {
33 const parts = name.split("/").filter((part) => part.length > 0);
34 const collectionAt = parts.lastIndexOf(collection);
35 const locationsAt = parts.lastIndexOf("locations");
36 const projectsAt = parts.lastIndexOf("projects");
37 return {
38 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
39 location:
40 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : DEFAULT_LOCATION,
41 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
42 };
43 };
44
45 export const resourceName = (project: string, location: string, collection: string, id: string) =>
46 `projects/${project}/locations/${location}/${collection}/${id}`;
47
48 export const parentOf = (project: string, location: string) =>
49 `projects/${project}/locations/${location}`;
50
51 export const toResourcePath = (value: string) => {
52 const trimmed = value.trim();
53 const match = trimmed.match(/projects\/[^/]+\/.+/);
54 return match ? match[0]!.replace(/\/+$/, "") : trimmed;
55 };
56
57 export const toNetworkResource = (project: string, network: string) => {
58 const trimmed = toResourcePath(network);
59 const match = trimmed.match(/projects\/[^/]+\/global\/networks\/[^/]+/);
60 if (match) return match[0]!;
61 if (trimmed.includes("/")) return trimmed;
62 return `projects/${project}/global/networks/${trimmed}`;
63 };
64
65 export const userLabels = (
66 labels: Record<string, string | undefined> | null | undefined,
67 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
68
69 export const toId = (
70 id: string,
71 explicit: string | undefined,
72 existing: string | undefined,
73 fallback: string,
74 ) =>
75 Effect.gen(function* () {
76 if (explicit !== undefined) return explicit;
77 if (existing !== undefined) return existing;
78 return rfc1035(
79 yield* createPhysicalName({
80 id,
81 maxLength: MAX_NAME_LENGTH,
82 lowercase: true,
83 }),
84 fallback,
85 );
86 });