-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessagepack2yaml
executable file
·59 lines (51 loc) · 1 KB
/
messagepack2yaml
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
#!/usr/bin/env perl
#
# $Id: messagepack2yaml,v 1.3 2011/06/28 06:14:42 ryo Exp $
#
# Copyright(c) 2011 Ryo SHIMIZU <[email protected]>
#
use strict;
use warnings;
use Data::MessagePack;
use Getopt::Std;
use YAML::XS;
use LWP::Simple;
sub usage {
die <<__USAGE__;
usage: messagepack2yaml [options] [file ...]
-q quiet
__USAGE__
}
my %opts;
getopts('q', \%opts) or usage();
if ($#ARGV >= 0) {
for my $file (@ARGV) {
print "== $file ==\n" if (($#ARGV > 0) && !exists($opts{q}));
messagepack2yaml(scalar(fileread($file)));
}
} else {
my $file = join("", <>);
messagepack2yaml($file);
}
exit;
sub messagepack2yaml {
my $messagepack = shift;
my $data = Data::MessagePack->unpack($messagepack);
print YAML::XS::Dump($data);
}
sub fileread {
my $file = shift;
if ($file =~ m#^(http|https|ftp)://#i) {
my $json = get($file);
unless (defined $json) {
warn "$file: error\n";
$json = '{}';
}
$json;
} else {
open my $fh, "<", $file or die "open: $file: $!\n";
local $/;
undef $/;
<$fh>;
}
}