line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Cmd::stream; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
137405
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
170467
|
|
|
1
|
|
|
|
|
11
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.0505'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
343
|
use parent 'Catmandu::Cmd'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
10
|
|
8
|
1
|
|
|
1
|
|
114947
|
use Catmandu; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
8
|
|
9
|
1
|
|
|
1
|
|
290
|
use Catmandu::Util; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
10
|
1
|
|
|
1
|
|
5
|
use Carp; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
55
|
|
11
|
1
|
|
|
1
|
|
5
|
use IO::Handle; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
12
|
1
|
|
|
1
|
|
5
|
use IO::File; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
166
|
|
13
|
1
|
|
|
1
|
|
7
|
use namespace::clean; |
|
1
|
|
|
|
|
7
|
|
|
1
|
|
|
|
|
4
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub command_opt_spec { |
16
|
2
|
|
|
2
|
1
|
142026
|
(["verbose|v", ""], ["delete", "delete existing items first"],); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub command { |
20
|
2
|
|
|
2
|
1
|
4395
|
my ($self, $opts, $args) = @_; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
15
|
my ($from_args, $from_opts, $into_args, $into_opts) |
23
|
|
|
|
|
|
|
= $self->_parse_options($args); |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
183
|
my $id_name; |
26
|
|
|
|
|
|
|
my $bag_name; |
27
|
2
|
|
|
|
|
0
|
my $store_name; |
28
|
2
|
|
|
|
|
0
|
my $store_opts; |
29
|
2
|
|
|
|
|
0
|
my $filename; |
30
|
2
|
|
|
|
|
0
|
my $upload; |
31
|
|
|
|
|
|
|
|
32
|
2
|
100
|
|
|
|
37
|
if ($bag_name = $from_opts->{bag}) { |
|
|
50
|
|
|
|
|
|
33
|
1
|
|
|
|
|
5
|
delete $from_opts->{bag}; |
34
|
1
|
|
33
|
|
|
7
|
$id_name = $from_opts->{id} // $self->usage_error("need a --id"); |
35
|
1
|
|
|
|
|
2
|
delete $from_opts->{id}; |
36
|
1
|
|
|
|
|
2
|
$store_name = $from_args->[0]; |
37
|
1
|
|
|
|
|
3
|
$store_opts = $from_opts; |
38
|
1
|
|
|
|
|
2
|
$filename = $into_args->[0]; |
39
|
1
|
|
|
|
|
11
|
$upload = 0; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif ($bag_name = $into_opts->{bag}) { |
42
|
1
|
|
|
|
|
4
|
delete $into_opts->{bag}; |
43
|
1
|
|
33
|
|
|
5
|
$id_name = $into_opts->{id} // $self->usage_error("need a --id"); |
44
|
1
|
|
|
|
|
5
|
delete $into_opts->{id}; |
45
|
1
|
|
|
|
|
2
|
$store_name = $into_args->[0]; |
46
|
1
|
|
|
|
|
2
|
$store_opts = $into_opts; |
47
|
1
|
|
|
|
|
2
|
$filename = $from_args->[0]; |
48
|
1
|
|
|
|
|
3
|
$upload = 1; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
0
|
|
|
|
|
0
|
$self->usage_error("need a --bag"); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
19
|
my $store = Catmandu->store($store_name, $store_opts); |
55
|
|
|
|
|
|
|
|
56
|
2
|
100
|
|
|
|
49
|
return $upload |
57
|
|
|
|
|
|
|
? $self->upload_file($store, $bag_name, $id_name, $filename) |
58
|
|
|
|
|
|
|
: $self->download_file($store, $bag_name, $id_name, $filename); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub upload_file { |
62
|
1
|
|
|
1
|
0
|
5
|
my ($self, $store, $bag_name, $id_name, $filename) = @_; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
7
|
unless ($store->bag->exists($bag_name)) { |
65
|
1
|
|
50
|
|
|
8
|
$store->bag->add({_id => $bag_name}) // return undef; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
73
|
my $bag = $store->bag->files($bag_name); |
69
|
|
|
|
|
|
|
|
70
|
1
|
50
|
|
|
|
158
|
return undef unless $bag; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
2
|
my $io; |
73
|
|
|
|
|
|
|
|
74
|
1
|
50
|
33
|
|
|
8
|
if (!defined($filename) || $filename eq '-') { |
75
|
0
|
|
|
|
|
0
|
$io = IO::Handle->new(); |
76
|
0
|
|
|
|
|
0
|
$io->fdopen(fileno(STDIN), "r"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
else { |
79
|
1
|
|
|
|
|
11
|
$io = IO::File->new("<$filename"); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
112
|
croak "can't open $filename for reading" unless defined($io); |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
10
|
$bag->upload($io, $id_name); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub download_file { |
88
|
1
|
|
|
1
|
0
|
7
|
my ($self, $store, $bag_name, $id_name, $filename) = @_; |
89
|
|
|
|
|
|
|
|
90
|
1
|
50
|
|
|
|
5
|
unless ($store->bag->exists($bag_name)) { |
91
|
0
|
|
|
|
|
0
|
croak "No such bag `$bag_name`"; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
1
|
|
|
|
|
6
|
my $bag = $store->bag->files($bag_name); |
95
|
|
|
|
|
|
|
|
96
|
1
|
50
|
|
|
|
2531
|
return undef unless $bag; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
24
|
my $file = $bag->get($id_name); |
99
|
|
|
|
|
|
|
|
100
|
1
|
50
|
|
|
|
6
|
unless ($file) { |
101
|
0
|
|
|
|
|
0
|
croak "No such file `$id_name` in `$bag_name`"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
1
|
|
|
|
|
2
|
my $io; |
105
|
|
|
|
|
|
|
|
106
|
1
|
50
|
33
|
|
|
6
|
if (!defined($filename) || $filename eq '-') { |
107
|
1
|
|
|
|
|
6
|
$io = bless(\*STDOUT => 'IO::File'); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
0
|
|
|
|
|
0
|
$io = IO::File->new(">$filename"); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
1
|
|
|
|
|
7
|
$io->binmode(':encoding(UTF-8)'); |
114
|
|
|
|
|
|
|
|
115
|
1
|
50
|
|
|
|
259
|
croak "can't open $filename for writing" unless defined($io); |
116
|
|
|
|
|
|
|
|
117
|
1
|
|
|
|
|
8
|
$bag->stream($io, $file); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
1; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
__END__ |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=pod |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 NAME |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Catmandu::Cmd::stream - import and export streams into a Catmandu::FileStore |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 EXAMPLES |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
catmandu stream /tmp/data.txt to File::Simple --root t/data --bag 1234 |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
catmandu stream File::Simple --root t/data --bag 1234 to /tmp/data |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
catmandu help stream |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |