File Coverage

script/ot
Criterion Covered Total %
statement 45 57 78.9
branch 10 20 50.0
condition 1 3 33.3
subroutine 7 7 100.0
pod n/a
total 63 87 72.4


line stmt bran cond sub pod time code
1             #!perl
2              
3 9     9   55362 use strict;
  9         18  
  9         383  
4 9     9   43 use warnings;
  9         14  
  9         603  
5              
6 9     9   7373 use Getopt::Long qw( GetOptions );
  9         177243  
  9         68  
7 9         1102 use Open::This qw(
8             editor_args_from_parsed_text
9             maybe_get_url_from_parsed_text
10             parse_text
11 9     9   7580 );
  9         42  
12 9     9   93 use Path::Tiny qw( path );
  9         51  
  9         528  
13 9     9   6766 use Pod::Usage qw( pod2usage );
  9         833125  
  9         20850  
14              
15 9 50       4261402 pod2usage("Error: missing argument\n") unless @ARGV;
16              
17 9         114 my $browse;
18             my $editor;
19 9         0 my $json;
20 9         0 my $print;
21              
22 9         101 GetOptions(
23             'browse|b' => \$browse,
24             'editor|e=s' => \$editor,
25             'json|j' => \$json,
26             'print|p' => \$print,
27             );
28              
29 9         12230 my $parsed = parse_text(@ARGV);
30 9 100       33 if ( !$parsed ) {
31 1         7 exit_with_error( 'Could not locate file', join ' ', @ARGV );
32             }
33              
34 8 50       26 if ($browse) {
35 0         0 my $url = maybe_get_url_from_parsed_text($parsed);
36 0 0       0 if ( !exists $parsed->{remote_file_url} ) {
37 0         0 exit_with_error( 'Could not find remote repository', $parsed );
38             }
39              
40             # A relative path should be inside our repository
41 0 0       0 if ( path( $parsed->{remote_file_url} )->is_relative ) {
42 0         0 require Browser::Open;
43 0         0 Browser::Open::open_browser( $parsed->{remote_file_url} );
44 0         0 exit(0);
45             }
46 0         0 return undef;
47             }
48              
49 8   33     182 local $ENV{EDITOR} = $editor || $ENV{EDITOR};
50              
51 8 50       41 unless ( $ENV{EDITOR} ) {
52 0         0 exit_with_error(
53             'Please set your $EDITOR env var or use the --editor arg');
54             }
55              
56 8         36 my @editor_args = editor_args_from_parsed_text($parsed);
57              
58 8 100       24 if ($json) {
59 4         4548 require Cpanel::JSON::XS; ## no perlimports
60             my $json = Cpanel::JSON::XS->new->canonical->encode(
61             {
62             editor => $ENV{EDITOR},
63 4         19018 editor_args => \@editor_args,
64             success => Cpanel::JSON::XS::true(),
65             }
66             );
67 4         61 print STDOUT $json, "\n";
68 4         0 exit(0);
69             }
70 4 50       9 if ($print) {
71 4         34 print STDOUT join( ' ', @editor_args ), "\n";
72 4         0 exit(0);
73             }
74              
75             sub exit_with_error {
76 1     1   2 my $error = shift;
77 1         2 my $details = shift;
78 1 50       4 unless ($json) {
79 0         0 print STDERR $error, "\n";
80 0         0 exit(1);
81             }
82 1         1236 require Cpanel::JSON::XS; ## no perlimports
83 1 50       5020 my $json = Cpanel::JSON::XS->new->canonical->encode(
84             {
85             ( $details ? ( details => $details ) : () ),
86             error => $error,
87             success => Cpanel::JSON::XS::false(),
88             }
89             );
90 1         11 print STDOUT $json, "\n";
91 1           exit(1);
92             }
93              
94 0         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__