-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathopen.ts
75 lines (65 loc) · 2.35 KB
/
open.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
import Command from '../../base'
import {flags} from '@oclif/command'
import chalk from 'chalk'
import cli from 'cli-ux'
import getStream from 'get-stream'
import * as utils from '../../utils'
export default class IncidentOpen extends Command {
static description = 'Open PagerDuty Incidents in your browser'
static flags = {
...Command.flags,
me: flags.boolean({
char: 'm',
description: 'Open all incidents assigned to me',
exclusive: ['ids'],
}),
ids: flags.string({
char: 'i',
description: 'Incident ID\'s to open. Specify multiple times for multiple incidents.',
multiple: true,
exclusive: ['me'],
}),
pipe: flags.boolean({
char: 'p',
description: 'Read incident ID\'s from stdin.',
exclusive: ['me', 'ids'],
}),
}
async run() {
const {flags} = this.parse(IncidentOpen)
let incident_ids = []
if (flags.me) {
const me = await this.me(true)
const params = {user_ids: [me.user.id]}
const incidents = await this.pd.fetchWithSpinner('incidents', {params: params, activityDescription: 'Getting incidents from PD'})
if (incidents.length === 0) {
this.error('No incidents found.', {exit: 1})
}
incident_ids = incidents.map((e: { id: any }) => e.id)
} else if (flags.ids || flags.pipe) {
if (flags.ids) {
incident_ids = utils.splitDedupAndFlatten(flags.ids)
} else if (flags.pipe) {
const str: string = await getStream(process.stdin)
incident_ids = utils.splitDedupAndFlatten([str])
}
} else {
this.error('You must specify one of: -i, -m, -p', {exit: 1})
}
cli.action.start('Finding your PD domain')
const domain = await this.pd.domain()
this.log('Incident URLs:')
const urlstrings: string[] = incident_ids.map(x => chalk.bold.blue(`https://${domain}.pagerduty.com/incidents/${x}`))
this.log(urlstrings.join('\n') + '\n')
cli.action.start(`Opening ${incident_ids.length} incidents in the browser`)
try {
for (const incident_id of incident_ids) {
await cli.open(`https://${domain}.pagerduty.com/incidents/${incident_id}`)
}
} catch (error) {
cli.action.stop(chalk.bold.red('failed!'))
this.error('Couldn\'t open your browser. Are you running as root?', {exit: 1})
}
cli.action.stop(chalk.bold.green('done'))
}
}