-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·46 lines (36 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env node
const program = require('commander');
const jsonToGo = require('./lib/json-to-go.js');
const fs = require('fs');
const highlight = require('cli-highlight').highlight;
const chalk = require('chalk');
const error = chalk.bold.red;
const info = chalk.bold.yellow;
let output = function (msg) {
return highlight(msg, {language: 'go', ignoreIllegals: true});
}
program
.name('json-to-go')
.usage("[Options] [-s/--string] <json-string> [-f/--file] <json-filepath>")
.version('1.0.0')
.option('-f, --file <path>', 'location of file containing json to convert')
.option('-s, --string <string>', 'json string to convert')
.option('-i, --inline', 'Use inline type definitions')
.parse(process.argv)
if (program.inline == undefined){program.inline = false};
if (program.file && program.string) {
return console.log(info('Error: Do not specify both [-s/--string] and [-f/--file]'));
};
if ((program.string == undefined) && (program.file == undefined)) {
return console.log(info('Error: Must specify one of [-s/--string] or [-f/--file]'));
}
let data = "";
if (program.string) {
data = program.string;
} else if (program.file) {
data = fs.readFileSync(program.file, 'utf-8');
} else {
return console.log( 'No options set' );
};
let result = jsonToGo( data, 'AutoGenerated', program.inline).go;
return console.log( output(result) );