bin/photobear

filter: Statementstmt 54.4% 49 / 90 Branchbran 29.4% 10 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod n/a totaltotal 49.2% 69 / 140 CC 1 SCAR 1.7
CC
1
Cov
47%
__ANON__
2.0
usage
2.0
BEGIN
1.0
CRAP 1.2
« blib/lib/App/Photobear.pm ↑ Summary
1 #!/usr/bin/env perl
2 #ABSTRACT: CLI for Photobear powered image processing
3 #PODNAME: photobear
4 1 ▶use v5.18;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Spec::Functions qw(catfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Term::ANSIColor qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶if (-d 'lib') {
Branch: 1 branch
BranchTrueFalseTotal
if (-d "lib") 1 0 1
12 1 ▶ use lib 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14 1 ▶use App::Photobear;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 my $BASE = basename($0);
16 1 my $HOME = $ENV{HOME};
17 1 my $CONFIG_FILE = catfile(catfile($HOME, '.config'), 'photobear.ini');
18 1 my $CONFIG = App::Photobear::loadconfig($CONFIG_FILE);
19 1 ▶my $API_KEY = $CONFIG->{"api_key"} || $ENV{PHOTOBEAR_API_KEY};
Condition33%1/3
$CONFIG->{'api_key'} || $ENV{'PHOTOBEAR_API_KEY'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$CONFIG->{'api_key'} || $ENV{'PHOTOBEAR_API_KEY'}
$CONFIG->{'api_key'} $ENV{'PHOTOBEAR_API_KEY'}
Truth table33%1/3
A || B
A= $CONFIG->{'api_key'}B= $ENV{'PHOTOBEAR_API_KEY'}
ABresult
1X1
011
000
20 1 my $opt_config_file;
21 my $opt_api_key;
22 1 my $opt_verbose;
23 1 my $opt_outdir = ".";
24 1 my @MODES = @App::Photobear::MODES;
25 GetOptions(
26 'o|outdir=s' => \$opt_outdir,
27 'api-key=s' => \$opt_api_key,
28 'config=s' => \$opt_config_file,
29 'verbose' => \$opt_verbose,
30 0 ▶ 'h|help' => sub { usage(); exit 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 1 );
32  
33 1 ▶if ($opt_config_file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_config_file) 0 1 1
34 0 ▶ say STDERR YELLOW, "# Using config file $opt_config_file", RESET if $opt_verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_verbose 0 0 0
35 0 $CONFIG = App::Photobear::loadconfig($opt_config_file);
36 0 ▶ $API_KEY = $CONFIG->{"api_key"} if $CONFIG->{"api_key"};
Branch: 1 branch
BranchTrueFalseTotal
if $CONFIG->{"api_key"} 0 0 0
37 }
38 1 ▶if ($opt_api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_api_key) 0 1 1
39 0 ▶ if (not defined $API_KEY) {#
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $API_KEY) 0 0 0
40 0 ▶ say STDERR YELLOW, "# Saving API KEY to $CONFIG_FILE", RESET if $opt_verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_verbose 0 0 0
41 0 $CONFIG->{"api_key"} = $opt_api_key;
42 0 App::Photobear::writeconfig($CONFIG_FILE, $CONFIG);
43 }
44
45 }
46  
47 1 ▶if (not defined $API_KEY) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $API_KEY) 0 1 1
48 0 usage();
49 0 ▶ say STDERR RED, "\n# No API KEY found [$CONFIG_FILE]", RESET if $opt_verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $opt_verbose 0 0 0
50 0 say STDERR "ERROR: API-KEY is required, add it to $CONFIG_FILE or use --api-key option, or the \$PHOTOBEAR_API_KEY environment variable";
51 0 exit 1;
52 }
53  
54 1 my $mode = shift @ARGV;
55 1 my $url = shift @ARGV;
56  
57  
58 1 ▶if (not defined $mode) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $mode) { } 0 1 1
elsif (not grep({$_ eq $mode;} @MODES)) { } 0 1 1
59 0 usage();
60 0 say STDERR RED, "\n ERROR: No command specified", RESET;
61 0 exit 1;
62 4 } elsif (not grep { $_ eq $mode } @MODES) {
63
64 0 usage();
65 0 say STDERR RED, "\nERROR: Unknown mode $mode", RESET;
66 0 exit 1;
67 }
68  
69 # Check URL
70 1 ▶if (not defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $url) { } 0 1 1
71 0 usage();
72 0 say STDERR "\nERROR: No URL specified";
73 0 exit 1;
74 } else {
75 1 my $check = App::Photobear::url_type($url);
76 1 ▶ if (not defined $check) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $check) { } 0 1 1
elsif (not $check =~ /image/iu) { } 0 1 1
77 0 say STDERR "\nERROR: Invalid URL $url";
78 0 exit 1;
79 } elsif ($check !~/image/i) {
80 0 say STDERR "\nWARNING: URL $url might not point to an image";
81 }
82 }
83  
84 1 ▶if (! -d $opt_outdir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opt_outdir) 0 1 1
85 0 eval { mkdir $opt_outdir };
86 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
87 0 say STDERR "\nERROR: Cannot create output directory $opt_outdir";
88 0 exit 1;
89 }
90 }
91 1 my $out = App::Photobear::photobear($API_KEY, $mode, $url);
92  
93 0 ▶if ($out->{"status"} ne "success") {
Branch: 1 branch
BranchTrueFalseTotal
if ($out->{'status'} ne 'success') { } 0 0 0
94 0 say Dumper $out;
95 0 say STDERR "\nWRONG REQUEST\n";
96 0 exit 1;
97 } else {
98 0 say STDERR "URL: ", GREEN $out->{"data"}->{"result_url"}, RESET;
99 0 my $dest = catfile($opt_outdir, basename($out->{"data"}->{"result_url"}));
100 0 say STDERR "Saving to: ", GREEN, $dest, RESET;
101 0 App::Photobear::download($out->{"data"}->{"result_url"}, $dest);
102 0 exit 0;
103 }
104  
105 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
106  
107 0 my $modes = join(' ', @MODES);
108 0 print <<EOF;
109 Usage: $BASE [options] <command> <url>
110  
111 Commands:
112 $modes
113
114 General options:
115 --api-key <key> API key to use
116 --config <file> Config file to use [~/.config/photobear.ini]
117 --verbose Print verbose output
118 --help Print this help message
119 EOF
120 }
121  
122 __END__
« blib/lib/App/Photobear.pm ↑ Summary