| 1 | #!perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 18 | ▶ | use Getopt::Long qw( GetOptions ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 18 | ▶ | use Open::This qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | editor_args_from_parsed_text | ||||||||||||||||||||||||||||
| 9 | maybe_get_url_from_parsed_text | ||||||||||||||||||||||||||||
| 10 | parse_text | ||||||||||||||||||||||||||||
| 11 | 18 | ); | |||||||||||||||||||||||||||
| 12 | 18 | ▶ | use Pod::Usage qw( pod2usage ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 18 | ▶ | pod2usage("Error: missing argument\n") unless @ARGV; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 18 | my $browse; | |||||||||||||||||||||||||||
| 17 | my $editor; | ||||||||||||||||||||||||||||
| 18 | 18 | my $help; | |||||||||||||||||||||||||||
| 19 | 18 | my $json; | |||||||||||||||||||||||||||
| 20 | 18 | my $print; | |||||||||||||||||||||||||||
| 21 | 18 | my $version; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 18 | my $options_ok = GetOptions( | |||||||||||||||||||||||||||
| 24 | 'browse|b' => \$browse, | ||||||||||||||||||||||||||||
| 25 | 'editor|e=s' => \$editor, | ||||||||||||||||||||||||||||
| 26 | 'help|h' => \$help, | ||||||||||||||||||||||||||||
| 27 | 'json|j' => \$json, | ||||||||||||||||||||||||||||
| 28 | 'print|p' => \$print, | ||||||||||||||||||||||||||||
| 29 | 'version|v' => \$version, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 18 | ▶ | if ($help) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 2 | pod2usage( | |||||||||||||||||||||||||||
| 34 | -exitval => 0, | ||||||||||||||||||||||||||||
| 35 | -output => \*STDOUT, | ||||||||||||||||||||||||||||
| 36 | -verbose => 99, | ||||||||||||||||||||||||||||
| 37 | -sections => [qw( SYNOPSIS PARAMETERS )], | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 16 | ▶ | if ($version) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 2 | print STDOUT 'ot ', Open::This->VERSION, "\n"; | |||||||||||||||||||||||||||
| 43 | 2 | exit(0); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # With a target left, carry on so unquoted text with a stray -foo still opens. | ||||||||||||||||||||||||||||
| 47 | 14 | ▶ | if ( !$options_ok && !@ARGV ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options_ok or @ARGV
MC/DC100%2/2
$options_ok or @ARGV
$options_ok
@ARGV
Truth table100%3/3
A or B
A= $options_okB= @ARGV
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | exit_with_error( 'Invalid options', undef, 2 ) if $json; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | pod2usage(2); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 12 | my $parsed = parse_text(@ARGV); | |||||||||||||||||||||||||||
| 53 | 12 | ▶ | if ( !$parsed ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | exit_with_error( 'Could not locate file', join ' ', @ARGV ); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 11 | ▶ | if ($browse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 2 | maybe_get_url_from_parsed_text($parsed); | |||||||||||||||||||||||||||
| 59 | 2 | ▶ | if ( !exists $parsed->{remote_file_url} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | exit_with_error( 'Could not find remote repository', $parsed ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | require Browser::Open; | |||||||||||||||||||||||||||
| 64 | 2 | Browser::Open::open_browser( $parsed->{remote_file_url} ); | |||||||||||||||||||||||||||
| 65 | 2 | exit(0); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 9 | ▶ | local $ENV{EDITOR} = $editor || $ENV{EDITOR}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$editor || $ENV{'EDITOR'}
MC/DC0%0/2
$editor || $ENV{'EDITOR'}
$editor
$ENV{'EDITOR'}
Truth table33%1/3
A || B
A= $editorB= $ENV{'EDITOR'}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 9 | ▶ | unless ( $ENV{EDITOR} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | exit_with_error( | |||||||||||||||||||||||||||
| 72 | 'Please set your $EDITOR env var or use the --editor arg'); | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 9 | my @editor_args = editor_args_from_parsed_text($parsed); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 9 | ▶ | if ($json) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 4 | require Cpanel::JSON::XS; ## no perlimports | |||||||||||||||||||||||||||
| 79 | my $json = Cpanel::JSON::XS->new->canonical->encode( | ||||||||||||||||||||||||||||
| 80 | { | ||||||||||||||||||||||||||||
| 81 | editor => $ENV{EDITOR}, | ||||||||||||||||||||||||||||
| 82 | 4 | editor_args => \@editor_args, | |||||||||||||||||||||||||||
| 83 | success => Cpanel::JSON::XS::true(), | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||
| 86 | 4 | print STDOUT $json, "\n"; | |||||||||||||||||||||||||||
| 87 | 4 | exit(0); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 5 | ▶ | if ($print) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 5 | print STDOUT join( ' ', @editor_args ), "\n"; | |||||||||||||||||||||||||||
| 91 | 5 | exit(0); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 2 | ▶ | sub exit_with_error { | ||||||||||||||||||||||||||
|
Subroutine
exit_with_error: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 95 | 2 | my $error = shift; | |||||||||||||||||||||||||||
| 96 | 2 | my $details = shift; | |||||||||||||||||||||||||||
| 97 | 2 | ▶ | my $status = shift // 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
shift() // 1
MC/DC100%1/1
shift() // 1
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 98 | 2 | ▶ | unless ($json) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | print STDERR $error, "\n"; | |||||||||||||||||||||||||||
| 100 | 0 | exit($status); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 2 | require Cpanel::JSON::XS; ## no perlimports | |||||||||||||||||||||||||||
| 103 | 2 | ▶ | my $json = Cpanel::JSON::XS->new->canonical->encode( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | ( $details ? ( details => $details ) : () ), | ||||||||||||||||||||||||||||
| 106 | error => $error, | ||||||||||||||||||||||||||||
| 107 | success => Cpanel::JSON::XS::false(), | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | 2 | print STDOUT $json, "\n"; | |||||||||||||||||||||||||||
| 111 | 2 | exit($status); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | exec $ENV{EDITOR}, @editor_args; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # ABSTRACT: parse text and (hopefully) open an editor with the correct arguments | ||||||||||||||||||||||||||||
| 117 | # PODNAME: ot | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||