diff --git a/bun/soupclown/index.ts b/bun/soupclown/index.ts index ca04d68..b70bc1d 100644 --- a/bun/soupclown/index.ts +++ b/bun/soupclown/index.ts @@ -1,3 +1,3 @@ import { SC_CONFIG } from "./src/interface/config"; -console.log('config', SC_CONFIG.config) \ No newline at end of file +console.log('config', SC_CONFIG) \ No newline at end of file diff --git a/bun/soupclown/src/interface/config.ts b/bun/soupclown/src/interface/config.ts index 70f0d32..2292239 100644 --- a/bun/soupclown/src/interface/config.ts +++ b/bun/soupclown/src/interface/config.ts @@ -3,56 +3,66 @@ import z, { config } from "zod"; const DEFAULT_CONFIG_PATH = "../../config.soupclown.json"; 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({ v: z.literal('v1'), - config: z.union([ + data: z.union([ hostConfigSchema, ]), }); export type CONFIG_SCHEMA_T = z.infer -const NEW_CONFIG: CONFIG_SCHEMA_T = { - v: 'v1', - config: { - services: new Map() - } -}; -async function _initConfigFile(path: string){ - await Bun.write(path, JSON.stringify(NEW_CONFIG)); -} +export class SC_CONFIG_C { + constructor( + protected runningConfig = { + v: 'v1', + data: { + services: [ + {name: 'test', desiredState: 'down'} + ] + }, + } as CONFIG_SCHEMA_T + ){} -async function _loadConfig(path = DEFAULT_CONFIG_PATH){ - const configFile = Bun.file(path); - let fileJsonData: any = null; - - try{ - fileJsonData = await configFile.json(); - console.log('json data', fileJsonData) - }catch(e){ - console.error('failed to parse json IG') - throw "failed" + public static async loadConfigFile(path = DEFAULT_CONFIG_PATH){ + const configFile = Bun.file(path); + let fileJsonData: any = null; + + try{ + fileJsonData = await configFile.json(); + }catch(e){ + console.error('failed to parse json IG') + throw "FAILED_SCHEMA_JSON_PARSE" + } + + const configParseJsonResult = CONFIG_SCHEMA.safeParse(fileJsonData); + if(!configParseJsonResult.success){ + console.error("Loaded config file but parsing failed") + throw "FAILED_CONFIG_SCHEMA_PARSE" + } + + return configParseJsonResult.data; } - const configParseJsonResult = CONFIG_SCHEMA.safeParse(fileJsonData); - - if(!configParseJsonResult.success){ - console.error('parsing error, overwrting config :3', configParseJsonResult.error) - await _initConfigFile(path); - return NEW_CONFIG; + public static async init(path = DEFAULT_CONFIG_PATH){ + const newConfig = new SC_CONFIG_C(); + await newConfig._loadConfigFile(path); + await newConfig._writeConfigFile(path); + return newConfig; } - return configParseJsonResult.data as CONFIG_SCHEMA_T; + 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)); + } } -async function methodWrapper(config: CONFIG_SCHEMA_T){ - const newConfig: CONFIG_SCHEMA_T & { - - } = config; - -} - -export const SC_CONFIG = await _loadConfig(); \ No newline at end of file +export const SC_CONFIG = await SC_CONFIG_C.init(); \ No newline at end of file diff --git a/config.soupclown.json b/config.soupclown.json index 442d504..4f3c196 100644 --- a/config.soupclown.json +++ b/config.soupclown.json @@ -1 +1 @@ -{"v":"v1","config":{"services":{}}} \ No newline at end of file +{"v":"v1","data":{"services":[{"name":"doohickey","desiredState":"up"}]}} \ No newline at end of file