This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
157 lines (151 loc) · 3.97 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import chalk from "chalk";
import convict from "convict";
import deepmerge from "deepmerge";
import prettyjson from "prettyjson";
import * as winston from "winston";
import { Argv } from "yargs";
import { Provider } from "./providers/provider";
export class Config {
private static initialConfig = {
verbose: {
doc: "Logging everything or just the important stuff.",
format: ["true", "false"],
default: "false",
env: "VERBOSE"
},
env: {
doc: "The application environment.",
format: ["production", "development", "test"],
default: "development",
env: "NODE_ENV"
},
projectPrefix: {
doc:
"A short project name which is used for directories on the SFTP server.",
default: "",
env: "PROJECT_PREFIX"
},
source: {
type: {
doc: "The type of service to back-up.",
default: "",
env: "SOURCE_TYPE"
}
},
target: {
host: {
doc: "The target host.",
default: "",
env: "TARGET_HOST"
},
port: {
doc: "The SFTP port",
default: "22",
env: "TARGET_PORT"
},
username: {
doc: "The SFTP username",
default: "",
env: "TARGET_USERNAME"
},
password: {
doc: "The SFTP password",
default: "",
env: "TARGET_PASSWORD",
sensitive: true
},
maxBackups: {
weekly: {
doc: "Maximum number of backups to keep for weekly backups",
default: "4",
env: "TARGET_MAX_BACKUPS_WEEKLY"
},
daily: {
doc: "Maximum number of backups to keep for daily backups",
default: "6",
env: "TARGET_MAX_BACKUPS_DAILY"
}
}
},
slack: {
webhook: {
doc: "The webhook URL.",
default: "",
env: "SLACK_WEBHOOK"
}
},
cluster: {
enabled: {
doc:
"Enable cluster-mode in which for multiple lifebelt processes only a single one (the leader) performs backups.",
format: ["true", "false"],
default: "false",
env: "CLUSTER_ENABLED"
},
leaderUrl: {
doc: "The URL with which to determine which process is the leader.",
default: "http://localhost:4040/",
env: "CLUSTER_LEADER_URL"
}
},
cron: {
weeklySchedule: {
doc: "The time by which to start a weekly backup",
default: "00 30 2 * * 0",
env: "CRON_WEEKLY_SCHEDULE"
},
dailySchedule: {
doc: "The time by which to start a daily backup",
default: "00 30 2 * * 1-6",
env: "CRON_DAILY_SCHEDULE"
},
delay: {
doc:
"The minutes by which to delay each backup execution. A random value between 0 and 120 is picked if empty (the default).",
default: "",
env: "CRON_DELAY"
}
}
};
private static schema: any = convict(Config.initialConfig);
private static mandatoryKeys = [
"projectPrefix",
"source.type",
"target.host",
"target.username",
"target.port"
];
static get(key: string) {
return this.schema.get(key);
}
static mergeProviderConfig(provider: Provider) {
const verbose = this.schema.get("verbose");
this.schema = convict(deepmerge(this.initialConfig, provider.config()));
this.schema.set("verbose", verbose);
provider.mandatoryKeys().forEach(k => this.mandatoryKeys.push(k));
let error = false;
this.mandatoryKeys.forEach(k => {
if (this.schema.get(k) === "") {
winston.error(
`Config is missing ${chalk.underline("mandatory")} key %s`,
chalk.bold(k)
);
error = true;
}
});
if (error) {
process.exit(1);
}
}
static mergeYargs(argv: Argv) {
if (argv["v"] !== undefined) {
this.schema.set("verbose", argv["v"]);
}
}
static print() {
winston.debug(
"Loaded the config:\n%s",
prettyjson.render(JSON.parse(this.schema.toString()))
);
}
}