Sky/CmdLine.pm

filter: Statementstmt 35.9% 42 / 117 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.9% 13 / 21 Podpod 100% 1 / 1 totaltotal 30.1% 56 / 186 CC 20 SCAR 47.1
CC
20
Cov
27%
run
272.0
__ANON__
12.0
_write_utf8_file
6.0
CRAP 110.8
↑ Summary Sky/Config/Validate.pm »
1 package App::Sky::CmdLine;
2 1 $App::Sky::CmdLine::VERSION = '0.8.0';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MooX 'late';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Getopt::Long qw(GetOptionsFromArray);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use App::Sky::Config::Validate ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use App::Sky::Manager ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use YAML::XS qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 has 'argv' => ( isa => 'ArrayRef[Str]', is => 'rw', required => 1, );
25  
26 # Better than DATA , IMHO
27 1 my $INIT_YAML_CONFIG_CONTENTS = <<"ENDFILE";
28 ---
29 default_site: homepage
30 sites:
31 homepage:
32 base_upload_cmd:
33 - 'rsync'
34 - '-a'
35 - '-v'
36 - '--progress'
37 - '--inplace'
38 dest_upload_prefix: 'hostgator:public_html/'
39 dest_upload_url_prefix: 'https://www.destsite.tld/'
40 dirs_section: 'dirs'
41 sections:
42 archives:
43 basename_re: '\\.(?:7z|AppImage\\.xz|ova(?:\\.xz)?|tar|tar\\.bz2|tar\\.gz|tar\\.xz|tar\\.zst|zip|exe|rpm)(?:\\.zsync)?\\z'
44 target_dir: 'Files/files/arcs/'
45 code:
46 basename_re: '\\.(?:bash|c|cpp|diff|hs|js|json|log|p6|patch|pl|pm|py|rb|rs|s|scm|spec|ts|vim|yaml|yml)(?:\\.bz2|\\.gz|\\.xz\\|\\.zst)?\\z'
47 target_dir: 'Files/files/code/'
48 dirs:
49 basename_re: '\\.(MYDIR)\\z'
50 target_dir: 'Files/files/dirs/'
51 images:
52 basename_re: '\\.(?:bmp|gif|jpeg|jpg|kra|png|(?:ai|svg|xcf)(?:\\.bz2|\\.gz|\\.xz\\|\\.zst)?|svgz|webp)\\z'
53 target_dir: 'Files/files/images/'
54 music:
55 basename_re: '\\.(?:aac|aup3|m4a|mod|mp3|ogg|s3m|wav|xm)\\z'
56 target_dir: 'Files/files/music/mp3-ogg/'
57 text:
58 basename_re: '\\.(?:asciidoc|docx|epub|html|md|mkdn|ods|odt|pdf|tsv|txt|xml|xhtml|xlsx)(?:\\.bz2|\\.gz|\\.xz\\|\\.zst)?\\z'
59 target_dir: 'Files/files/text/'
60 video:
61 basename_re: '\\.(?:avi|flv|mkv|mp4|mpeg|mpg|ogv|srt|webm)\\z'
62 target_dir: 'Files/files/video/'
63 ENDFILE
64  
65  
66 sub _basic_help
67 0 ▶{
Subroutine _basic_help: not called CC=1 SCAR=6.9
68 0 my ($self) = @_;
69  
70 0 print <<'EOF';
71 sky upload /path/to/myfile.txt
72 sky up-r /path/to/directory
73  
74 Specifying --copy or -x will copy the URL to the clipboard.
75 EOF
76  
77 0 exit(0);
78 }
79  
80 sub _basic_usage
81 0 ▶{
Subroutine _basic_usage: not called CC=1 SCAR=6.9
82 0 my ($self) = @_;
83  
84 0 print "Usage: sky [up|upload] /path/to/myfile.txt\n";
85 0 exit(-1);
86 }
87  
88 sub _is_copy_to_clipboard
89 0 ▶{
Subroutine _is_copy_to_clipboard: not called CC=1 SCAR=6.9
90 0 my ( $self, $flag ) = @_;
91  
92 0 return ( $flag =~ /\A(--copy|-x)\z/ );
93 }
94  
95 sub _shift
96 0 ▶{
Subroutine _shift: not called CC=1 SCAR=6.9
97 0 my $self = shift;
98  
99 0 return shift( @{ $self->argv() } );
100 }
101  
102 sub _write_utf8_file
103 0 ▶{
Subroutine _write_utf8_file: not called CC=2 SCAR=17.9
104 0 my ( $out_path, $contents ) = @_;
105  
106 0 ▶ open my $out_fh, '>:encoding(utf8)', $out_path
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">:encoding(utf8)", $out_path or die "Cannot open '${out_path}' for writing - $!" 0 0 0
107 or die "Cannot open '$out_path' for writing - $!";
108  
109 0 print {$out_fh} $contents;
110  
111 0 close($out_fh);
112  
113 0 return;
114 }
115  
116 sub run
117 0 ▶{
Subroutine run: not called CC=16 SCAR=56.1
Pod documented
118 0 my ($self) = @_;
119  
120 0 my $copy = 0;
121  
122 0 ▶ if ( !@{ $self->argv() } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->argv;}) 0 0 0
123 {
124 0 return $self->_basic_usage();
125 }
126  
127 0 my $verb = shift( @{ $self->argv() } );
128  
129 0 ▶ if ( ( $verb eq '--help' ) or ( $verb eq '-h' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($verb eq "--help" or $verb eq "-h") 0 0 0
Condition0%0/3
$verb eq "--help" or $verb eq "-h"
l!l&&r!l&&!r
000
MC/DC0%0/2
$verb eq "--help" or $verb eq "-h"
$verb eq "--help" $verb eq "-h"
Truth table0%0/3
A or B
A= $verb eq "--help"B= $verb eq "-h"
ABresult
1X1
011
000
130 {
131 0 return $self->_basic_help();
132 }
133  
134 0 ▶ if ( $self->_is_copy_to_clipboard($verb) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_copy_to_clipboard($verb)) 0 0 0
135 {
136 0 $copy = 1;
137  
138 0 $verb = shift( @{ $self->argv() } );
139 }
140  
141 0 ▶ my $_calc_manager = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
142 0 my $dist_config_dir =
143 File::HomeDir->my_dist_config( 'App-Sky', { create => 1 }, );
144  
145 0 my $config_fn =
146 File::Spec->catfile( $dist_config_dir, 'app_sky_conf.yml' );
147  
148 0 ▶ if ( not( -e $config_fn ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $config_fn) 0 0 0
149 {
150 0 _write_utf8_file( $config_fn, $INIT_YAML_CONFIG_CONTENTS );
151 0 warn
152 qq#Populated the "$config_fn" configuration file with initial contents. You should review it.#;
153 }
154  
155 0 my $config = LoadFile($config_fn);
156  
157 0 my $validator =
158 App::Sky::Config::Validate->new( { config => $config } );
159 0 $validator->is_valid();
160  
161 0 return App::Sky::Manager->new(
162 {
163 config => $config,
164 }
165 );
166 0 };
167  
168 0 ▶ my $_handle_results = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
169 0 my ($results) = @_;
170  
171 0 my $upload_cmd = $results->upload_cmd();
172 0 my $urls = $results->urls();
173  
174 0 ▶ if ( ( system { $upload_cmd->[0] } @$upload_cmd ) != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (system({$upload_cmd->[0];} @$upload_cmd) != 0) 0 0 0
175 {
176 0 die "Upload cmd <<@$upload_cmd>> failed with $!";
177 }
178  
179 0 my $URL = $urls->[0]->as_string();
180  
181 0 print "Got URL:\n", $URL, "\n";
182  
183 0 ▶ if ($copy)
Branch: 1 branch
BranchTrueFalseTotal
if ($copy) 0 0 0
184 {
185 0 require Clipboard;
186 0 Clipboard->VERSION('0.19');
187 0 Clipboard->import('');
188 0 Clipboard->copy_to_all_selections($URL);
189 }
190  
191 0 exit(0);
192 0 };
193  
194 0 my $op;
195 0 ▶ if ( ( ( $verb eq 'up' ) || ( $verb eq 'upload' ) ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($verb eq 'up' or $verb eq 'upload') { } 0 0 0
elsif ($verb eq 'up-r' or $verb eq 'upload-recursive') { } 0 0 0
Condition0%0/3
$verb eq "up" or $verb eq "upload"
l!l&&r!l&&!r
000
MC/DC0%0/2
$verb eq "up" or $verb eq "upload"
$verb eq "up" $verb eq "upload"
Truth table0%0/3
A or B
A= $verb eq "up"B= $verb eq "upload"
ABresult
1X1
011
000
196 {
197 0 $op = 'upload';
198 }
199 ▶ elsif ( ( $verb eq 'up-r' ) || ( $verb eq 'upload-recursive' ) )
Condition0%0/3
$verb eq "up-r" or $verb eq "upload-recursive"
l!l&&r!l&&!r
000
MC/DC0%0/2
$verb eq "up-r" or $verb eq "upload-recursive"
$verb eq "up-r" $verb eq "upload-recursive"
Truth table0%0/3
A or B
A= $verb eq "up-r"B= $verb eq "upload-recursive"
ABresult
1X1
011
000
200 {
201 0 $op = "up-r";
202 }
203 else
204 {
205 0 return $self->_basic_usage();
206 }
207  
208 # GetOptionsFromArray(
209 # $self->argv(),
210 # );
211  
212 0 my $filename = $self->_shift();
213  
214 ARGS_LOOP:
215 0 ▶ while ( $filename =~ /\A-/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\A-/u) 0 0 0
216 {
217 0 ▶ if ( $self->_is_copy_to_clipboard($filename) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_copy_to_clipboard($filename)) { } 0 0 0
elsif ($filename eq '--') { } 0 0 0
218 {
219 0 $copy = 1;
220 0 $filename = $self->_shift();
221 }
222 elsif ( $filename eq '--' )
223 {
224 0 $filename = $self->_shift();
225 0 last ARGS_LOOP;
226 }
227 else
228 {
229 0 die qq#Unrecognized argument "$filename"!#;
230 }
231 }
232  
233 0 ▶ if ( not( ( $op eq 'upload' ) ? ( -f $filename ) : ( -d $filename ) ) )
Branch: 2 branches
BranchTrueFalseTotal
unless ($op eq "upload" ? -f $filename : -d $filename) 0 0 0
$op eq 'upload' ? : 0 0 0
234 {
235 0 ▶ if ( $op eq 'upload' )
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "upload") 0 0 0
236 {
237 0 die qq#"up" can only upload files. '$filename' is not a file!#;
238 }
239 die
240 0 "Can only upload directories. '$filename' is not a valid directory name.";
241 }
242  
243 0 ▶ my $meth =
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'upload' ? : 0 0 0
244 $op eq 'upload' ? 'get_upload_results' : 'get_recursive_upload_results';
245  
246 0 $_handle_results->(
247 scalar(
248 $_calc_manager->()->$meth(
249 {
250 filenames => [$filename],
251 }
252 )
253 )
254 );
255  
256 0 return;
257 }
258  
259 1 1;
260  
261 __END__
↑ Summary Sky/Config/Validate.pm »