| 1 | #!perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 9 | ▶ | use Getopt::Long qw( GetOptions ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 9 | ▶ | 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 | 9 | ); | |||||||||||||||||||
| 12 | 9 | ▶ | use Path::Tiny qw( path ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 9 | ▶ | use Pod::Usage qw( pod2usage ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 9 | ▶ | pod2usage("Error: missing argument\n") unless @ARGV; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 9 | my $browse; | |||||||||||||||||||
| 18 | my $editor; | ||||||||||||||||||||
| 19 | 9 | my $json; | |||||||||||||||||||
| 20 | 9 | my $print; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 9 | GetOptions( | |||||||||||||||||||
| 23 | 'browse|b' => \$browse, | ||||||||||||||||||||
| 24 | 'editor|e=s' => \$editor, | ||||||||||||||||||||
| 25 | 'json|j' => \$json, | ||||||||||||||||||||
| 26 | 'print|p' => \$print, | ||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 9 | my $parsed = parse_text(@ARGV); | |||||||||||||||||||
| 30 | 9 | ▶ | if ( !$parsed ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 31 | 1 | exit_with_error( 'Could not locate file', join ' ', @ARGV ); | |||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 8 | ▶ | if ($browse) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 0 | my $url = maybe_get_url_from_parsed_text($parsed); | |||||||||||||||||||
| 36 | 0 | ▶ | if ( !exists $parsed->{remote_file_url} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | exit_with_error( 'Could not find remote repository', $parsed ); | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # A relative path should be inside our repository | ||||||||||||||||||||
| 41 | 0 | ▶ | if ( path( $parsed->{remote_file_url} )->is_relative ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 0 | require Browser::Open; | |||||||||||||||||||
| 43 | 0 | Browser::Open::open_browser( $parsed->{remote_file_url} ); | |||||||||||||||||||
| 44 | 0 | exit(0); | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | 0 | return undef; | |||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 8 | ▶ | 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'}
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 8 | ▶ | unless ( $ENV{EDITOR} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | exit_with_error( | |||||||||||||||||||
| 53 | 'Please set your $EDITOR env var or use the --editor arg'); | ||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 8 | my @editor_args = editor_args_from_parsed_text($parsed); | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 8 | ▶ | if ($json) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 4 | require Cpanel::JSON::XS; ## no perlimports | |||||||||||||||||||
| 60 | my $json = Cpanel::JSON::XS->new->canonical->encode( | ||||||||||||||||||||
| 61 | { | ||||||||||||||||||||
| 62 | editor => $ENV{EDITOR}, | ||||||||||||||||||||
| 63 | 4 | editor_args => \@editor_args, | |||||||||||||||||||
| 64 | success => Cpanel::JSON::XS::true(), | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | 4 | print STDOUT $json, "\n"; | |||||||||||||||||||
| 68 | 4 | exit(0); | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 4 | ▶ | if ($print) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 4 | print STDOUT join( ' ', @editor_args ), "\n"; | |||||||||||||||||||
| 72 | 4 | exit(0); | |||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | ▶ | sub exit_with_error { | ||||||||||||||||||
|
Subroutine
exit_with_error: called CC=3 SCAR=11.8
| |||||||||||||||||||||
| 76 | 1 | my $error = shift; | |||||||||||||||||||
| 77 | 1 | my $details = shift; | |||||||||||||||||||
| 78 | 1 | ▶ | unless ($json) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 0 | print STDERR $error, "\n"; | |||||||||||||||||||
| 80 | 0 | exit(1); | |||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | 1 | require Cpanel::JSON::XS; ## no perlimports | |||||||||||||||||||
| 83 | 1 | ▶ | my $json = Cpanel::JSON::XS->new->canonical->encode( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | { | ||||||||||||||||||||
| 85 | ( $details ? ( details => $details ) : () ), | ||||||||||||||||||||
| 86 | error => $error, | ||||||||||||||||||||
| 87 | success => Cpanel::JSON::XS::false(), | ||||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||
| 90 | 1 | print STDOUT $json, "\n"; | |||||||||||||||||||
| 91 | 1 | exit(1); | |||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | exec $ENV{EDITOR}, @editor_args; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # ABSTRACT: parse text and (hopefully) open an editor with the correct arguments | ||||||||||||||||||||
| 97 | # PODNAME: ot | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||