Skip to content

Commit

Permalink
nInput_Shell: +parseYaml
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsaguiar committed Aug 10, 2023
1 parent 916ff7f commit e6c9390
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions config/objects/nInput_Shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
* \
* - cmd (the command-line to execute)\
* - parseJson (boolean to indicate if the cmd output is json parsable)\
* - parseYaml (boolean to indicate if the cmd output is yaml parsable)\
* - attrTemplate (a string template for the name of the attribute)\
* - keys (a map with a SSH key or array of maps with SSH keys)\
* - chKeys (a channel with similar maps as keys)\
* \
* </odoc>
*/
var nInput_Shell = function(aCommand, isJson, attributeName) {
if (isObject(aCommand)) {
if (isMap(aCommand)) {
this.params = aCommand;
this.cmd = (isDef(aCommand.cmd) ? aCommand.cmd : "");
this.parseJson = (isDef(aCommand.parseJson) ? aCommand.parseJson : false);
this.parseYaml = (isDef(aCommand.parseYaml) ? aCommand.parseYaml : false)
this.name = (isDef(aCommand.name) ? aCommand.name : this.cmd);
this.attrTemplate = (isDef(aCommand.attrTemplate) ? aCommand.attrTemplate : "Server status/{{name}}");
} else {
Expand All @@ -36,6 +38,12 @@ inherit(nInput_Shell, nInput);
nInput_Shell.prototype.input = function(scope, args) {
var ret = {};

// Generic parsing function
var _parse = s => {
if (this.params.parseJson) return jsonParse(s, true)
if (this.params.parseYaml) return af.fromYAML(s)
}

if (isDef(this.params.chKeys) || isDef(this.params.keys)) {
var res = [], attrname;
var parent = this;
Expand Down Expand Up @@ -87,10 +95,10 @@ nInput_Shell.prototype.input = function(scope, args) {
epods.forEach(pod => {
try {
var rr = String(k.exec(v.namespace, pod, [ templify(parent.cmd) ], void 0, true));
if (parent.parseJson) {
if (parent.parseJson || parent.parseYaml) {
res.push({
key: parent.params.keys[i],
result: jsonParse(rr, true)
result: _parse(rr)
});
} else {
res.push({
Expand All @@ -108,10 +116,10 @@ nInput_Shell.prototype.input = function(scope, args) {
case "ssh":
default:
nattrmon.useObject(this.params.keys[i], (ssh) => {
if (this.parseJson) {
if (this.parseJson || this.parseYaml) {
res.push({
key: this.params.keys[i],
result: jsonParse(ssh.exec(templify(this.cmd)), true)
result: _parse(ssh.exec(templify(this.cmd)))
});
} else {
res.push({
Expand Down Expand Up @@ -140,8 +148,8 @@ nInput_Shell.prototype.input = function(scope, args) {

value = (isDef(value.stdin) ? value.stdin : "") + (isDef(value.stdout) ? value.stdout : "");

if (this.parseJson) {
ret[attrname] = jsonParse(value, true);
if (this.parseJson || this.parseYaml) {
ret[attrname] = _parse(value)
} else {
ret[attrname] = value;
}
Expand Down

0 comments on commit e6c9390

Please sign in to comment.