initial commit
[nodejs-repoproxy.git] / lib / config.js
1 var fs = require("fs");
2
3 exports.loadConfig = function (conffile) {
4         
5         global.repoproxy = new Object();
6         global.repoproxy.listenPort = 8008;
7         global.repoproxy.cacheDir = "./cache";
8         global.repoproxy.repo = new Object();
9         
10         var confFileData = fs.readFileSync(conffile, "utf8");
11         
12         // first split the file on carriage returns;
13         var confLines = confFileData.split("\n");
14         
15         // go thru each line looking for config vars
16         for(var i=0; i<confLines.length; i++) {
17                 
18                 // trim a line down
19                 var line_one = confLines[i].trim();
20                 
21                 // split it up with :'s
22                 var line_real = line_one.replace(/#.*/,"").split(":");
23                 
24                 // parse the line
25                 switch(line_real[0]) {
26                 case "repo":
27                         
28                         // TODO: VALIDATE!
29                         console.log("Adding repo: '/%s' type '%s' from '%s', with update interval of '%s' days, and expire time of '%s' days.", line_real[1], line_real[2], line_real[3]+":"+line_real[4], line_real[5], line_real[6]);
30                         var thisrepo = { type : line_real[2], url: line_real[3]+":"+line_real[4], updateinterval: line_real[5], expiretime: line_real[6] };
31                         global.repoproxy.repo[line_real[1]] = thisrepo;
32                         
33                         break;
34                 case "cachedir":
35                         console.log("Cache dir set to: ", line_real[1]);
36                         global.repoproxy.cacheDir = line_real[1];
37                         
38                         break;
39                 case "listenport":
40                         console.log("Port set to: ", line_real[1]);
41                         global.repoproxy.listenPort = line_real[1];
42                         break;
43                 default:
44                         if(line_real[0] != "") {
45                                 console.log("Invalid line in configuration file ignored: '%s'", line_one);
46                         }
47                 }
48         }
49         
50         createCacheStructure();
51 }
52
53
54 function createCacheStructure() {
55         try {
56                 var state = fs.statSync(global.repoproxy.cacheDir);
57                 //console.log("state is:", state);
58         } catch(e) {
59                 try {
60                         fs.mkdirSync(global.repoproxy.cacheDir);
61                 } catch(ex) {
62                         console.log("ERROR: failure to create cache directory, '%s'", global.repoproxy.cacheDir);
63                 }
64         }
65         
66         for(var index in global.repoproxy.repo) {
67                 var fullDir = global.repoproxy.cacheDir + "/" + index;
68                 try {
69                         var state = fs.statSync(global.repoproxy.cacheDir);
70                         //console.log("state is:", state);
71                 } catch(e) {
72                         try {
73                                 fs.mkdirSync(fullDir);
74                         } catch(ex) {
75                                 console.log("ERROR: failed to create cache directory, '%s' for '%s'", fullDir, index);
76                         }
77                 }
78         }
79 }