-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor env config to be centralized
- Loading branch information
Showing
5 changed files
with
71 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { z } from "zod"; | ||
import fs from "fs/promises"; | ||
import merge from "deepmerge"; | ||
|
||
const configPath = `${process.cwd()}/config.json`; | ||
|
||
const schemaConfig = z.object({ | ||
database: z.object({ | ||
url: z.string(), | ||
}), | ||
kubeconfig: z | ||
.object({ | ||
base64: z.string().optional(), | ||
path: z.string().optional(), | ||
fromcluster: z.boolean().optional(), | ||
}) | ||
.optional(), | ||
prometheus: z.object({ | ||
url: z.string(), | ||
}), | ||
}); | ||
|
||
const parseEnv = ({ prefix = "", envs = process.env } = {}) => { | ||
const parsed = {} as any; | ||
Object.entries(envs).forEach(([key, value]) => { | ||
const seq = (obj: any, arr: Array<string>, v: string | undefined) => { | ||
if (typeof obj === "string") return; | ||
if (arr.length > 1) { | ||
const [pos] = arr.splice(0, 1); | ||
if (!obj[pos]) obj[pos] = {}; | ||
seq(obj[pos], arr, v); | ||
} else { | ||
obj[arr[0]] = v; | ||
} | ||
}; | ||
const keyArr = key.split("_").map((e) => e.toLocaleLowerCase()); | ||
seq(parsed, keyArr, value); | ||
}); | ||
return prefix !== "" ? parsed?.[prefix] : parsed; | ||
}; | ||
|
||
const parseJSON = async () => { | ||
try { | ||
return JSON.parse((await fs.readFile(configPath)).toString()); | ||
} catch (e) { | ||
console.info("no json config found, get config only from envs"); | ||
return {}; | ||
} | ||
}; | ||
|
||
const getConfig = async () => { | ||
const config = merge(await parseJSON(), parseEnv({ prefix: "deploycat" })); | ||
console.log("config:", config); | ||
return config; | ||
}; | ||
|
||
export const config = schemaConfig.parse(await getConfig()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters