From e8bcf25eb6688d1a46a52f3a6771a51ba53b59e5 Mon Sep 17 00:00:00 2001 From: Abdirahim Musse <33973272+abmusse@users.noreply.github.com> Date: Wed, 19 Feb 2020 16:00:47 -0600 Subject: [PATCH] refactor: Remove ixml refs in CommandCall --- lib/CommandCall.js | 51 ++++++++++++++++------------------------------ lib/ixml.js | 6 ------ 2 files changed, 18 insertions(+), 39 deletions(-) diff --git a/lib/CommandCall.js b/lib/CommandCall.js index 638bf628..b64e5fa7 100644 --- a/lib/CommandCall.js +++ b/lib/CommandCall.js @@ -16,15 +16,6 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -const { - iXmlNodeCmdOpen, - iXmlNodeCmdClose, - iXmlNodeQshOpen, - iXmlNodeQshClose, - iXmlNodeShOpen, - iXmlNodeShClose, -} = require('./ixml'); - const availableCommands = ['sh', 'cl', 'qsh']; class CommandCall { @@ -44,7 +35,7 @@ class CommandCall { this.command = config.command; this.type = config.type; - this.options = config.options || null; + this.options = config.options || {}; this.xml = ''; } @@ -53,32 +44,26 @@ class CommandCall { * @returns {string} */ toXML() { - if (this.type === 'sh') { - if (this.options) { - this.xml = iXmlNodeShOpen(this.options.rows, this.options.hex, this.options.before, - this.options.after, this.options.error) - + this.command + iXmlNodeShClose(); - } else { - this.xml = iXmlNodeShOpen('', '', '', '', '') + this.command + iXmlNodeShClose(); - } - } else if (this.type === 'cl') { - const exec = (this.command.indexOf('?') > 0) ? 'rexx' : 'cmd'; + if (!availableCommands.includes(this.type)) { + throw new Error(`Invalid command type (${this.type}). Valid types: (${availableCommands.join(', ')})`); + } + + const type = this.type === 'cl' ? 'cmd' : this.type; + this.xml = `<${type}`; - if (this.options) { - this.xml = iXmlNodeCmdOpen(this.options.exec, this.options.hex, this.options.before, - this.options.after, this.options.error) + this.command + iXmlNodeCmdClose(); - } else { - this.xml = iXmlNodeCmdOpen(exec, '', '', '', '', '') + this.command + iXmlNodeCmdClose(); - } - } else if (this.type === 'qsh') { - if (this.options) { - this.xml = iXmlNodeQshOpen(this.options.rows, this.options.hex, this.options.before, - this.options.after, this.options.error) + this.command + iXmlNodeQshClose(); - } else { - this.xml = iXmlNodeQshOpen('', '', '', '', '') + this.command + iXmlNodeQshClose(); - } + if (this.type === 'cl') { + const defaultExec = (this.command.indexOf('?') > 0) ? 'rexx' : 'cmd'; + this.xml += ` exec='${this.options.exec || defaultExec}'`; } + // append optional values if set + if (this.options.rows) this.xml += ` rows='${this.options.rows}'`; + if (this.options.hex) this.xml += ` hex='${this.options.hex}'`; + if (this.options.before) this.xml += ` before='${this.options.before}'`; + if (this.options.after) this.xml += ` after='${this.options.after}'`; + + this.xml += ` error='${this.options.error || 'fast'}'>${this.command}`; + return this.xml; } } diff --git a/lib/ixml.js b/lib/ixml.js index ff3cd97a..8b5e1179 100644 --- a/lib/ixml.js +++ b/lib/ixml.js @@ -76,17 +76,11 @@ module.exports.iXmlNodeDataOpen = (xtype, options) => { module.exports.iXmlNodeDataClose = () => ''; -module.exports.iXmlNodeCmdOpen = (xexec, xhex, xbefore, xafter, xerror) => ``; -module.exports.iXmlNodeCmdClose = () => ''; -module.exports.iXmlNodeShOpen = (xrows, xhex, xbefore, xafter, xerror) => ``; -module.exports.iXmlNodeShClose = () => ''; -module.exports.iXmlNodeQshOpen = (xrows, xhex, xbefore, xafter, xerror) => ``; -module.exports.iXmlNodeQshClose = () => '';