config json thing
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
import { SC_CONFIG } from "./src/interface/config";
|
||||
|
||||
console.log('config', SC_CONFIG.config)
|
||||
console.log('config', SC_CONFIG)
|
||||
@@ -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<typeof CONFIG_SCHEMA>
|
||||
const NEW_CONFIG: CONFIG_SCHEMA_T = {
|
||||
|
||||
export class SC_CONFIG_C {
|
||||
constructor(
|
||||
protected runningConfig = {
|
||||
v: 'v1',
|
||||
config: {
|
||||
services: new Map()
|
||||
}
|
||||
};
|
||||
data: {
|
||||
services: [
|
||||
{name: 'test', desiredState: 'down'}
|
||||
]
|
||||
},
|
||||
} as CONFIG_SCHEMA_T
|
||||
){}
|
||||
|
||||
async function _initConfigFile(path: string){
|
||||
await Bun.write(path, JSON.stringify(NEW_CONFIG));
|
||||
}
|
||||
|
||||
async function _loadConfig(path = DEFAULT_CONFIG_PATH){
|
||||
public static async loadConfigFile(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"
|
||||
throw "FAILED_SCHEMA_JSON_PARSE"
|
||||
}
|
||||
|
||||
const configParseJsonResult = CONFIG_SCHEMA.safeParse(fileJsonData);
|
||||
|
||||
if(!configParseJsonResult.success){
|
||||
console.error('parsing error, overwrting config :3', configParseJsonResult.error)
|
||||
await _initConfigFile(path);
|
||||
return NEW_CONFIG;
|
||||
console.error("Loaded config file but parsing failed")
|
||||
throw "FAILED_CONFIG_SCHEMA_PARSE"
|
||||
}
|
||||
|
||||
return configParseJsonResult.data as CONFIG_SCHEMA_T;
|
||||
|
||||
return configParseJsonResult.data;
|
||||
}
|
||||
|
||||
async function methodWrapper(config: CONFIG_SCHEMA_T){
|
||||
const newConfig: CONFIG_SCHEMA_T & {
|
||||
|
||||
} = 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;
|
||||
}
|
||||
|
||||
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 @@
|
||||
{"v":"v1","config":{"services":{}}}
|
||||
{"v":"v1","data":{"services":[{"name":"doohickey","desiredState":"up"}]}}
|
||||
Reference in New Issue
Block a user