Netrc.js
[gitlive] / Netrc.js
1 const GLib = imports.gi.GLib;
2
3 const File  = imports['../File.js'].File;
4
5
6 Netrc = {
7     
8     forHost: function(name)
9     {
10         try {
11             return this._data[name];
12         } catch(e) {
13             throw "Host " + name + " was not found in netrc file (or parser could not read file..)\n";
14         }
15     
16     },
17     
18     _data : {},
19     /**
20      * private - called at the bottom..
21      */
22     _load : function()
23     {
24         
25         var netrc = File.read(GLib.get_home_dir() + "/.netrc");
26         var authdata = {};
27         netrc.split("\n").forEach(function(nl) {
28             var line = {};
29             var k = false
30              
31             nl.replace(/\s+$/,'').replace(/^\s+/,'').split(/\s+/).forEach(function(kv) {
32               
33                 if (!k) {
34                     k = kv;
35                     return
36                 }
37                 line[k] = kv
38                 k = false;
39            });
40                
41            authdata[line.machine] = line;
42             
43         });
44         this._data = authdata;
45          
46     }
47     
48   
49     
50     
51 }
52 // load the file up.
53 Netrc._load();