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