import { createPhysicalName } from "alchemy/PhysicalName";
import * as Effect from "effect/Effect";
import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
export const MAX_ID_LENGTH = 63;
export const MAX_DISPLAY_NAME_LENGTH = 128;
export const lastSegment = (value: string) => {
const trimmed = value.replace(/\/+$/, "");
const parts = trimmed.split("/");
return parts[parts.length - 1] || trimmed;
};
export const locationOf = (name: string, fallback = "") => {
const parts = name.split("/");
const index = parts.indexOf("locations");
return index >= 0 ? (parts[index + 1] ?? fallback) : fallback;
};
export const parentOf = (name: string) => {
const parts = name.split("/").filter((part) => part.length > 0);
return parts.slice(0, -2).join("/");
};
export const locationParent = (project: string, location: string) =>
`projects/${project}/locations/${location}`;
export const toResourceId = (
id: string,
requested: string | undefined,
existing: string | undefined,
maxLength = MAX_ID_LENGTH,
) =>
Effect.gen(function* () {
if (requested !== undefined) return requested;
if (existing !== undefined) return existing;
const generated = yield* createPhysicalName({
id,
maxLength,
lowercase: true,
});
return /^[a-z]/.test(generated) ? generated : `a${generated}`.slice(0, maxLength);
});
const markerOf = (labels: Record<string, string>) =>
`[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
export const encodeOwnership = (
labels: Record<string, string>,
text: string | undefined,
): string => {
const marker = markerOf(labels);
return text ? `${marker}\n${text}` : marker;
};
export const encodeOwnershipLine = (
labels: Record<string, string>,
text: string | undefined,
maxLength = MAX_DISPLAY_NAME_LENGTH,
): string => {
const marker = markerOf(labels);
if (!text) return marker.slice(0, maxLength);
return `${marker} ${text}`.slice(0, maxLength);
};
export const parseOwnership = (
text: string | undefined,
): {
labels: Record<string, string>;
text: string | undefined;
} => {
if (!text?.startsWith("[alchemy ")) {
return { labels: {}, text };
}
const end = text.indexOf("]");
if (end < 0) return { labels: {}, text };
const labels: Record<string, string> = {};
for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
const eq = part.indexOf("=");
if (eq > 0) {
labels[part.slice(0, eq)] = part.slice(eq + 1);
}
}
const rest = text.slice(end + 1).replace(/^[\s\n]+/, "");
return { labels, text: rest.length > 0 ? rest : undefined };
};
export const hasOwnershipMarker = (text: string | undefined) =>
Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));
export const ownedByAlchemy = (id: string, text: string | undefined) =>
Effect.gen(function* () {
const { labels } = parseOwnership(text);
return yield* hasAlchemyLabels(id, labels);
});
export const internalLabels = (id: string) => createInternalLabels(id);