-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlist.ts
142 lines (130 loc) · 3.82 KB
/
list.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
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
import Command from '../../../base'
import {flags} from '@oclif/command'
import cli from 'cli-ux'
import chalk from 'chalk'
import * as utils from '../../../utils'
import jp from 'jsonpath'
export default class TeamUserList extends Command {
static description = 'List PagerDuty Team Members'
static flags = {
...Command.flags,
name: flags.string({
char: 'n',
description: 'Select teams whose names contain the given text',
}),
ids: flags.string({
char: 'i',
description: 'The IDs of teams to list members for.',
exclusive: ['name', 'pipe'],
multiple: true,
}),
keys: flags.string({
char: 'k',
description: 'Additional fields to display. Specify multiple times for multiple fields.',
multiple: true,
}),
json: flags.boolean({
char: 'j',
description: 'output full details as JSON',
exclusive: ['columns', 'filter', 'sort', 'csv', 'extended'],
}),
pipe: flags.boolean({
char: 'p',
description: 'Print user ID\'s only to stdout, for use with pipes.',
exclusive: ['columns', 'sort', 'csv', 'extended', 'json'],
}),
delimiter: flags.string({
char: 'd',
description: 'Delimiter for fields that have more than one value',
default: '\n',
}),
...cli.table.flags(),
}
async run() {
const {flags} = this.parse(TeamUserList)
const params: Record<string, any> = {}
if (flags.name) {
params.query = flags.name
}
let team_ids = []
if (flags.name) {
params.query = flags.name
cli.action.start('Finding teams in PD')
const teams = await this.pd.fetch('teams', {params: params})
if (teams.length === 0) {
cli.action.stop(chalk.bold.red('no teams found matching ') + chalk.bold.blue(flags.name))
this.exit(0)
}
for (const team of teams) {
team_ids.push(team.id)
}
} else if (flags.ids) {
const invalid_ids = utils.invalidPagerDutyIDs(flags.ids)
if (invalid_ids.length > 0) {
this.error(`Invalid team IDs ${chalk.bold.blue(invalid_ids.join(', '))}`, {exit: 1})
}
team_ids = flags.ids
} else {
this.error('You must specify one of: -i, -n', {exit: 1})
}
if (team_ids.length === 0) {
cli.action.stop(chalk.bold.red('no teams specified'))
this.exit(0)
}
let members: any[] = []
for (const team_id of team_ids) {
// eslint-disable-next-line no-await-in-loop
const r = await this.pd.fetchWithSpinner(`teams/${team_id}/members`, {
activityDescription: `Getting members of team ${chalk.bold.blue(team_id)}`,
stopSpinnerWhenDone: false,
})
for (const m of r) {
m.team = {id: team_id}
}
members = [...members, ...r]
}
cli.action.stop(chalk.bold.green('done'))
if (flags.json) {
await utils.printJsonAndExit(members)
}
const columns: Record<string, object> = {
team_id: {
header: 'Team ID',
get: (row: any) => row.team.id,
},
user_id: {
header: 'User ID',
get: (row: any) => row.user.id,
},
user_name: {
header: 'User Name',
get: (row: any) => row.user.summary,
},
role: {
get: (row: any) => row.role,
},
}
if (flags.keys) {
for (const key of flags.keys) {
columns[key] = {
header: key,
get: (row: any) => utils.formatField(jp.query(row, key), flags.delimiter),
}
}
}
const options = {
printLine: this.log,
...flags, // parsed flags
}
if (flags.pipe) {
for (const k of Object.keys(columns)) {
if (k !== 'id') {
const colAny = columns[k] as any
colAny.extended = true
}
}
options['no-header'] = true
}
cli.table(members, columns, options)
}
}