-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.ts
110 lines (105 loc) · 3.22 KB
/
config.ts
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
import fs from 'fs'
import Joi from '@hapi/joi'
// import { fatal } from './log'
export type Config = {
// To see the list of all your chats, run $ keybase chat api -p -m '{"method": "list"}'
// Or, more conveniently, with jq:
// $ keybase chat api -p -m '{"method": "list"}' | jq '.result.conversations'
// This accepts strings of the following forms:
// - you,them
// - family#general
// - $id$0000f0b5c2c2211c8d67ed15e75e656c7862d086e9245420892a7de62cd9ec58
chats: string[],
init: {
type: 'init',
username: string,
paperkey: string
} | {
// NOTE: The watcher doesn't seem to collect user's own messages with this option
// NOTE: It seems like some messages may be outdated with this option
type: 'initFromRunningService'
},
watcher: {
enabled: boolean,
timeout: number // seconds
},
attachments: {
// Download attachments
download: boolean,
directory: string
},
// // (Incremental export is not implemented)
// incremental: {
// enabled: boolean,
// sessionFile: string // like "keybase-export.session"
// },
messageTypes: {
reactions: boolean,
reactionMessages: boolean,
systemMessages: boolean,
headline: boolean
},
jsonl: {
enabled: boolean,
file: string
},
elasticsearch: {
enabled: boolean,
indexPattern: string,
config: Record<string, any> // ElasticSearch config
},
eol: string
}
const schema = Joi.object({
chats: Joi.array().items(Joi.string()).required(),
init: Joi.alternatives(
Joi.object({
type: Joi.string().valid('init').required(),
username: Joi.string().required(),
paperkey: Joi.string().required()
}),
Joi.object({
type: Joi.string().valid('initFromRunningService').required()
})
).required(),
watcher: Joi.object({
enabled: Joi.boolean().default(false),
timeout: Joi.number().default(20)
}).default(),
// incremental: Joi.object({
// enabled: Joi.boolean(),
// sessionFile: Joi.string()
// }),
messageTypes: Joi.object({
reactions: Joi.boolean().default(true),
reactionMessages: Joi.boolean().default(true),
systemMessages: Joi.boolean().default(true),
headline: Joi.boolean().default(true)
}).default(),
attachments: Joi.object({
download: Joi.boolean().default(false),
directory: Joi.string().default('attachments')
}).default().unknown(true),
jsonl: Joi.object({
enabled: Joi.boolean().default(false),
file: Joi.string().default('export.jsonl')
}).default(),
elasticsearch: Joi.object({
enabled: Joi.boolean().default(false),
indexPattern: Joi.string().default('keybase_$channelname$'),
config: Joi.object().default({ host: 'localhost:9200', log: 'info' })
}).default(),
eol: Joi.string().default('\n')
}).unknown(true)
let config: Config | null = null
export function initConfig (filename: string): void {
if (config != null) throw new Error('Config is already initialized')
const untrustedConfig = JSON.parse(fs.readFileSync(filename).toString())
const result = schema.validate(untrustedConfig)
if (result.error) throw result.error
config = result.value
}
export function getConfig (): Config {
if (config == null) throw new Error('Config is not initialized')
return config
}