Skip to content

Commit

Permalink
sec buckets support
Browse files Browse the repository at this point in the history
  • Loading branch information
nmaguiar committed Feb 28, 2024
1 parent f0cce1d commit 66710e0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ List of options to use with the _inputcsv_ input option (when input type=csv) an
> Example of options provided in JSON: csv="{withHeader:false,withDelimiter:'|'}"
> Example of options provided in SLON: inputcsv="(withHeader: false, quoteMode: ALL)"
> You can also use _incsv_ as a shortcut for _inputcsv_
---

## 🧾 Base64 input/output options
Expand Down
3 changes: 1 addition & 2 deletions src/include/outputFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ var _outputFns = new Map([
var xls = new XLS(isDef(origFile) && io.fileExists(origFile) ? origFile : __)
var sheet = xls.getSheet(_$(params.xlssheet, "xlssheet").isString().default("data"))
params.xlsformat = _$(params.xlsformat, "xlsformat").isString().default("(bold: true, borderBottom: \"medium\", borderBottomColor: \"red\")")
if (params.xlsformat.trim().startsWith("{")) params.xlsformat = jsonParse(params.xlsformat, true)
if (params.xlsformat.trim().startsWith("(")) params.xlsformat = af.fromSLON(params.xlsformat)
params.xlsformat = _fromJSSLON(params.xlsformat)
ow.format.xls.setTable(xls, sheet, "A", 1, ar, __, params.xlsformat)
xls.writeFile(params.xlsfile)
xls.close()
Expand Down
10 changes: 10 additions & 0 deletions src/include/utilFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ const _runCmd2Bytes = (cmd, toStr) => {
.get()
return toStr ? af.fromBytes2String(data) : data
}
const _fromJSSLON = aString => {
if (!isString(aString) || aString == "" || isNull(aString)) return ""

aString = aString.trim()
if (aString.startsWith("{")) {
return jsonParse(aString, __, __, true)
} else {
return af.fromSLON(aString)
}
}
const _msg = "(processing data...)"
const _showTmpMsg = msg => printErrnl(_$(msg).default(_msg))
const _clearTmpMsg = msg => printErrnl("\r" + " ".repeat(_$(msg).default(_msg).length) + "\r")
26 changes: 24 additions & 2 deletions src/oafp.source.js.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ params.format = _$(params.format, "format").isString().default(__)
__initializeCon()
if (!String(java.lang.System.getProperty("os.name")).match(/Windows/)) __con.getTerminal().settings.set("sane")

// Check for OpenAF's sec buckets

if (isDef(params.secKey)) {
if (toBoolean(params.secEnv)) {
params.secRepo = "system"
params.secBucket = "envs"
}
params.secRepo = _$(params.secRepo, "secRepo").isString().default(getEnv("OAFP_SECREPO"))
params.secBucket = _$(params.secBucket, "secBucket").isString().default(getEnv("OAFP_SECBUCKET"))
params.secPass = _$(params.secPass, "secPass").isString().default(getEnv("OAFP_SECPASS"))
params.secMainPass = _$(params.secMainPass, "secMainPass").isString().default(getEnv("OAFP_SECMAINPASS"))
params.secFile = _$(params.secFile, "secFile").isString().default(getEnv("OAFP_SECFILE"))

let res = $sec(params.secRepo, params.secBucket, params.secPass, params.secMainPass, params.secFile).get(secKey)
if (isDef(res)) {
Object.keys(res).forEach(r => params[r] = res[r])
}
}

// Set options
var options = { __format: params.format, __from: params.from, __sql: params.sql, __path: params.path, __csv: params.csv, __pause: params.pause, __key: params.__key }
// ndjson options
Expand All @@ -257,10 +276,13 @@ var options = { __format: params.format, __from: params.from, __sql: params.sql,
}*/
// csv options
if (isDef(params.inputcsv)) {
params.inputcsv = params.inputcsv.trim().startsWith("{") ? jsonParse(params.inputcsv, true) : af.fromSLON(params.inputcsv)
params.inputcsv = _fromJSSLON(params.inputcsv)
}
if (isDef(params.incsv)) {
params.incsv = _fromJSSLON(params.incsv)
}
if (isDef(params.csv)) {
params.csv = params.csv.trim().startsWith("{") ? jsonParse(params.csv, true) : af.fromSLON(params.csv)
params.csv = _fromJSSLON(params.csv)
}

// Check version
Expand Down
20 changes: 20 additions & 0 deletions src/tests/autoTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,24 @@
var _r3 = $sh([getOpenAFPath() + "/oaf", "-f", "../oafp.source.js", "-e", "in=json out=json jsonschema=" + _f2 + " file=" + _f3]).get(0)
ow.test.assert(jsonParse(_r3.stdout).valid, true, "Problem with validating generated data from a jsonschema")
}

// CSV
exports.testCSV = function() {
var _f = io.createTempFile("testCSV", ".csv")
var data1 = [
{id: 1, status: true, text: "abc", number: 123},
{id: 2, status: false, text: "def", number: 456},
{id: 3, status: true, text: "ghi", number: 789}
]
var out1 = "id|status|text|number\r\n1|true|abc|123\r\n2|false|def|456\r\n3|true|ghi|789"

io.writeFileString(_f, stringify(data1, __, ""))
var _r = $sh([getOpenAFPath() + "/oaf", "-f", "../oafp.source.js", "-e", "in=json out=csv csv=\"(withDelimiter: '|')\" file=" + _f]).get(0)
ow.test.assert(_r.stdout.trim(), out1, "Problem with json to csv")

var _f2 = io.createTempFile("testCSV2", ".csv")
io.writeFileString(_f2, _r.stdout)
var _r2 = $sh([getOpenAFPath() + "/oaf", "-f", "../oafp.source.js", "-e", "in=csv inputcsv=\"(withDelimiter: '|')\" correcttypes=true out=json file=" + _f2]).get(0)
ow.test.assert(compare(jsonParse(_r2.stdout), data1), true, "Problem with csv to json")
}
})()
8 changes: 8 additions & 0 deletions src/tests/autoTest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ todo:
- oafp::YAML2JSON
- oafp::JSON2Base64
- oafp::JSON2Base64Gzip
- oafp::CSV2JSON

- oafp::merge
- oafp::sortMapKeys
Expand Down Expand Up @@ -67,6 +68,13 @@ jobs:
args:
func: "global.test.testYAML2JSON()"

# --------------------
- name: oafp::CSV2JSON
to : oJob Test
deps: Load test
args:
func: "global.test.testCSV()"

# -----------------------
- name: oafp::JSON2Base64
to : oJob Test
Expand Down

0 comments on commit 66710e0

Please sign in to comment.