forked from fauna/fauna-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.js
More file actions
173 lines (155 loc) · 4.45 KB
/
eval.js
File metadata and controls
173 lines (155 loc) · 4.45 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const util = require('util')
const fs = require('fs')
const esprima = require('esprima')
const { flags } = require('@oclif/command')
const faunadb = require('faunadb')
const FaunaCommand = require('../lib/fauna-command.js')
const { readFile, runQueries, errorOut, writeFile } = require('../lib/misc.js')
const EVAL_OUTPUT_FORMATS = ['json', 'shell']
/**
* Write json encoded output
*
* @param {String} file Target filename
* @param {Any} data Data to encode
*/
function writeFormattedJson(file, data) {
let str = JSON.stringify(data)
if (file === null) {
return Promise.resolve(console.log(str))
}
return writeFile(file, str)
}
/**
* Write fauna shell encoded output
*
* @param {String} file Target filename
* @param {Any} data Data to encode
*/
function writeFormattedShell(file, data) {
let str = util.inspect(data, { depth: null })
if (file === null) {
return Promise.resolve(console.log(str))
}
return writeFile(file, str)
}
/**
* Writes out the formatted output to file
*
* @param {*} file Target filename
* @param {*} data Data to write
* @param {*} format Format to write as
*/
function writeFormattedOutput(file, data, format) {
if (format === 'json') return writeFormattedJson(file, data)
else if (format === 'shell') return writeFormattedShell(file, data)
else errorOut('Unsupported output format')
}
function performQuery(client, fqlQuery, outputFile, outputFormat) {
let res = esprima.parseScript(fqlQuery)
if (res.body[0].type === 'BlockStatement') {
res = esprima.parseScript(`(${fqlQuery})`)
}
return runQueries(res.body, client)
.then(function (response) {
return writeFormattedOutput(outputFile, response, outputFormat)
})
.catch(function (error) {
errorOut(
error.faunaError instanceof faunadb.errors.FaunaHTTPError
? util.inspect(
JSON.parse(error.faunaError.requestResult.responseRaw),
{
depth: null,
compact: false,
}
)
: error.faunaError.message
)
})
}
class EvalCommand extends FaunaCommand {
async run() {
const queryFromStdin = this.flags.stdin
let queriesFile = this.flags.file
const outputFile = this.flags.output
const outputFormat = this.flags.format
const { dbname, query } = this.getArgs()
const noSourceSet =
!queryFromStdin && query === undefined && queriesFile === undefined
if (noSourceSet) {
return errorOut(
'No source set. Pass --stdin to read from stdin or --file.'
)
}
try {
const { client } = await (dbname
? this.ensureDbScopeClient(dbname)
: this.getClient())
const readQuery = queryFromStdin || queriesFile !== undefined
let queryFromFile
if (readQuery) {
if (queryFromStdin && !fs.existsSync(queriesFile)) {
this.warn('Reading from stdin')
queriesFile = process.stdin.fd
}
queryFromFile = await readFile(queriesFile)
}
const result = await performQuery(
client,
queryFromFile || query,
outputFile,
outputFormat
)
return result
} catch (err) {
return errorOut(err.message, 1)
}
}
// Remap arguments if a user provide only one
getArgs() {
const { stdin, file } = this.flags
const { dbname, query } = this.args
if (dbname && !query && !stdin && !file) return { query: dbname }
return { dbname, query }
}
}
EvalCommand.examples = [
'$ fauna eval "Paginate(Collections())"',
'$ fauna eval nestedDbName "Paginate(Collections())"',
'$ fauna eval --file=/path/to/queries.fql',
'$ echo "Add(1,1)" | fauna eval --stdin',
'$ fauna eval "Add(2,3)" --output=/tmp/result"',
'$ fauna eval "Add(2,3)" --format=json --output=/tmp/result"',
]
EvalCommand.flags = {
...FaunaCommand.flags,
file: flags.string({
description: 'File where to read queries from',
}),
stdin: flags.boolean({
description: 'Read file input from stdin. Writes to stdout by default',
default: false,
}),
output: flags.string({
description: 'File to write output to',
default: null,
}),
format: flags.string({
description: 'Output format',
default: 'json',
options: EVAL_OUTPUT_FORMATS,
}),
}
EvalCommand.args = [
{
name: 'dbname',
required: false,
description: 'Database name',
},
{
name: 'query',
required: false,
description: 'FQL query to execute',
},
]
module.exports = EvalCommand