-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnetrc.js
82 lines (70 loc) · 2.01 KB
/
netrc.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
/*!
* netrc
*
* Copyright(c) 2012 JD Brennan <[email protected]>
* MIT License
*/
var fs = require('fs');
exports = module.exports = new NetRC();
function NetRC() {
this.filename = process.env.HOME + "/.netrc";
this.machines = null;
}
NetRC.prototype.file = function(filename) {
this.filename = filename;
}
NetRC.prototype.host = function(hostname) {
if (this.machines === null) {
this.init();
}
if (!this.machines[hostname]) this.error("Machine " + hostname + " not found in " + this.filename);
return this.machines[hostname];
}
NetRC.prototype.init = function() {
if (!fs.existsSync(this.filename)) this.error("File does not exist: " + this.filename);
this.machines = {};
var data = fs.readFileSync(this.filename, "UTF-8");
// Remove comments
var lines = data.split('\n');
for (var n in lines) {
var i = lines[n].indexOf('#');
if (i > -1) lines[n] = lines[n].substring(0,i);
}
data = lines.join('\n');
var tokens = data.split(/[ \t\n\r]+/);
var machine = new Machine();
var key = null;
for (var i in tokens) {
if (!key) {
key = tokens[i];
if (key === 'machine') {
this.machines[machine.machine] = machine;
machine = new Machine();
}
} else {
machine[key] = this.unescape(tokens[i]);
key = null;
}
}
this.machines[machine.machine] = machine;
}
// Allow spaces and other weird characters in passwords by supporting \xHH
NetRC.prototype.unescape = function(s) {
var match = /\\x([0-9a-fA-F]{2})/.exec(s);
if (match) {
s = s.substr(0,match.index)
+ String.fromCharCode(parseInt(match[1], 16))
+ s.substr(match.index+4)
}
return s;
}
NetRC.prototype.error = function(message) {
console.error("netrc: Error:", message);
process.exit(1);
}
function Machine() {
this.machine = 'empty';
this.login = null;
this.password = null;
this.account = null;
}