config.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Generated by IcedCoffeeScript 1.7.1-c
  2. (function() {
  3. var Config, constants, fs, iced, log, mkdirp, purge, util, __iced_k, __iced_k_noop;
  4. iced = require('iced-runtime').iced;
  5. __iced_k = __iced_k_noop = function() {};
  6. fs = require('fs');
  7. log = require('./log');
  8. util = require('util');
  9. purge = require('./util').purge;
  10. mkdirp = require('./fs').mkdirp;
  11. constants = require('./constants').constants;
  12. exports.Config = Config = (function() {
  13. function Config(filename, opts) {
  14. this.filename = filename;
  15. this.opts = opts;
  16. this.json = null;
  17. this.loaded = false;
  18. this.cache = {};
  19. this.changed = false;
  20. }
  21. Config.prototype.open = function(cb) {
  22. var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  23. __iced_k = __iced_k_noop;
  24. ___iced_passed_deferral = iced.findDeferral(arguments);
  25. err = null;
  26. log.debug("+ opening config file: " + this.filename);
  27. (function(_this) {
  28. return (function(__iced_k) {
  29. __iced_deferrals = new iced.Deferrals(__iced_k, {
  30. parent: ___iced_passed_deferral,
  31. filename: "/Users/max/src/keybase/node-client/src/config.iced",
  32. funcname: "Config.open"
  33. });
  34. fs.exists(_this.filename, __iced_deferrals.defer({
  35. assign_fn: (function(__slot_1) {
  36. return function() {
  37. return __slot_1.found = arguments[0];
  38. };
  39. })(_this),
  40. lineno: 25
  41. }));
  42. __iced_deferrals._fulfill();
  43. });
  44. })(this)((function(_this) {
  45. return function() {
  46. (function(__iced_k) {
  47. if (_this.found) {
  48. (function(__iced_k) {
  49. __iced_deferrals = new iced.Deferrals(__iced_k, {
  50. parent: ___iced_passed_deferral,
  51. filename: "/Users/max/src/keybase/node-client/src/config.iced",
  52. funcname: "Config.open"
  53. });
  54. _this.load(__iced_deferrals.defer({
  55. assign_fn: (function() {
  56. return function() {
  57. return err = arguments[0];
  58. };
  59. })(),
  60. lineno: 27
  61. }));
  62. __iced_deferrals._fulfill();
  63. })(__iced_k);
  64. } else {
  65. return __iced_k(!_this.opts.quiet ? log.warn("No config file found; tried '" + _this.filename + "'") : void 0);
  66. }
  67. })(function() {
  68. log.debug("- opened config file; found=" + _this.found);
  69. return cb(err);
  70. });
  71. };
  72. })(this));
  73. };
  74. Config.prototype.is_empty = function() {
  75. return !(this.json != null);
  76. };
  77. Config.prototype.is_dirty = function() {
  78. return this.changed;
  79. };
  80. Config.prototype.unlink = function(cb) {
  81. var err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  82. __iced_k = __iced_k_noop;
  83. ___iced_passed_deferral = iced.findDeferral(arguments);
  84. (function(_this) {
  85. return (function(__iced_k) {
  86. __iced_deferrals = new iced.Deferrals(__iced_k, {
  87. parent: ___iced_passed_deferral,
  88. filename: "/Users/max/src/keybase/node-client/src/config.iced",
  89. funcname: "Config.unlink"
  90. });
  91. fs.unlink(_this.filename, __iced_deferrals.defer({
  92. assign_fn: (function() {
  93. return function() {
  94. return err = arguments[0];
  95. };
  96. })(),
  97. lineno: 41
  98. }));
  99. __iced_deferrals._fulfill();
  100. });
  101. })(this)((function(_this) {
  102. return function() {
  103. log.info("Removing file: " + _this.filename);
  104. return cb(err);
  105. };
  106. })(this));
  107. };
  108. Config.prototype.write = function(cb) {
  109. var d, dat, err, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  110. __iced_k = __iced_k_noop;
  111. ___iced_passed_deferral = iced.findDeferral(arguments);
  112. err = null;
  113. (function(_this) {
  114. return (function(__iced_k) {
  115. if (_this.changed) {
  116. _this.json = purge(_this.json);
  117. dat = JSON.stringify(_this.json, null, " ");
  118. (function(__iced_k) {
  119. __iced_deferrals = new iced.Deferrals(__iced_k, {
  120. parent: ___iced_passed_deferral,
  121. filename: "/Users/max/src/keybase/node-client/src/config.iced",
  122. funcname: "Config.write"
  123. });
  124. mkdirp(_this.filename, __iced_deferrals.defer({
  125. assign_fn: (function() {
  126. return function() {
  127. err = arguments[0];
  128. return d = arguments[1];
  129. };
  130. })(),
  131. lineno: 52
  132. }));
  133. __iced_deferrals._fulfill();
  134. })(function() {
  135. (function(__iced_k) {
  136. if (err != null) {
  137. return __iced_k(log.error("Error creating directory '" + d + "': " + err.message));
  138. } else {
  139. (function(__iced_k) {
  140. __iced_deferrals = new iced.Deferrals(__iced_k, {
  141. parent: ___iced_passed_deferral,
  142. filename: "/Users/max/src/keybase/node-client/src/config.iced",
  143. funcname: "Config.write"
  144. });
  145. fs.writeFile(_this.filename, dat, {
  146. mode: constants.permissions.file
  147. }, __iced_deferrals.defer({
  148. assign_fn: (function() {
  149. return function() {
  150. return err = arguments[0];
  151. };
  152. })(),
  153. lineno: 56
  154. }));
  155. __iced_deferrals._fulfill();
  156. })(function() {
  157. return __iced_k(err != null ? log.error("Error writing to " + _this.filename + ": " + err) : void 0);
  158. });
  159. }
  160. })(function() {
  161. return __iced_k(err == null ? (log.info("Updated file: " + _this.filename), _this.changed = false) : void 0);
  162. });
  163. });
  164. } else {
  165. return __iced_k();
  166. }
  167. });
  168. })(this)((function(_this) {
  169. return function() {
  170. return cb(err);
  171. };
  172. })(this));
  173. };
  174. Config.prototype.get = function(key) {
  175. var p, parts, v, _i, _len;
  176. parts = key.split(".");
  177. v = this.json;
  178. for (_i = 0, _len = parts.length; _i < _len; _i++) {
  179. p = parts[_i];
  180. if (v != null) {
  181. v = v[p];
  182. }
  183. }
  184. return v;
  185. };
  186. Config.prototype.set = function(key, val) {
  187. var d, e, last, p, parts, _i, _len, _ref;
  188. parts = key.split(".");
  189. if (this.json == null) {
  190. this.json = {};
  191. this.changed = true;
  192. }
  193. d = this.json;
  194. _ref = parts.slice(0, parts.length - 1);
  195. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  196. p = _ref[_i];
  197. if (d[p] == null) {
  198. d[p] = {};
  199. this.changed = true;
  200. }
  201. d = d[p];
  202. }
  203. last = parts.slice(-1)[0];
  204. e = d[last];
  205. if (e !== val) {
  206. d[last] = val;
  207. return this.changed = true;
  208. }
  209. };
  210. Config.prototype.load = function(cb) {
  211. var e, err, file, key, msg, ___iced_passed_deferral, __iced_deferrals, __iced_k;
  212. __iced_k = __iced_k_noop;
  213. ___iced_passed_deferral = iced.findDeferral(arguments);
  214. log.debug("+ loading config file " + this.filename);
  215. err = null;
  216. (function(_this) {
  217. return (function(__iced_k) {
  218. __iced_deferrals = new iced.Deferrals(__iced_k, {
  219. parent: ___iced_passed_deferral,
  220. filename: "/Users/max/src/keybase/node-client/src/config.iced",
  221. funcname: "Config.load"
  222. });
  223. fs.readFile(_this.filename, __iced_deferrals.defer({
  224. assign_fn: (function() {
  225. return function() {
  226. err = arguments[0];
  227. return file = arguments[1];
  228. };
  229. })(),
  230. lineno: 96
  231. }));
  232. __iced_deferrals._fulfill();
  233. });
  234. })(this)((function(_this) {
  235. return function() {
  236. var _i, _len, _ref, _ref1;
  237. if (err != null) {
  238. log.error("Cannot read file " + _this.filename + ": " + err);
  239. } else {
  240. try {
  241. _this.json = JSON.parse(file);
  242. } catch (_error) {
  243. e = _error;
  244. log.error("Invalid json in " + _this.filename + ": " + e);
  245. err = e;
  246. }
  247. }
  248. if (err == null) {
  249. _ref = [];
  250. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  251. key = _ref[_i];
  252. if (((_ref1 = _this.json) != null ? _ref1[key] : void 0) == null) {
  253. log.error("Missing JSON component '" + key + "' in " + _this.filename);
  254. err = new E.ConfigError("missing component '" + key + "'");
  255. }
  256. }
  257. }
  258. msg = _this.opts.secret ? "<redacted>" : JSON.stringify(_this.json);
  259. log.debug("- loaded config file -> " + msg);
  260. return cb(err);
  261. };
  262. })(this));
  263. };
  264. Config.prototype.obj = function() {
  265. return this.json;
  266. };
  267. Config.prototype.clear = function() {
  268. return this.json = {};
  269. };
  270. return Config;
  271. })();
  272. }).call(this);