-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·175 lines (164 loc) · 4.89 KB
/
index.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const program = require('commander');
const homedir = require('os').homedir();
const help = require('./src/help');
const Config = require('./src/config/config');
const Snooze = require('./src/snooze/snooze');
const config = new Config();
if (!fs.existsSync(path.join(homedir, '.pd-snooze'))) {
program
.command('set-config')
.description('Create the config required to interact with PagerDuty api')
.option('--apikey <APIKEY>', 'your apiKey')
.option('--email <EMAIL>', 'your email')
.option('--timezone <TIMEZONE>', 'your time-zone')
.action(({ apikey, email, timezone }) => {
if (apikey && email && timezone) {
config.setConfig({ apikey, email, timezone });
} else {
console.log(
chalk.red(
'Error setting config, ensure you are setting the config correctly'
)
);
program.help();
}
});
} else {
const snooze = new Snooze();
program
.command('set-config')
.description(
'Create the config required to interact with the PagerDuty API'
)
.option('--apikey <APIKEY>', 'your apiKey')
.option('--email <EMAIL>', 'your email')
.option('--timezone <TIMEZONE>', 'your time-zone')
.action(({ apikey, email, timezone }) => {
if (apikey && email && timezone) {
config.setConfig({ apikey, email, timezone });
} else {
console.log(
chalk.red(
'Error setting config, ensure you are setting the config correctly'
)
);
program.help();
}
});
program
.command('current-config')
.description('Output current config')
.action(() => {
config.getConfig().then(res => console.log(res));
});
program
.command('update-config')
.description('Update single or multiple values in the config.')
.option('--apikey <APIKEY>', 'your apiKey')
.option('--email <EMAIL>', 'your email')
.option('--timezone <TIMEZONE>', 'your time-zone')
.action(({ apikey, email, timezone }) => {
if (apikey || email || timezone) {
config.updateConfig({ apikey, email, timezone }).catch(console.log);
} else {
console.log(
chalk.red(
'Error updating config, ensure you are passing the correct flags and values'
)
);
program.help();
}
});
program
.command('start')
.description('Put all services or a particular service into maintenance')
.option('-a, --all', 'Put all services into maintenance mode')
.option(
'-s, --service <SERVICE_NAME>',
'Put a single service into maintenance mode'
)
.option('-d, --duration <min>', 'Specify the duration in minutes.')
.action(({ all, service, duration }) => {
if (all) {
snooze.init({
option: 'start',
data: {
value: 'all',
duration,
},
});
} else if (service) {
snooze.init({
option: 'start',
data: {
value: service,
duration,
},
});
} else {
console.log(
chalk.red(
'Missing flag, specify if you want to start all services or a single service.'
)
);
program.help();
}
});
program
.command('end')
.description(
'End all maintenance windows or a window containing a particular service'
)
.option('-a, --all', 'End all maintenance windows')
.option(
'-s, --service <SERVICE_NAME>',
'End the maintenance window that contains this service'
)
.action(({ all, service }) => {
if (all) {
snooze.init({
option: 'end',
data: {
value: 'all',
},
});
} else if (service) {
snooze.init({
option: 'end',
data: {
value: service,
},
});
} else {
console.log(
chalk.red(
'Missing flag, specify if you want to end all maintenance windows or a maintenance window for a single service.'
)
);
program.help();
}
});
program
.command('list')
.description('List all services or open maintenance windows')
.option('-s , --services', 'List all your active services in PagerDuty')
.option('-m , --maintenance', 'List all your open maintenance windows')
.action(({ services, maintenance }) => {
if (services) {
snooze.init({ option: 'services' });
} else if (maintenance) {
snooze.init({ option: 'maintenance' });
} else {
console.log(chalk.red('Missing flag, specify what you want to list.'));
program.help();
}
});
}
program.version('2.2.2', '-v, --version');
program.on('--help', () => help());
program.parse(process.argv);
if (!program.args.length) help();