|
| 1 | +// v1.0.0 |
| 2 | +"use strict" |
| 3 | + |
| 4 | +const fs = require('fs') |
| 5 | +const http = require('http') |
| 6 | +const https = require('https') |
| 7 | +const yaml = require('js-yaml') |
| 8 | + |
| 9 | +// this extension loads (global) attributes into the playbook (site.yml) |
| 10 | +// !! attributes in the playbook take precedence over loaded attributes !! |
| 11 | +// allows test building a repo like in multi repo envs with custom local attribute values |
| 12 | +// you can temporarily disable loading by setting 'enabled: false' |
| 13 | +// error handling: |
| 14 | +// if no 'attributefile' is configured: warn, but continue processing |
| 15 | +// if loading or processing the file caused an error (like file not found, needs fixing): stop |
| 16 | + |
| 17 | +module.exports.register = function ({ config }) { |
| 18 | + const logger = this.getLogger('load-global-site-attributes-extension') |
| 19 | + let attrib_file = '' |
| 20 | + let attrib_yaml = {} |
| 21 | + let orig_playbook = {} |
| 22 | + let result = {} |
| 23 | + |
| 24 | + this.on("playbookBuilt", async ({ playbook }) => { |
| 25 | + // get the original playbook asciidoc attributes, note it can be empty |
| 26 | + orig_playbook = JSON.parse(JSON.stringify(playbook.asciidoc.attributes)) || {} |
| 27 | + |
| 28 | + // only if attributefile is configured in site.yml pointing to a resource (file or url) |
| 29 | + if (config.attributefile) { |
| 30 | + try { |
| 31 | + // define the get function to use and load the file |
| 32 | + if (config.attributefile.startsWith('http')) { |
| 33 | + attrib_file = await get_file_from_url(config.attributefile) |
| 34 | + } else { |
| 35 | + attrib_file = await get_file_from_local(config.attributefile) |
| 36 | + } |
| 37 | + |
| 38 | + // convert and update |
| 39 | + attrib_yaml = await convert_yaml(attrib_file) |
| 40 | + result = Object.assign(attrib_yaml, playbook.asciidoc.attributes) |
| 41 | + playbook.asciidoc.attributes = result |
| 42 | + this.updateVariables( playbook ) |
| 43 | + |
| 44 | + // loading or processing the file caused an error |
| 45 | + } catch (error) { |
| 46 | + logger.error(error) |
| 47 | + this.stop() |
| 48 | + } |
| 49 | + } else { |
| 50 | + logger.warn('attributefile is not configured in the playbook (site.yml).') |
| 51 | + } |
| 52 | +// console.log(this.getVariables()) |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +function get_file_from_url(url) { |
| 57 | + // promise a file from url |
| 58 | + // when executed it returns the contents if found |
| 59 | + return new Promise((resolve, reject) => { |
| 60 | + const client = url.startsWith('https') ? https : http |
| 61 | + const req = client.request(url, (res) => { |
| 62 | + if (res.statusCode < 200 || res.statusCode >= 300) { |
| 63 | + reject(`Request Failed.\n` + |
| 64 | + `Status Code: ${res.statusCode}\n` + |
| 65 | + `${url}`) |
| 66 | + } |
| 67 | + var body = [] |
| 68 | + res.on('data', function(chunk) { |
| 69 | + body.push(chunk) |
| 70 | + }) |
| 71 | + res.on('end', function() { |
| 72 | + try { |
| 73 | + body = Buffer.concat(body).toString() |
| 74 | + } catch(error) { |
| 75 | + reject(error) |
| 76 | + } |
| 77 | + resolve(body) |
| 78 | + }) |
| 79 | + }) |
| 80 | + req.on('error', (error) => { |
| 81 | + reject(error.message) |
| 82 | + }) |
| 83 | + // send the request |
| 84 | + req.end() |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +function get_file_from_local(file) { |
| 89 | + // promise a file from local filesystem |
| 90 | + // when executed it returns the contents if found |
| 91 | + return new Promise((resolve, reject) => { |
| 92 | + fs.readFile(file, 'utf8', (error, data) => { |
| 93 | + if (error) reject(error) |
| 94 | + resolve(data) |
| 95 | + }) |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +function convert_yaml(data) { |
| 100 | + // promise to parse yaml data |
| 101 | + // when executed it returns the parsed contents |
| 102 | + return new Promise((resolve, reject) => { |
| 103 | + try { |
| 104 | + var d = yaml.load(data) |
| 105 | + // the parser found an error, no object will be returned |
| 106 | + } catch (error) { |
| 107 | + reject(`yaml parser: ` + error) |
| 108 | + } |
| 109 | + resolve(d) |
| 110 | + }) |
| 111 | +} |
0 commit comments