-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogs.js
64 lines (49 loc) · 1.58 KB
/
logs.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
const { exec } = require('child_process')
const _ = require('lodash')
const logs = require('../../config.js').logs
const Command = require('../command.js')
const Helpers = require('../../util/helpers.js')
module.exports = class Logs extends Command {
constructor() {
super('logs')
}
call(bot, opts, respond) {
if (!this.adminCallable(opts)) return
const options = { lines: '-n 100' }
const argString = _.join(opts.args, ' ').trim()
if (this.isNumber(argString)) {
options.lines = `-n ${argString}`
}
if (this.isTimeRange(argString)) {
options[since] = `--since "${argString}"`
}
this.getJournalLogs(options, (paste) => respond(`Last ${options.lines.split('-n ')[1]} lines: ${paste}`))
}
async uploadLogs(data, respond) {
const res = await Helpers.uploadText(data)
return respond(res.body.path)
}
getJournalLogs(options, respond) {
let cmd = `journalctl -u botto.service -q --no-pager --no-hostname `
Object.keys(options).forEach(k => cmd += options[k] )
return exec(cmd, (e, out, err) => {
if (e) {
console.log(e.message)
return respond('Ironically, something went wrong and you need to check logs')
}
if (err) {
console.log('Error retrieving local logs: ' + err)
return respond(err)
}
this.uploadLogs(out, (url) => respond(url))
})
}
// e.g. !logs 15
isNumber(argString) {
return /^\d+$/.test(argString)
}
// e.g. !logs 2 days ago OR !logs 2d ago
isTimeRange(argString) {
return /^((\d+\w)|(\d+\s\w+))\sago$/.test(argString)
}
}