-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.js
103 lines (97 loc) · 2.91 KB
/
config.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
/*
Try and find username and password from optimist.
If not found, attempt to load it from ~/.teslams/config.json
format of config.json:
{
"username": "teslamotors.com username/email",
"password": "teslamotors.com password",
"visualize": {
"baseUrl": "/teslavis",
"webusers": [
{ "id": 1, "username": "dirk", "password": "secret" },
{ "id": 2, "username": "bob", "password": "different" }
]
}
}
If there is any trouble loading or parsing that file,
make the optimist args required as usual.
This now also contains the web user/password database and path for visualize.js.
Please note that here all the properties need to be in quotes as we are parsing
this file as JSON - so don't just copy the array as it was in visualize.js before
(where id, username and password were NOT quoted).
*/
exports.config = function (opt)
{
var config,
configFile = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] +
"/.teslams/config.json",
configSuccess = false;
if (opt.argv['$0'].indexOf("visualize.js") != -1)
{
// we are calling from visualize.js, so let's get the user/password pairs for that
try
{
config = JSON.parse(require('fs').readFileSync(configFile).toString());
configSuccess = config.hasOwnProperty('visualize');
}
catch (err)
{
console.warn("Unable to load " + configFile + "; web server authentication turned off");
}
if (configSuccess)
{
if (opt.argv.debug) {
console.log("found " + config.visualize.webusers.length + " user / password entries; enabling web authentication");
}
}
else
{
console.log("didn't find 'visualize' property in config file, web authentication turned off");
}
return config;
}
// if no user name & password supplied on cmd line options, check environment variable
if (!opt.argv.username && !opt.argv.password){
config = {
username: process.env.TSLA_USERNAME,
password: process.env.TSLA_PASSWORD
};
if (config.username != undefined && config.password != undefined){
if (opt.argv.debug) {
console.log('Teslamotors.com logon information loaded from environment variables $TSLA_USERNAME and $TSLA_PASSWORD');
}
return config;
}
}
// if no user name & password supplied on cmd line options, look in config file
if (!opt.argv.username && !opt.argv.password)
{
try
{
config = JSON.parse(require('fs').readFileSync(configFile).toString());
configSuccess = config.hasOwnProperty('username') && config.hasOwnProperty('password');
}
catch (err)
{
console.warn("Unable to load " + configFile + "; username & password are required arguments");
}
}
if (configSuccess)
{
if (opt.argv.debug) {
console.log('Teslamotors.com logon information loaded from ' + configFile);
}
}
else
{
// fall back to making them optimist required options
opt.demand('u');
opt.demand('p');
config =
{
username: opt.argv.username,
password: opt.argv.password
};
}
return config;
};