Skip to content

Commit

Permalink
outfile support
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Mar 17, 2024
1 parent 565b659 commit 25e9e45
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ Takes an input, usually a data structure such as json, and transforms it to an e
| sqlfilter | Enables the forcing of the sql filter parser (values: auto, simple, advanced) |
| path | A JMESPath expression to filter output |
| csv | If type=csv, the CSV options to use |
| outputkey | If defined the map/list output will be prefix with the provided key |
| outkey | If defined the map/list output will be prefix with the provided key |
| outfile | If defined all output will be written to the provided file |
| pause | If 'true' will try to pause contents in alternative to _less -r_ |
| color | If 'true' will force colored output if available |
| url | Retrieves data from the provided URL |
| urlmethod | If 'url' is provided defines the http method to use if different from GET |
| urlparams | If 'url' is provided extra parameters (equivalent to OpenAF's $rest) can be provided in JSON/SLON |
| urldata | If 'url' is provided a JSON/SLON/text data can be provided |
| loop | If defined will loop the processing by the number of seconds provided |
| loopcls | If 'true' and loop is defined it will clear the screen on each loop cycle |
| loopcls | If 'true' and loop is defined it will clear the screen (or file) on each loop cycle |
| -v | Changes the input to a map with the tool's version info |
| version | Alternative way to change the input to a map with the tool's version |

Expand Down
50 changes: 25 additions & 25 deletions src/include/outputFns.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var _outputFns = new Map([
["?" , (r, options) => {
r = Array.from(_outputFns.keys()).filter(r => r != '?').sort()
$o(r, options)
_o$o(r, options)
}],
["pm", (r, options) => {
$o(r, options)
_o$o(r, options)
}],
["key", (r, options) => {
$o(r, options)
_o$o(r, options)
}],
["html", (r, options) => {
let html, tmpf, res = false
Expand All @@ -32,21 +32,21 @@ var _outputFns = new Map([
if (res) {
sleep(params.htmlwait, true)
} else {
print(html)
_print(html)
}
}],
["ctable", (r, options) => {
$o(r, options)
_o$o(r, options)
}],
["stable", (r, options) => {
$o(r, options)
_o$o(r, options)
}],
["table", (r, options) => {
$o(r, options)
_o$o(r, options)
}],
["log", (r, options) => {
if (isString(r) && toBoolean(params.logprintall)) {
print(r.replace(/\n$/, ""))
_print(r.replace(/\n$/, ""))
} else {
var _arr = r
if (isMap(r)) _arr = [ r ]
Expand All @@ -69,39 +69,39 @@ var _outputFns = new Map([
if (l.toLowerCase().indexOf("warn") >= 0) lineC = _lt.warnLevel
}
if (isDef(d) && d.length > 24) d = d.substring(0, 23) + "Z"
if (isDef(m) || isDef(d)) print(ansiColor(_lt.timestamp, d) + (isDef(l) ? " | " + ansiColor(lineC, l) : "") + " | " + ansiColor(lineC, m))
if (isDef(m) || isDef(d)) _print(ansiColor(_lt.timestamp, d) + (isDef(l) ? " | " + ansiColor(lineC, l) : "") + " | " + ansiColor(lineC, m))
}
})
}
}
}],
["raw", (r, options) => {
if (isString(r))
print(r)
_print(r)
else
sprint(r)
_print(stringify(r))
}],
["ini", (r, options) => {
if (!isString(r)) {
ow.loadJava()
var ini = new ow.java.ini()
print( ini.put(r).save() )
_print( ini.put(r).save() )
}
}],
["mdyaml", (r, options) => {
if (isArray(r)) {
r.forEach((_y, i) => {
$o(_y, merge(options, { __format: "yaml" }))
if (i < r.length - 1) print("---\n")
_o$o(_y, merge(options, { __format: "yaml" }))
if (i < r.length - 1) _print("---\n")
})
} else {
$o(r, merge(options, { __format: "yaml" }))
_o$o(r, merge(options, { __format: "yaml" }))
}
}],
["mdtable", (r, options) => {
if (isArray(r)) {
ow.loadTemplate()
print( ow.template.md.table(r) )
_print( ow.template.md.table(r) )
}
}],
["template", (r, options) => {
Expand All @@ -111,7 +111,7 @@ var _outputFns = new Map([
ow.template.addOpenAFHelpers()
ow.template.addFormatHelpers()
if (isUnDef(params.template)) _exit(-1, "For output=handlebars you need to provide a template=someFile.hbs")
tprint(io.readFileString(params.template), r)
_print($t(io.readFileString(params.template), r))
}
}],
["openmetrics", (r, options) => {
Expand All @@ -124,12 +124,12 @@ var _outputFns = new Map([
if (line.indexOf("_id=\"") >= 0) line = line.replace(/,_id=\"\d+\",/, ",")
return line
}).join("\n")
$o(_out, options)
_o$o(_out, options)
}
}],
["pjson", (r, options) => {
options.__format = "prettyjson"
$o(r, options)
_o$o(r, options)
}],
["base64", (r, options) => {
var _o = ""
Expand All @@ -139,9 +139,9 @@ var _outputFns = new Map([
_o = stringify(r)

if (toBoolean(params.base64gzip)) {
print(af.fromBytes2String(af.toBase64Bytes(io.gzip(af.fromString2Bytes(_o)))))
_print(af.fromBytes2String(af.toBase64Bytes(io.gzip(af.fromString2Bytes(_o)))))
} else {
print(af.fromBytes2String(af.toBase64Bytes(_o)))
_print(af.fromBytes2String(af.toBase64Bytes(_o)))
}
}],
["grid" , (r, options) => {
Expand Down Expand Up @@ -175,7 +175,7 @@ var _outputFns = new Map([
})
})
let _out = ow.format.string.grid(_f, __, __, " ", true)
print(_out)
_print(_out)
} else {
_exit(-1, "Invalid grid parameter: '" + stringify(params.grid, __, "") + "'")
}
Expand All @@ -187,7 +187,7 @@ var _outputFns = new Map([
let fmt = _chartPathParse(r, params.chart)
if (fmt.length > 0) {
if (toBoolean(params.chartcls)) cls()
print(printChart("oafp " + fmt))
_print(printChart("oafp " + fmt))
}

}],
Expand Down Expand Up @@ -288,7 +288,7 @@ var _outputFns = new Map([
params.sqlnocreate = toBoolean(_$(params.sqlnocreate, "sqlnocreate").isString().default("false"))

ow.loadObj()
if (!params.sqlnocreate) print(ow.obj.fromObj2DBTableCreate(params.sqltable, r, __, !params.sqlicase)+";\n")
if (!params.sqlnocreate) _print(ow.obj.fromObj2DBTableCreate(params.sqltable, r, __, !params.sqlicase)+";\n")

var okeys, ookeys = Object.keys(ow.obj.flatMap(r[0]))
if (!params.sqlicase)
Expand All @@ -312,7 +312,7 @@ var _outputFns = new Map([
return _sql
}

print(r.map(_parseVal).join("\n"))
_print(r.map(_parseVal).join("\n"))
}],
["xls", (r, options) => {
if (!isString(r)) {
Expand Down
24 changes: 22 additions & 2 deletions src/include/utilFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ const _$f = (r, options) => {
const _$o = (r, options, lineByLine) => {
var nOptions = clone(options)

if (toBoolean(params.color)) __conConsole = true
if (toBoolean(params.color)) {
__conConsole = true
} else {
if (isDef(params.color)) {
__conAnsi = false
}
}
if (!isString(r)) {
if (lineByLine)
r = _$f([r], nOptions)[0]
Expand All @@ -79,7 +85,8 @@ const _$o = (r, options, lineByLine) => {
if (_outputFns.has(nOptions.__format)) {
_outputFns.get(nOptions.__format)(r, nOptions)
} else {
$o(r, nOptions)
if (isUnDef(nOptions.__format)) nOptions.__format = "tree"
_o$o(r, nOptions, __)
}
}
const _runCmd2Bytes = (cmd, toStr) => {
Expand Down Expand Up @@ -132,6 +139,19 @@ const _chartPathParse = (r, frmt,prefix) => {
}
return ""
}
const _print = (m) => {
if ("undefined" === typeof params.outfile) {
print(m)
} else {
if ("undefined" === typeof global.__oafp_streams) global.__oafp_streams = {}
if ("undefined" !== typeof global.__oafp_streams[params.outfile]) {
ioStreamWrite(global.__oafp_streams[params.outfile].s, m)
}
}
}
const _o$o = (a, b, c) => {
_print($o(a, b, c, true))
}
const _msg = "(processing data...)"
const _showTmpMsg = msg => { if (params.out != 'grid' && !toBoolean(params.loopcls) && !toBoolean(params.chartcls)) printErrnl(_$(msg).default(_msg)) }
const _clearTmpMsg = msg => { if (params.out != 'grid' && !toBoolean(params.loopcls) && !toBoolean(params.chartcls)) printErrnl("\r" + " ".repeat(_$(msg).default(_msg).length) + "\r") }
35 changes: 27 additions & 8 deletions src/oafp.source.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Object.keys(params).forEach(pk => {
if (pk != npk && isUnDef(params[npk])) {
params[npk] = params[pk]
delete params[pk]
}
}
})

// --- Util functions
Expand Down Expand Up @@ -64,25 +64,25 @@ const showHelp = () => {
if (isDef(ow.format.string.pauseString) && toBoolean(params.pause))
ow.format.string.pauseString( ow.format.withMD( io.readFileString(_f) + _customHelp ) )
else
print(ow.format.withMD( io.readFileString(_f) + _customHelp ))
_print(ow.format.withMD( io.readFileString(_f) + _customHelp ))
} else {
if (isDef(global._oafphelp) && isDef(global._oafphelp[_ff])) {
__ansiColorFlag = true
__conConsole = true
if (isDef(ow.format.string.pauseString) && toBoolean(params.pause))
ow.format.string.pauseString( ow.format.withMD( global._oafphelp[_ff] + _customHelp ) )
else
print(ow.format.withMD( global._oafphelp[_ff] + _customHelp))
_print(ow.format.withMD( global._oafphelp[_ff] + _customHelp))
} else {
if (isString(_oafhelp_libs[params.help])) {
__ansiColorFlag = true
__conConsole = true
if (isDef(ow.format.string.pauseString) && toBoolean(params.pause))
ow.format.string.pauseString( ow.format.withMD( _oafhelp_libs[params.help] ) )
else
print(ow.format.withMD( _oafhelp_libs[params.help] ))
_print(ow.format.withMD( _oafhelp_libs[params.help] ))
} else {
print("Check https://github.com/OpenAF/oafp/blob/master/src/" + _ff)
_print("Check https://github.com/OpenAF/oafp/blob/master/src/" + _ff)
}
}
}
Expand Down Expand Up @@ -229,14 +229,16 @@ if (isArray(params.libs)) {
try {
var _req = require("oafp_" + lib + ".js")
if (isDef(_req.oafplib)) {
var res = _req.oafplib(clone(params), _$o, $o, {
var res = _req.oafplib(clone(params), _$o, _o$o, {
_runCmd2Bytes: _runCmd2Bytes,
_fromJSSLON: _fromJSSLON,
_msg: _msg,
_showTmpMsg: _showTmpMsg,
_clearTmpMsg: _clearTmpMsg,
_chartPathParse: _chartPathParse,
_exit: _exit
_exit: _exit,
_print: _print,
_o$o: _o$o
})
if (isMap(res)) {
if (isArray(res.fileExtensions)) res.fileExtensions.forEach(r => _addSrcFileExtensions(r.ext, r.type))
Expand Down Expand Up @@ -323,6 +325,13 @@ if (params["-v"] == "" || (isString(params.version) && params.version.length > 0
// Read input from stdin or file
var _res = "", noFurtherOutput = false

// Check for output streams
if (isDef(params.outfile)) {
if ("undefined" === typeof global.__oafp_streams) global.__oafp_streams = {}
if ("undefined" === typeof global.__oafp_streams[params.outfile])
global.__oafp_streams[params.outfile] = { s: io.writeFileStream(params.outfile) }
}

var _run = () => {
if (_version) {
_res = showVersion()
Expand Down Expand Up @@ -471,12 +480,22 @@ if (isDef(params["-debug"])) {

if (isNumber(params.loop)) {
while(1) {
if (toBoolean(params.loopcls)) cls()
if (toBoolean(params.loopcls)) {
if (isDef(params.outfile)) {
global.__oafp_streams[params.outfile].close()
global.__oafp_streams[params.outfile] = io.writeFileStream(params.outfile)
} else {
cls()
}
}
_run()
sleep(params.loop * 1000, true)
}
} else {
_run()
}

// Close streams
if (isDef(global.__oafp_streams)) Object.keys(global.__oafp_streams).forEach(s => global.__oafp_streams[s].s.close())
}
oafp(params)

0 comments on commit 25e9e45

Please sign in to comment.