UNPKG

mecano

Version:

Common functions for system deployment.

95 lines (90 loc) 2.39 kB
// Generated by CoffeeScript 1.11.1 var fs, misc, yaml; module.exports = function(options, callback) { var content, do_file, do_get, merge, ssh, target; if (options.line_width == null) { options.line_width = 160; } options.log({ message: "Entering file.yaml", level: 'DEBUG', module: 'mecano/lib/file/yaml' }); merge = options.merge, target = options.target, content = options.content, ssh = options.ssh; if (options.clean == null) { options.clean = true; } if (!content) { return callback(Error('Missing content')); } if (!target) { return callback(Error('Missing target')); } do_get = function() { if (!merge) { return do_file(); } options.log({ message: "Get content for merge", level: 'DEBUG', module: 'mecano/lib/file/yaml' }); return fs.exists(ssh, target, function(err, exists) { if (err) { return callback(err); } if (!exists) { return do_file(); } return fs.readFile(ssh, target, 'ascii', function(err, c) { var error; if (err && err.code !== 'ENOENT') { return callback(err); } try { return yaml.safeLoadAll(c, function(data) { data = misc.yaml.clean(data, content, true); options.content = misc.yaml.merge(data, content); return do_file(); }); } catch (error1) { error = error1; return callback(error); } }); }); }; do_file = (function(_this) { return function() { if (options.indent == null) { options.indent = 2; } if (options.clean) { options.log({ message: "Clean content", level: 'INFO', module: 'mecano/lib/file/yaml' }); misc.ini.clean(content); } options.log({ message: "Serialize content", level: 'DEBUG', module: 'mecano/lib/file/yaml' }); try { options.content = yaml.safeDump(options.content, { noRefs: true, lineWidth: options.line_width }); return _this.file(options, function(err, written) { return callback(err, written); }); } catch (error1) {} }; })(this); return do_get(); }; fs = require('ssh2-fs'); misc = require('../misc'); yaml = require('js-yaml');