-
Notifications
You must be signed in to change notification settings - Fork 1
/
katago-analyze-sgf-cli.js
executable file
·74 lines (65 loc) · 1.67 KB
/
katago-analyze-sgf-cli.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
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
#!/bin/node
const net = require('net')
const SERVER_PORT = 6364
function main() {
const argv = require('yargs').command(
'$0 COMMAND [PARAMS] [OPTIONS]',
'Process SGF files using the KataGo analysis engine - client.',
(yargs) => {
yargs.option('port', {
describe: 'Port that the katago-analyze-sgf daemon is listening on.',
type: 'number',
default: SERVER_PORT,
})
yargs.positional('COMMAND', {
describe: 'The command. Options: "submit", "list-jobs"',
type: 'string',
})
yargs.positional('PARAMS', {
describe: 'Parameters to the command.',
type: 'string',
})
}
).argv
const {port, COMMAND, PARAMS} = argv
let params
if (PARAMS) {
try {
params = JSON.parse(PARAMS)
} catch {
console.error(`Error: PARAMS must be a valid JSON string.`)
return
}
}
const connection = net.createConnection(port)
connection.on('error', (error) => {
switch (error.code) {
case 'ECONNREFUSED':
console.error(
`Error: Could not connect to server port ${port}. ` +
'Make sure the KataGo analysis server is running.'
)
break
default:
console.error(error)
}
})
connection.on('data', (data) => {
const response = JSON.parse(data)
const {result, error} = response
if (error) {
console.error('Error:')
console.error(error)
} else {
console.log(result)
}
connection.destroy()
})
connection.on('ready', () => {
const request = {method: COMMAND, params, id: 0}
connection.write(JSON.stringify(request))
})
}
if (module === require.main) {
main()
}