feat: scripts
This commit is contained in:
37
scripts/viewjson.js
Normal file
37
scripts/viewjson.js
Normal file
@@ -0,0 +1,37 @@
|
||||
#! /usr/bin/env runjs
|
||||
|
||||
var argsEx = require('component-args.js');
|
||||
|
||||
var System = java.lang.System;
|
||||
var FastJSON = Packages.com.alibaba.fastjson.JSON;
|
||||
var RStream = Packages.me.hatter.tools.commons.io.RStream;
|
||||
|
||||
var main = () => {
|
||||
var args = argsEx.parseDefARGs(['h', 'help', 'n']);
|
||||
if (args.flg('h', 'help')) {
|
||||
println('viewjson.js [FLAGS] [FILENAME]');
|
||||
println(' -h, --help Print help');
|
||||
println(' -n Do not print new line at last')
|
||||
return;
|
||||
}
|
||||
var json = null;
|
||||
if (args.length < 1) {
|
||||
json = RStream.from(System.in).string();
|
||||
} else {
|
||||
var j = $$.file(args[0]);
|
||||
if (!(j.exists())) {
|
||||
xprintln('[ERROR] File not exists: ' + j);
|
||||
return;
|
||||
}
|
||||
json = $$.rFile(j).string();
|
||||
}
|
||||
|
||||
try {
|
||||
var formatedJSON = FastJSON.toJSONString($$.parseJSON(json), true).replaceAll('\t', ' ');
|
||||
if (args.flg('n')) { print(formatedJSON) } else { println(formatedJSON); }
|
||||
} catch (e) {
|
||||
xprintln('[ERROR] Parse JSON failed.');
|
||||
}
|
||||
};
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user