-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.ts
52 lines (43 loc) · 1.11 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { parse } from "https://deno.land/[email protected]/toml/mod.ts";
import { parseArgs } from "https://deno.land/[email protected]/cli/parse_args.ts";
type Config = {
port: number
enable_api: boolean
enable_prometheus: boolean
}
function findConfig(): string {
const flags = parseArgs(Deno.args, {
string: ["config-path"],
default: {
"config-path": "config.toml"
}
});
let path = `${Deno.cwd()}/${flags["config-path"]}` // Default config path is where binary is currently located
if (flags["config-path"] && flags["config-path"] !== "") {
path = flags["config-path"]
}
try {
const decoder = new TextDecoder();
return decoder.decode(Deno.readFileSync(path));
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
console.error(error);
return "";
}
}
return ""
}
export function loadConfig(): Config {
const config = findConfig()
const conf = parse(config) as Config
if (!conf.port) {
conf.port = 80;
}
if (!conf.enable_api) {
conf.enable_api = false
}
if (!conf.enable_prometheus) {
conf.enable_prometheus = false
}
return conf
}