-
Notifications
You must be signed in to change notification settings - Fork 1
/
Daemon.pm
378 lines (324 loc) · 8.51 KB
/
Daemon.pm
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env perl
package Daemon;
use feature 'say';
use Cwd;
use File::HomeDir;
use JSON::PP;
use Switch;
use Term::ANSIColor;
use Term::Complete;
use Text::Aligner;
use Text::ASCIITable;
use Text::Table;
use Text::Trim;
use Text::Unidecode;
use Hash::Merge qw( merge );
use HTTP::Response;
use URI;
use LWP::UserAgent;
use Env;
our($progress_index) = 1;
our($progress_step) = 1;
sub write {
if (($#_ + 1) != 4) {
die("Missing arguments for write()");
}
$file = $_[0]; # File name.
$content = $_[1]; # Content to be written into the file.
$create = $_[2]; # 1 or 0: Whether or not create the file.
$method = $_[3]; # > or >> to overwrite or append $content.
if (!-f $file) {
if ($create == 1) {
$method = '>';
}
else {
die("File $file does not exists");
}
}
open(my $fh, $method, $file);
say $fh $content;
close $fh;
}
sub read {
if (($#_ + 1) != 1) {
die("Missing arguments for read()");
}
$file = $_[0]; # Name the file
open(INFO, $file); # Open the file
@lines = <INFO>; # Read it into an array
close(INFO); # Close the file
return @lines; # Print the array
}
sub json_decode_file {
my $file = shift;
my $data = undef;
if ((-s $file) && (open (my $json_stream, $file))) {
local $/ = undef;
my $json = JSON::PP->new->allow_nonref;
$data = $json->decode(<$json_stream>);
close($json_stream);
}
return $data;
}
sub open_default_browser {
my $url = shift;
my $platform = $^O;
my $cmd;
if ($platform eq 'darwin') { $cmd = "open \"$url\""; } # Mac OS X
elsif ($platform eq 'linux') { $cmd = "x-www-browser \"$url\""; } # Linux
elsif ($platform eq 'MSWin32') { $cmd = "start $url"; } # Win95..Win7
if (defined $cmd) {
system($cmd);
} else {
die "Can't locate default browser";
}
}
sub open_default_editor {
my $filename = shift or die "Missing filename to open\n";
if (!-f $filename) {
die "Cannot find $filename! $!\n";
}
my $editor = $ENV{EDITOR} || 'vim';
system $editor => $filename;
}
sub prompt {
my $question = shift;
my $defaultValue = shift;
my @options = shift;
my $max_length = shift;
my $answer = undef;
my $printed_list = 0;
my $hash = 0;
my $remaining = 0;
my $has_options = 0;
if (ref(@options[0]) eq 'HASH') {
$hash = @options[0];
@options = [sort keys %{$hash}];
}
if (ref(@options[0]) eq 'ARRAY') {
$has_options = 1;
}
do {
if ($defaultValue) {
print $question, "[", $defaultValue, "]: ";
}
else {
print $question, ": ";
}
if (!$has_options && $max_length) {
print "[max-length: $max_length]: ";
$remaining = $max_length;
}
else {
$remaining = 0;
}
if (@options[0] && !$printed_list) {
print "\n";
print_list(@options);
$printed_list = 1;
print "» ";
}
$| = 1; # force a flush after our print
if (@options[0]) {
$_ = Complete('', @options);
}
else {
$_ = <STDIN>; # get the input from STDIN
chomp;
}
if ("$defaultValue") {
$answer = $_ ? $_ : $defaultValue; # return $_ if it has a value
}
else {
$answer = $_;
}
if (!$has_options && $max_length) {
$remaining = $max_length - length $answer;
if ($remaining < 0) {
Daemon::printLabel("Max length exceeded: $remaining", "black on_red", 1);
}
}
}
while (!$answer || !length $answer || ($remaining < 0) || ($has_options && !in_array(@options, $answer)));
if ($printed_list) {
print "\n";
}
if ($hash) {
$answer = $hash->{$answer};
}
return $answer;
}
sub array2table {
my $title = shift;
my $items = shift || ();
my $options = shift;
my $default = {
exclude => [],
allow_nested => 1,
extract_nested_key => 'name',
};
$options = merge($default, $options);
my @header = ();
my @header_keys = ();
my @rows = ();
my $header_row = 1;
for my $item (@{$items}) {
if ($header_row) {
for my $key (sort keys %{$item}) {
if (!in_array($options->{exclude}, $key)) {
my $ref = ref($item->{$key});
if (($ref ne 'ARRAY') || ($ref ne 'HASH') || $options->{allow_nested}) {
push(@header, uc $key);
push(@header_keys, $key);
}
}
}
}
$header_row = 0;
my @row = ();
for my $key (@header_keys) {
my $ref = ref($item->{$key});
my $value;
if ((($ref eq 'HASH') || ($ref eq 'ARRAY')) && $options->{allow_nested}) {
if ($ref eq 'HASH') {
if ($options->{full_nested}) {
$value = array2table(0, [$item->{$key}], $options);
}
else {
$value = $item->{$key}->{$options->{extract_nested_key}};
}
}
else {
$value = array2table(0, $item->{$key}, $options);
}
}
else {
$value = $item->{$key};
}
push(@row, $value);
}
push(@rows, [@row]);
}
if (@rows) {
if ($options->{colored}) {
my $table = Text::Table->new(@header);
$table->load(@rows);
&printLabel($title);
return colored(['black on_bright_white'], $table);
}
else {
my $args = $title ? { headingText => $title } : {};
$t = Text::ASCIITable->new($args);
$t->setCols(@header);
for my $row (@rows) {
$t->addRow($row);
}
return $t;
}
}
return '';
}
sub system {
my $command = shift;
my $retMessage = printColor($command, "white on_red");
my $color = $ENV{MEMENTO_SYSTEM_COLOR} || 'bold black';
say Daemon::printColor("▶ $command", $color);
0 == system($command)
or die "There was an error while trying to execute command $retMessage. Fix the problem and try again\n";
}
sub printLabel {
my $label = shift;
my $color = shift || "bold white on_rgb015";
my $lower = shift;
$label = $lower ? $label : uc $label;
say colored([$color], " $label ");
}
sub printColor {
my $label = shift;
my $color = shift || "bold white on_rgb015";
my $upper = shift;
$label = $upper ? uc $label : $label;
return colored([$color], " $label ");
}
sub in_array {
my ($arr, $search_for) = @_;
my %items = map {$_ => 1} @$arr;
return (exists($items{$search_for})) ? 1 : 0;
}
sub print_list {
my $array = shift;
foreach my $item (@{$array}) {
say "- $item";
}
}
sub storage {
my $home = File::HomeDir->my_home;
my $storage = "$home/.memento";
if (!-d $storage) {
mkdir($storage) or die "Cannot create .memento dir in your home directory: $!\n";
}
return $storage;
}
sub http_request {
my $method = shift || 'GET';
my $uri = shift;
my $data = shift || {};
my $headers = shift || {};
my $credentials = shift;
my $client = LWP::UserAgent->new;
my $content = undef;
my $timeout = $ENV{MEMENTO_HTTP_TIMEOUT} ? $ENV{MEMENTO_HTTP_TIMEOUT} : 10;
$client->timeout($timeout);
$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;
$client->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00);
$method = uc $method;
if (!in_array(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], $method)) {
die "Invalid HTTP Method supplied: $method\n";
}
$uri = URI->new($uri);
if ($method eq 'GET') {
my %querystring = %{$data};
$uri->query_form(%querystring);
$data = undef;
}
my $req = HTTP::Request->new($method => $uri);
# Set request credentials.
if ($credentials) {
$req->authorization_basic($credentials->{user}, $credentials->{pass});
}
# Set request headers.
foreach my $key (keys %$headers) {
$req->header($key => $headers->{$key});
}
if ($data) {
$req->content(encode_json $data);
}
my $resp = $client->request($req);
if ($resp->is_success) {
$content = $resp->decoded_content;
}
else {
say "HTTP $method error code: ", $resp->code;
say "HTTP $method error message: ", $resp->message;
die("\n");
}
return $content;
}
sub machine_name {
my $name = shift;
$name = lc unidecode($name);
$name =~ s/(\w)\-([a-z])/$1_$2/g; #converts dashes between a char and a number.
$name =~ s/[^\w\-]+/_/g; #converts anything different from the pattern.
$name =~ s/^_\w{1,2}|_\w{1,2}_|_\w{1,2}$/_/g; #removes short words (<= 2).
$name =~ s/_{2,}/_/g; #removes multiple underscores.
$name =~ s/^_|_$//g; #removes trailing and leading "_".
$name =~ s/_[\w\-]_/_/g; #converts dirty segments to "_".
$name =~ s/_\-_/_/g; #converts "_-_" to "_".
$name =~ s/^_\w{1,2}|_\w{1,2}_|_\w{1,2}$/_/g; #recheck for short words (<= 2).
return $name;
}
sub current_dir_name {
my @dir = split('/', getcwd());
return $dir[-1];
}
1;