config json thing

This commit is contained in:
2026-04-26 22:53:01 -04:00
parent eda66b7e69
commit 565630cfe4
3 changed files with 48 additions and 38 deletions
+1 -1
View File
@@ -1,3 +1,3 @@
import { SC_CONFIG } from "./src/interface/config"; import { SC_CONFIG } from "./src/interface/config";
console.log('config', SC_CONFIG.config) console.log('config', SC_CONFIG)
+36 -26
View File
@@ -3,56 +3,66 @@ import z, { config } from "zod";
const DEFAULT_CONFIG_PATH = "../../config.soupclown.json"; const DEFAULT_CONFIG_PATH = "../../config.soupclown.json";
const hostConfigSchema = z.object({ const hostConfigSchema = z.object({
services: z.map(z.string(), z.unknown()), services: z.array(z.object({
name: z.string(),
desiredState: z.enum(['up', 'down']),
})),
}) })
const CONFIG_SCHEMA = z.object({ const CONFIG_SCHEMA = z.object({
v: z.literal('v1'), v: z.literal('v1'),
config: z.union([ data: z.union([
hostConfigSchema, hostConfigSchema,
]), ]),
}); });
export type CONFIG_SCHEMA_T = z.infer<typeof CONFIG_SCHEMA> export type CONFIG_SCHEMA_T = z.infer<typeof CONFIG_SCHEMA>
const NEW_CONFIG: CONFIG_SCHEMA_T = {
export class SC_CONFIG_C {
constructor(
protected runningConfig = {
v: 'v1', v: 'v1',
config: { data: {
services: new Map() services: [
} {name: 'test', desiredState: 'down'}
}; ]
},
} as CONFIG_SCHEMA_T
){}
async function _initConfigFile(path: string){ public static async loadConfigFile(path = DEFAULT_CONFIG_PATH){
await Bun.write(path, JSON.stringify(NEW_CONFIG));
}
async function _loadConfig(path = DEFAULT_CONFIG_PATH){
const configFile = Bun.file(path); const configFile = Bun.file(path);
let fileJsonData: any = null; let fileJsonData: any = null;
try{ try{
fileJsonData = await configFile.json(); fileJsonData = await configFile.json();
console.log('json data', fileJsonData)
}catch(e){ }catch(e){
console.error('failed to parse json IG') console.error('failed to parse json IG')
throw "failed" throw "FAILED_SCHEMA_JSON_PARSE"
} }
const configParseJsonResult = CONFIG_SCHEMA.safeParse(fileJsonData); const configParseJsonResult = CONFIG_SCHEMA.safeParse(fileJsonData);
if(!configParseJsonResult.success){ if(!configParseJsonResult.success){
console.error('parsing error, overwrting config :3', configParseJsonResult.error) console.error("Loaded config file but parsing failed")
await _initConfigFile(path); throw "FAILED_CONFIG_SCHEMA_PARSE"
return NEW_CONFIG;
} }
return configParseJsonResult.data as CONFIG_SCHEMA_T; return configParseJsonResult.data;
} }
async function methodWrapper(config: CONFIG_SCHEMA_T){ public static async init(path = DEFAULT_CONFIG_PATH){
const newConfig: CONFIG_SCHEMA_T & { const newConfig = new SC_CONFIG_C();
await newConfig._loadConfigFile(path);
} = config; await newConfig._writeConfigFile(path);
return newConfig;
} }
export const SC_CONFIG = await _loadConfig(); private async _loadConfigFile(path = DEFAULT_CONFIG_PATH){
this.runningConfig = await SC_CONFIG_C.loadConfigFile(path);
}
private async _writeConfigFile(path = DEFAULT_CONFIG_PATH){
await Bun.write(path, JSON.stringify(this.runningConfig));
}
}
export const SC_CONFIG = await SC_CONFIG_C.init();
+1 -1
View File
@@ -1 +1 @@
{"v":"v1","config":{"services":{}}} {"v":"v1","data":{"services":[{"name":"doohickey","desiredState":"up"}]}}