refactor: 抽取 YAML field reader factory
This commit is contained in:
@@ -19,24 +19,7 @@ import {
|
|||||||
type FrpcSecretMaterial,
|
type FrpcSecretMaterial,
|
||||||
} from "./platform-infra-public-service";
|
} from "./platform-infra-public-service";
|
||||||
import {
|
import {
|
||||||
yamlAbsolutePathField,
|
createYamlFieldReader,
|
||||||
yamlApiPathField,
|
|
||||||
yamlArrayOfRecords,
|
|
||||||
yamlBooleanField,
|
|
||||||
yamlEnumField,
|
|
||||||
yamlEnvKeyField,
|
|
||||||
yamlHostField,
|
|
||||||
yamlHttpsUrlField,
|
|
||||||
yamlIntegerArrayField,
|
|
||||||
yamlIntegerField,
|
|
||||||
yamlKubernetesNameField,
|
|
||||||
yamlObjectField,
|
|
||||||
yamlPgIdentifierField,
|
|
||||||
yamlPortField,
|
|
||||||
yamlRecord,
|
|
||||||
yamlSourceRefField,
|
|
||||||
yamlStringArrayField,
|
|
||||||
yamlStringField,
|
|
||||||
} from "./platform-infra-ops-library";
|
} from "./platform-infra-ops-library";
|
||||||
import { fingerprintSecretValues, parseEnvFile, readEnvSourceFile, readTextFile, redactRepoPath, requiredEnvValue } from "./secrets";
|
import { fingerprintSecretValues, parseEnvFile, readEnvSourceFile, readTextFile, redactRepoPath, requiredEnvValue } from "./secrets";
|
||||||
|
|
||||||
@@ -45,6 +28,26 @@ const configLabel = "config/platform-infra/langbot.yaml";
|
|||||||
const serviceName = "langbot";
|
const serviceName = "langbot";
|
||||||
const pluginRuntimeServiceName = "langbot-plugin-runtime";
|
const pluginRuntimeServiceName = "langbot-plugin-runtime";
|
||||||
const fieldManager = "unidesk-platform-langbot";
|
const fieldManager = "unidesk-platform-langbot";
|
||||||
|
const {
|
||||||
|
asRecord,
|
||||||
|
objectField,
|
||||||
|
arrayOfRecords,
|
||||||
|
stringField,
|
||||||
|
integerField,
|
||||||
|
booleanField,
|
||||||
|
stringArrayField,
|
||||||
|
numberArrayField,
|
||||||
|
enumField,
|
||||||
|
kubernetesNameField,
|
||||||
|
sourceRefField,
|
||||||
|
envKeyField,
|
||||||
|
pgIdentifierField,
|
||||||
|
hostField,
|
||||||
|
portField,
|
||||||
|
absolutePathField,
|
||||||
|
apiPathField,
|
||||||
|
httpsUrlField,
|
||||||
|
} = createYamlFieldReader(configLabel);
|
||||||
|
|
||||||
interface LangBotConfig {
|
interface LangBotConfig {
|
||||||
version: number;
|
version: number;
|
||||||
@@ -1461,78 +1464,6 @@ function boolField(value: Record<string, unknown> | null, key: string, fallback:
|
|||||||
return typeof value?.[key] === "boolean" ? value[key] : fallback;
|
return typeof value?.[key] === "boolean" ? value[key] : fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
function asRecord(value: unknown, path: string): Record<string, unknown> {
|
|
||||||
return yamlRecord(value, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function objectField(obj: Record<string, unknown>, key: string, path: string): Record<string, unknown> {
|
|
||||||
return yamlObjectField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function arrayOfRecords(value: unknown, path: string): Record<string, unknown>[] {
|
|
||||||
return yamlArrayOfRecords(value, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlStringField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function integerField(obj: Record<string, unknown>, key: string, path: string): number {
|
|
||||||
return yamlIntegerField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function booleanField(obj: Record<string, unknown>, key: string, path: string): boolean {
|
|
||||||
return yamlBooleanField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringArrayField(obj: Record<string, unknown>, key: string, path: string): string[] {
|
|
||||||
return yamlStringArrayField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function numberArrayField(obj: Record<string, unknown>, key: string, path: string): number[] {
|
|
||||||
return yamlIntegerArrayField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function enumField<const T extends readonly string[]>(obj: Record<string, unknown>, key: string, path: string, values: T): T[number] {
|
|
||||||
return yamlEnumField(obj, key, configLabel, path, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
function kubernetesNameField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlKubernetesNameField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function sourceRefField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlSourceRefField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function envKeyField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlEnvKeyField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function pgIdentifierField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlPgIdentifierField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function hostField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlHostField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function portField(obj: Record<string, unknown>, key: string, path: string): number {
|
|
||||||
return yamlPortField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function absolutePathField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlAbsolutePathField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function apiPathField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlApiPathField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function httpsUrlField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlHttpsUrlField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function pvcSpec(record: Record<string, unknown>, path: string): PvcSpec {
|
function pvcSpec(record: Record<string, unknown>, path: string): PvcSpec {
|
||||||
const name = kubernetesNameField(record, "name", path);
|
const name = kubernetesNameField(record, "name", path);
|
||||||
const size = stringField(record, "size", path);
|
const size = stringField(record, "size", path);
|
||||||
|
|||||||
@@ -21,23 +21,7 @@ import {
|
|||||||
type PublicServiceTarget,
|
type PublicServiceTarget,
|
||||||
} from "./platform-infra-public-service";
|
} from "./platform-infra-public-service";
|
||||||
import {
|
import {
|
||||||
yamlAbsolutePathField,
|
createYamlFieldReader,
|
||||||
yamlArrayOfRecords,
|
|
||||||
yamlBooleanField,
|
|
||||||
yamlEnumField,
|
|
||||||
yamlEnvKeyField,
|
|
||||||
yamlHostField,
|
|
||||||
yamlHttpsUrlField,
|
|
||||||
yamlIntegerArrayField,
|
|
||||||
yamlIntegerField,
|
|
||||||
yamlKubernetesNameField,
|
|
||||||
yamlObjectField,
|
|
||||||
yamlPgIdentifierField,
|
|
||||||
yamlPortField,
|
|
||||||
yamlRecord,
|
|
||||||
yamlSourceRefField,
|
|
||||||
yamlStringArrayField,
|
|
||||||
yamlStringField,
|
|
||||||
} from "./platform-infra-ops-library";
|
} from "./platform-infra-ops-library";
|
||||||
import { fingerprintSecretValues, parseEnvFile, readEnvSourceFile, readTextFile, redactRepoPath, requiredEnvValue } from "./secrets";
|
import { fingerprintSecretValues, parseEnvFile, readEnvSourceFile, readTextFile, redactRepoPath, requiredEnvValue } from "./secrets";
|
||||||
|
|
||||||
@@ -45,6 +29,25 @@ const configFile = rootPath("config", "platform-infra", "n8n.yaml");
|
|||||||
const configLabel = "config/platform-infra/n8n.yaml";
|
const configLabel = "config/platform-infra/n8n.yaml";
|
||||||
const serviceName = "n8n";
|
const serviceName = "n8n";
|
||||||
const fieldManager = "unidesk-platform-n8n";
|
const fieldManager = "unidesk-platform-n8n";
|
||||||
|
const {
|
||||||
|
asRecord,
|
||||||
|
objectField,
|
||||||
|
arrayOfRecords,
|
||||||
|
stringField,
|
||||||
|
integerField,
|
||||||
|
booleanField,
|
||||||
|
stringArrayField,
|
||||||
|
numberArrayField,
|
||||||
|
enumField,
|
||||||
|
kubernetesNameField,
|
||||||
|
sourceRefField,
|
||||||
|
envKeyField,
|
||||||
|
pgIdentifierField,
|
||||||
|
hostField,
|
||||||
|
portField,
|
||||||
|
absolutePathField,
|
||||||
|
httpsUrlField,
|
||||||
|
} = createYamlFieldReader(configLabel);
|
||||||
|
|
||||||
interface N8nConfig {
|
interface N8nConfig {
|
||||||
version: number;
|
version: number;
|
||||||
@@ -1065,74 +1068,6 @@ function boolField(value: Record<string, unknown> | null, key: string, fallback:
|
|||||||
return typeof value?.[key] === "boolean" ? value[key] : fallback;
|
return typeof value?.[key] === "boolean" ? value[key] : fallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
function asRecord(value: unknown, path: string): Record<string, unknown> {
|
|
||||||
return yamlRecord(value, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function objectField(obj: Record<string, unknown>, key: string, path: string): Record<string, unknown> {
|
|
||||||
return yamlObjectField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function arrayOfRecords(value: unknown, path: string): Record<string, unknown>[] {
|
|
||||||
return yamlArrayOfRecords(value, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlStringField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function integerField(obj: Record<string, unknown>, key: string, path: string): number {
|
|
||||||
return yamlIntegerField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function booleanField(obj: Record<string, unknown>, key: string, path: string): boolean {
|
|
||||||
return yamlBooleanField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function stringArrayField(obj: Record<string, unknown>, key: string, path: string): string[] {
|
|
||||||
return yamlStringArrayField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function numberArrayField(obj: Record<string, unknown>, key: string, path: string): number[] {
|
|
||||||
return yamlIntegerArrayField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function enumField<const T extends readonly string[]>(obj: Record<string, unknown>, key: string, path: string, values: T): T[number] {
|
|
||||||
return yamlEnumField(obj, key, configLabel, path, values);
|
|
||||||
}
|
|
||||||
|
|
||||||
function kubernetesNameField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlKubernetesNameField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function sourceRefField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlSourceRefField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function envKeyField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlEnvKeyField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function pgIdentifierField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlPgIdentifierField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function hostField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlHostField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function portField(obj: Record<string, unknown>, key: string, path: string): number {
|
|
||||||
return yamlPortField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function absolutePathField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlAbsolutePathField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function httpsUrlField(obj: Record<string, unknown>, key: string, path: string): string {
|
|
||||||
return yamlHttpsUrlField(obj, key, configLabel, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
function pvcSpec(record: Record<string, unknown>, path: string): PvcSpec {
|
function pvcSpec(record: Record<string, unknown>, path: string): PvcSpec {
|
||||||
const name = kubernetesNameField(record, "name", path);
|
const name = kubernetesNameField(record, "name", path);
|
||||||
const size = stringField(record, "size", path);
|
const size = stringField(record, "size", path);
|
||||||
|
|||||||
@@ -294,6 +294,50 @@ export function yamlHttpsUrlField(obj: Record<string, unknown>, key: string, con
|
|||||||
return value.replace(/\/+$/u, "");
|
return value.replace(/\/+$/u, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface YamlFieldReader {
|
||||||
|
asRecord(value: unknown, label: string): Record<string, unknown>;
|
||||||
|
objectField(obj: Record<string, unknown>, key: string, path: string): Record<string, unknown>;
|
||||||
|
arrayOfRecords(value: unknown, path: string): Record<string, unknown>[];
|
||||||
|
stringField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
integerField(obj: Record<string, unknown>, key: string, path: string): number;
|
||||||
|
booleanField(obj: Record<string, unknown>, key: string, path: string): boolean;
|
||||||
|
stringArrayField(obj: Record<string, unknown>, key: string, path: string): string[];
|
||||||
|
numberArrayField(obj: Record<string, unknown>, key: string, path: string): number[];
|
||||||
|
enumField<const T extends readonly string[]>(obj: Record<string, unknown>, key: string, path: string, values: T): T[number];
|
||||||
|
kubernetesNameField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
sourceRefField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
envKeyField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
pgIdentifierField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
hostField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
portField(obj: Record<string, unknown>, key: string, path: string): number;
|
||||||
|
absolutePathField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
apiPathField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
httpsUrlField(obj: Record<string, unknown>, key: string, path: string): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createYamlFieldReader(configLabel: string): YamlFieldReader {
|
||||||
|
return {
|
||||||
|
asRecord: (value, label) => yamlRecord(value, label),
|
||||||
|
objectField: (obj, key, path) => yamlObjectField(obj, key, configLabel, path),
|
||||||
|
arrayOfRecords: (value, path) => yamlArrayOfRecords(value, configLabel, path),
|
||||||
|
stringField: (obj, key, path) => yamlStringField(obj, key, configLabel, path),
|
||||||
|
integerField: (obj, key, path) => yamlIntegerField(obj, key, configLabel, path),
|
||||||
|
booleanField: (obj, key, path) => yamlBooleanField(obj, key, configLabel, path),
|
||||||
|
stringArrayField: (obj, key, path) => yamlStringArrayField(obj, key, configLabel, path),
|
||||||
|
numberArrayField: (obj, key, path) => yamlIntegerArrayField(obj, key, configLabel, path),
|
||||||
|
enumField: (obj, key, path, values) => yamlEnumField(obj, key, configLabel, path, values),
|
||||||
|
kubernetesNameField: (obj, key, path) => yamlKubernetesNameField(obj, key, configLabel, path),
|
||||||
|
sourceRefField: (obj, key, path) => yamlSourceRefField(obj, key, configLabel, path),
|
||||||
|
envKeyField: (obj, key, path) => yamlEnvKeyField(obj, key, configLabel, path),
|
||||||
|
pgIdentifierField: (obj, key, path) => yamlPgIdentifierField(obj, key, configLabel, path),
|
||||||
|
hostField: (obj, key, path) => yamlHostField(obj, key, configLabel, path),
|
||||||
|
portField: (obj, key, path) => yamlPortField(obj, key, configLabel, path),
|
||||||
|
absolutePathField: (obj, key, path) => yamlAbsolutePathField(obj, key, configLabel, path),
|
||||||
|
apiPathField: (obj, key, path) => yamlApiPathField(obj, key, configLabel, path),
|
||||||
|
httpsUrlField: (obj, key, path) => yamlHttpsUrlField(obj, key, configLabel, path),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function repoRelative(path: string): string {
|
export function repoRelative(path: string): string {
|
||||||
const rel = relative(rootPath(), path);
|
const rel = relative(rootPath(), path);
|
||||||
return rel.startsWith("..") ? path : rel;
|
return rel.startsWith("..") ? path : rel;
|
||||||
|
|||||||
Reference in New Issue
Block a user