-
Notifications
You must be signed in to change notification settings - Fork 1
/
update-list
executable file
·292 lines (222 loc) · 6.06 KB
/
update-list
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/perl -w
##
# Script to update a JSON file with technical postmortem URLs.
#
# You may detect a whiff of Erlang here.
#
# Author: John R. Daily
# Date: 2012-12-26
##
use strict;
use warnings;
use JSON;
use constant BLANK_OK => "__ok";
use Getopt::Long;
sub usage {
return <<END;
update-list <file> [--debug]
The file specified will be appended (technically overwritten) if it
exists.
The JSON will be a list of dictionary values; each value should
contain the URL of the postmortem, the domain of the URL, the first
date of the outage, and any relevant keywords.
--debug means do everything but write the new file. The content that
would be written will be displayed to the terminal instead. No
other helpful debugging information will be provided.
END
}
my ($usage, $input_file, $debug) = parse_args();
if ($usage) {
print usage();
exit;
}
sub parse_args {
my $debug = 0;
my $end = 0;
my $help = 0;
my $input_file = "";
my $result = GetOptions("debug" => \$debug,
"help" => \$help);
$input_file = $ARGV[0] if @ARGV > 0;
$end = ($help || !$input_file || !$result);
return ($end, $input_file, $debug);
}
my $existing_input = read_input($input_file);
#
# Unless $debug is true or the input file is protected, write_file
# will overwrite it
write_file($debug, $input_file,
prompt_for_urls(get_keywords($existing_input),
get_urls($existing_input)),
$existing_input);
sub prompt_for_urls {
my ($keywords, $urls) = @_;
my @results = ();
while(1) {
my ($ok, $data, $keywords, $urls) = prompt_for_entry($keywords, $urls);
return \@results unless $ok eq 'ok';
push(@results, $data);
}
}
sub match_date {
my ($date) = @_;
if ($date =~ /^(\d{4})-(\d{2})-(\d{2})$/) {
if ($1 > 1950 && $1 < 2200
&& $2 > 0 && $2 < 13
&& $3 > 0 && $3 < 32) {
return $date;
}
}
return 0;
}
sub get_keywords {
my ($data) = @_;
my %keys = ();
for my $h (@{$data}) {
for my $k (@{$h->{keywords}}) {
$keys{$k} = 1;
}
}
return \%keys;
}
sub get_urls {
my ($data) = @_;
my %keys = ();
for my $h (@{$data}) {
$keys{$h->{url}} = 1;
}
return \%keys;
}
sub url_fun {
my ($urls) = @_;
return sub {
my $url = shift;
#
# Make sure we have at least one non-whitespace character
return 0 unless $url =~ /\S/;
if (defined $urls->{$url}) {
print STDERR "Already have that URL in our data set\n";
return 0;
}
if (defined $urls->{$url . '/'}) {
print STDERR "Already have that URL in our data set\n";
return 0;
}
# Drop trailing slash and anything that follows for comparison
my $shorter_url = $url;
$shorter_url =~ s#/[^/]*$##;
if (defined $urls->{$shorter_url}) {
print STDERR "Already have $shorter_url in our data set\n";
return 0;
}
return $url;
};
}
sub keyword_fun {
my ($keywords) = @_;
return sub {
my $input = shift;
if ($input eq '?') {
print " * ", join("\n * ", sort keys %{$keywords}), "\n";
return 0;
}
#
# For keywords, we accept empty input
if ($input eq '') {
return BLANK_OK;
}
return $input;
};
}
sub prompt_for_entry {
my ($keywords, $urls) = @_;
my ($ok1, $url) = get_input("URL ('quit' to end)", url_fun($urls));
return ('quit', '', $keywords, $urls) if $ok1 eq 'quit';
die "Aborted\n" unless $ok1 eq 'ok';
my ($ok2, $source) = grab_domain($url);
die "Aborted\n" unless $ok2 eq 'ok';
my ($ok3, $keys) = get_input("Keywords (? for list)", keyword_fun($keywords));
die "Aborted\n" unless $ok3 eq 'ok';
my @keys = split(/[,\s]+/, $keys);
my ($ok4, $date) = get_input("First day of incident (YYYY-MM-DD)", \&match_date);
die "Aborted\n" unless $ok4 eq 'ok';
return ('ok',
{ url => $url,
domain => $source,
keywords => \@keys,
date => $date},
add_values($keywords, \@keys),
add_values($urls, [ $url ])
);
}
sub add_values {
my ($keys, $new) = @_;
for my $key (@{$new}) {
$keys->{$key} = 1;
}
return $keys;
}
sub match_url {
my ($url) = @_;
return $url =~ /\S/;
}
sub grab_domain {
my ($url) = @_;
if ($url =~ m#\w+://([^/]+)/#) {
return ('ok', $1);
}
return ('ok', (split(m#/#, $url))[0]);
}
#
# Strip away leading/trailing whitespace
sub normalize {
my ($str) = @_;
$str =~ s/^\s*//;
$str =~ s/\s*$//;
return $str;
}
sub get_input {
my ($prompt, $match) = @_;
printf("%s ", $prompt);
my $input = <STDIN>;
chomp $input;
$input = normalize($input);
my $matched_input = '';
if ($match) {
$matched_input = $match->($input);
return get_input($prompt, $match)
unless $matched_input;
}
$input = $matched_input
if $matched_input && $matched_input ne BLANK_OK;
if ($input eq 'q' || $input eq 'quit') {
return ('quit', undef);
}
return ('ok', $input);
}
sub write_file {
my ($debug, $file, $new_content, $old_content) = @_;
push(@{$old_content}, @{$new_content});
my $json = JSON->new->allow_nonref;
my $to_print = $json->pretty->encode($old_content);
if ($debug || (-e $file && ! -w $file)) {
print $to_print, "\n";
} else {
open(OUT, ">$file") || die;
print OUT $to_print;
close(OUT);
}
}
#
# Takes a filename, attempts to read it, and returns a list reference
# of hashes, assuming the JSON is constructed to spec
sub read_input {
my ($file) = @_;
return [] unless open(IN, $file);
my @file_content = <IN>;
close(IN);
my $content = join('', @file_content);
return [] unless $content =~ /\S/;
my $json = JSON->new->allow_nonref;
return $json->decode($content);
}