| 6 | const config = require('../../config.js'); |
| 7 | |
| 8 | class HostnamesAddCommand extends Command { |
| 9 | |
| 10 | constructor() { |
| 11 | |
| 12 | super('hostnames', 'add'); |
| 13 | |
| 14 | } |
| 15 | |
| 16 | help() { |
| 17 | |
| 18 | return { |
| 19 | description: [ |
| 20 | 'Adds a new hostname route from a source custom hostname to a target service you own.', |
| 21 | 'Accepts wildcards wrapped in curly braces ("{}") or "*" at the front of the hostname.' |
| 22 | ].join('\n'), |
| 23 | args: [ |
| 24 | 'source', |
| 25 | 'target' |
| 26 | ] |
| 27 | }; |
| 28 | |
| 29 | } |
| 30 | |
| 31 | run(params, callback) { |
| 32 | |
| 33 | let host = 'api.autocode.com'; |
| 34 | let port = 443; |
| 35 | |
| 36 | let source = params.args[0] || ''; |
| 37 | let target = params.args[1] || ''; |
| 38 | |
| 39 | let versionString = target.split('[@')[1]; |
| 40 | versionString = versionString && versionString.replace(']', ''); |
| 41 | let service = target.split('[@')[0]; |
| 42 | let urlComponentArray = [service.split('.')[1], service.split('.')[0], 'api.stdlib.com']; |
| 43 | |
| 44 | if (versionString) { |
| 45 | versionString = versionString.split('.').join('-'); |
| 46 | urlComponentArray.unshift(versionString); |
| 47 | } |
| 48 | |
| 49 | let hostname = (params.flags.h && params.flags.h[0]) || ''; |
| 50 | let matches = hostname.match(/^(https?:\/\/)?(.*?)(:\d+)?$/); |
| 51 | |
| 52 | if (hostname && matches) { |
| 53 | host = matches[2]; |
| 54 | port = parseInt((matches[3] || '').substr(1) || (hostname.indexOf('https') === 0 ? 443 : 80)); |
| 55 | } |
| 56 | |
| 57 | let resource = new APIResource(host, port); |
| 58 | resource.authorize(config.get('ACCESS_TOKEN')); |
| 59 | |
| 60 | resource.request('v1/hostname_routes').create({}, { |
| 61 | hostname: source, |
| 62 | target: urlComponentArray.join('.') |
| 63 | }, (err, response) => { |
| 64 | |
| 65 | if (err) { |
nothing calls this directly
no outgoing calls
no test coverage detected