/
index.js
114 lines (102 loc) · 3.34 KB
/
index.js
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
const _ = require("lodash");
const assert = require("assert");
const fs = require("fs");
const neodoc = require("neodoc");
const yaml = require("js-yaml");
const { run } = require("./src/run");
const help = `
bessersmith
Usage:
bessersmith -c <CONFIG_PATH>
bessersmith -h | --help
Options:
-c --config=<CONFIG_PATH> Specify the path to the UTF-8 YAML configuration
file.
-h --help Show this screen.
--version Show version.
`;
const toNumberOrUndefined = arg =>
typeof arg === "undefined" ? undefined : _.toNumber(arg);
const constructConfigFromEnvironment = () => {
const filled = {
bunyan: {
name: process.env.BUNYAN_NAME,
level: process.env.BUNYAN_LEVEL
},
mqtt: {
subscribe: {
url: process.env.MQTT_SUB_URL,
connectionOptions: {
port: toNumberOrUndefined(process.env.MQTT_SUB_PORT),
clientId: process.env.MQTT_SUB_CLIENT_ID,
clean: process.env.MQTT_SUB_CLEAN,
reconnectPeriod: toNumberOrUndefined(
process.env.MQTT_SUB_RECONNECT_PERIOD
),
username: process.env.MQTT_SUB_USERNAME,
password: process.env.MQTT_SUB_PASSWORD
},
subscriptionOptions: {
qos: toNumberOrUndefined(process.env.MQTT_SUB_QOS)
},
topic: process.env.MQTT_SUB_TOPIC
},
publish: {
url: process.env.MQTT_PUB_URL,
connectionOptions: {
port: toNumberOrUndefined(process.env.MQTT_PUB_PORT),
clientId: process.env.MQTT_PUB_CLIENT_ID,
clean: process.env.MQTT_PUB_CLEAN,
reconnectPeriod: toNumberOrUndefined(
process.env.MQTT_PUB_RECONNECT_PERIOD
),
username: process.env.MQTT_PUB_USERNAME,
password: process.env.MQTT_PUB_PASSWORD
},
publishingOptions: {
qos: toNumberOrUndefined(process.env.MQTT_PUB_QOS)
},
topic: process.env.MQTT_PUB_TOPIC,
logIntervalInSeconds: toNumberOrUndefined(
process.env.MQTT_PUB_LOG_INTERVAL_IN_SECONDS
)
}
},
cacheTTLInSeconds: toNumberOrUndefined(process.env.CACHE_TTL_IN_SECONDS),
protoPath: process.env.PROTO_PATH
};
return _.omitBy(filled, _.isUndefined);
};
const assertFieldExists = (value, name) => {
if (typeof value === "undefined") {
assert.fail(`${name} must be provided`);
}
};
const getConfig = cliConfigPath => {
const useCliConfigPath = typeof cliConfigPath === "undefined";
const configPath = cliConfigPath || process.env.CONFIG_PATH;
let configFromFile = {};
if (typeof configPath !== "undefined") {
configFromFile = yaml.safeLoad(fs.readFileSync(configPath, "utf8"));
}
const envConfig = constructConfigFromEnvironment();
let config = {};
if (!useCliConfigPath) {
config = _.merge(envConfig, configFromFile);
} else {
config = _.merge(configFromFile, envConfig);
}
assertFieldExists(config.bunyan.name, "bunyan.name");
assertFieldExists(config.mqtt.subscribe.topic, "mqtt.subscribe.topic");
assertFieldExists(config.mqtt.publish.topic, "mqtt.publish.topic");
assertFieldExists(config.protoPath, "protoPath");
return config;
};
const main = () => {
const args = neodoc.run(help);
const config = getConfig(args["--config"]);
Promise.resolve(run(config));
};
if (require.main === module) {
main();
}