/
Netrc.js
60 lines (45 loc) · 1.19 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
const GLib = imports.gi.GLib;
const File = imports.File.File;
/**
* usage:
* var cfg = Netrc.forHost('www.google.com');
* var uname = cfg.login
* var pass = cfg.password
*
*
*/
Netrc = {
forHost: function(name)
{
try {
return this._data[name];
} catch(e) {
throw "Host " + name + " was not found in netrc file (or parser could not read file..)\n";
}
},
_data : {},
/**
* private - called at the bottom..
*/
_load : function()
{
var netrc = File.read(GLib.get_home_dir() + "/.netrc");
var authdata = {};
netrc.split("\n").forEach(function(nl) {
var line = {};
var k = false
nl.replace(/\s+$/,'').replace(/^\s+/,'').split(/\s+/).forEach(function(kv) {
if (!k) {
k = kv;
return
}
line[k] = kv
k = false;
});
authdata[line.machine] = line;
});
this._data = authdata;
}
}
// load the file up.
Netrc._load();