line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Cmd::stream; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
136506
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
169529
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.0505'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
380
|
use parent 'Catmandu::Cmd'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
9
|
|
8
|
1
|
|
|
1
|
|
110637
|
use Catmandu; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
9
|
1
|
|
|
1
|
|
289
|
use Catmandu::Util; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
10
|
1
|
|
|
1
|
|
6
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
58
|
|
11
|
1
|
|
|
1
|
|
6
|
use IO::Handle; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
12
|
1
|
|
|
1
|
|
5
|
use IO::File; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
123
|
|
13
|
1
|
|
|
1
|
|
6
|
use namespace::clean; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
4
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub command_opt_spec { |
16
|
2
|
|
|
2
|
1
|
138166
|
(["verbose|v", ""], ["delete", "delete existing items first"],); |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub command { |
20
|
2
|
|
|
2
|
1
|
4374
|
my ($self, $opts, $args) = @_; |
21
|
|
|
|
|
|
|
|
22
|
2
|
|
|
|
|
13
|
my ($from_args, $from_opts, $into_args, $into_opts) |
23
|
|
|
|
|
|
|
= $self->_parse_options($args); |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
190
|
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
|
|
|
|
24
|
if ($bag_name = $from_opts->{bag}) { |
|
|
50
|
|
|
|
|
|
33
|
1
|
|
|
|
|
3
|
delete $from_opts->{bag}; |
34
|
1
|
|
33
|
|
|
6
|
$id_name = $from_opts->{id} // $self->usage_error("need a --id"); |
35
|
1
|
|
|
|
|
2
|
delete $from_opts->{id}; |
36
|
1
|
|
|
|
|
3
|
$store_name = $from_args->[0]; |
37
|
1
|
|
|
|
|
2
|
$store_opts = $from_opts; |
38
|
1
|
|
|
|
|
2
|
$filename = $into_args->[0]; |
39
|
1
|
|
|
|
|
3
|
$upload = 0; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif ($bag_name = $into_opts->{bag}) { |
42
|
1
|
|
|
|
|
4
|
delete $into_opts->{bag}; |
43
|
1
|
|
33
|
|
|
15
|
$id_name = $into_opts->{id} // $self->usage_error("need a --id"); |
44
|
1
|
|
|
|
|
5
|
delete $into_opts->{id}; |
45
|
1
|
|
|
|
|
4
|
$store_name = $into_args->[0]; |
46
|
1
|
|
|
|
|
3
|
$store_opts = $into_opts; |
47
|
1
|
|
|
|
|
2
|
$filename = $from_args->[0]; |
48
|
1
|
|
|
|
|
2
|
$upload = 1; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
else { |
51
|
0
|
|
|
|
|
0
|
$self->usage_error("need a --bag"); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
2
|
|
|
|
|
26
|
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
|
4
|
my ($self, $store, $bag_name, $id_name, $filename) = @_; |
63
|
|
|
|
|
|
|
|
64
|
1
|
50
|
|
|
|
6
|
unless ($store->bag->exists($bag_name)) { |
65
|
1
|
|
50
|
|
|
7
|
$store->bag->add({_id => $bag_name}) // return undef; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
12
|
my $bag = $store->bag->files($bag_name); |
69
|
|
|
|
|
|
|
|
70
|
1
|
50
|
|
|
|
206
|
return undef unless $bag; |
71
|
|
|
|
|
|
|
|
72
|
1
|
|
|
|
|
2
|
my $io; |
73
|
|
|
|
|
|
|
|
74
|
1
|
50
|
33
|
|
|
9
|
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
|
|
|
|
|
9
|
$io = IO::File->new("<$filename"); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
1
|
50
|
|
|
|
94
|
croak "can't open $filename for reading" unless defined($io); |
83
|
|
|
|
|
|
|
|
84
|
1
|
|
|
|
|
7
|
$bag->upload($io, $id_name); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub download_file { |
88
|
1
|
|
|
1
|
0
|
5
|
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
|
|
|
|
2530
|
return undef unless $bag; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
22
|
my $file = $bag->get($id_name); |
99
|
|
|
|
|
|
|
|
100
|
1
|
50
|
|
|
|
7
|
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
|
|
|
|
|
7
|
$io = bless(\*STDOUT => 'IO::File'); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
else { |
110
|
0
|
|
|
|
|
0
|
$io = IO::File->new(">$filename"); |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
1
|
|
|
|
|
9
|
$io->binmode(':encoding(UTF-8)'); |
114
|
|
|
|
|
|
|
|
115
|
1
|
50
|
|
|
|
295
|
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 |