Skip to content

Commit

Permalink
Add --json flag
Browse files Browse the repository at this point in the history
  • Loading branch information
oalders committed Apr 4, 2024
1 parent dc3b415 commit 2fb5d57
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 17 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Revision history for Open-This

{{$NEXT}}
- Add --json flag (GH#52) (Olaf Alders)

0.000033 2023-09-12 14:46:08Z
- Add support for IntellJ IDEA, VSCode, VSCodium and more. Also fix typo
Expand Down
47 changes: 42 additions & 5 deletions script/ot
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@ pod2usage("Error: missing argument\n") unless @ARGV;

my $browse;
my $editor;
my $json;
my $print;

GetOptions(
'browse|b' => \$browse,
'editor|e=s' => \$editor,
'json|j' => \$json,
'print|p' => \$print,
);

my $parsed = parse_text(@ARGV);
if ( !$parsed ) {
print "Could not locate file\n";
exit(1);
exit_with_error('Could not locate file');
}

if ($browse) {
my $url = maybe_get_url_from_parsed_text($parsed);
if ( !exists $parsed->{remote_file_url} ) {
print "Could not find remote repository\n";
exit(1);
exit_with_error('Could not find remote repository');
}

# A relative path should be inside our repository
Expand All @@ -50,14 +50,43 @@ local $ENV{EDITOR} = $editor || $ENV{EDITOR};

my @editor_args = editor_args_from_parsed_text($parsed);

if ($json) {
require Cpanel::JSON::XS; ## no perlimports
my $json = Cpanel::JSON::XS->new->canonical->encode(
{
editor => $ENV{EDITOR},
editor_args => \@editor_args,
success => Cpanel::JSON::XS::true(),
}
);
print STDOUT $json, "\n";
exit(0);
}
if ($print) {
print STDOUT join( ' ', @editor_args ), "\n";
exit(0);
}

sub exit_with_error {
my $error = shift;
unless ($json) {
print STDERR $error, "\n";
exit(1);
}
require Cpanel::JSON::XS; ## no perlimports
my $json = Cpanel::JSON::XS->new->canonical->encode(
{
error => $error,
success => Cpanel::JSON::XS::false(),
}
);
print STDOUT $json, "\n";
exit(1);
}

$ENV{EDITOR}
? exec $ENV{EDITOR}, @editor_args
: die('Please set your $EDITOR env var or use the --editor arg');
: exit_with_error('Please set your $EDITOR env var or use the --editor arg');

# ABSTRACT: parse text and (hopefully) open an editor with the correct arguments
# PODNAME: ot
Expand Down Expand Up @@ -138,6 +167,9 @@ disk. All security caveats apply when requiring 3rd party modules.
# Don't open anything. Just print to STDOUT. [-p|--print]
ot -p -e kate Foo::Bar
# Don't open anything. Just print JSON to STDOUT. [-j|--json]
ot -j -e kate Foo::Bar
=head1 PARAMETERS
=head2 -b|--browse
Expand All @@ -156,6 +188,11 @@ plugins.
ot --editor kate Foo::Bar
=head2 -j|--json
(Experimental). Print editor arguments to C<STDOUT> as well as an indication of success or
failure. This can be used for editor integration.
=head2 -p|--print
Print the editor arguments to C<STDOUT> rather than invoking an editor. Helpful
Expand Down
45 changes: 33 additions & 12 deletions t/ot.t
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
use strict;
use warnings;

use Test::More import => [qw( done_testing )];
use Test::More import => [qw( done_testing subtest )];
use Test::Script qw( script_runs script_stdout_is );

my @args = (
[ '--print', '--editor' ],
[ '-p', '-e' ],
[ '-p', '--editor' ],
[ '--print', '-e' ],
);
subtest 'print' => sub {
my @args = (
[ '--print', '--editor' ],
[ '-p', '-e' ],
[ '-p', '--editor' ],
[ '--print', '-e' ],
);

for my $args (@args) {
script_runs( [ './script/ot', @$args, 'kate', 'Open::This line 222' ] );
my $test_name = join ' ', @$args;
script_stdout_is( "--line 222 lib/Open/This.pm\n", $test_name );
}
for my $args (@args) {
script_runs(
[ './script/ot', @$args, 'kate', 'Open::This line 222' ] );
my $test_name = join ' ', @$args;
script_stdout_is( "--line 222 lib/Open/This.pm\n", $test_name );
}
};

subtest 'json' => sub {
my @args = (
[ '--json', '--editor' ],
[ '-j', '-e' ],
[ '-j', '--editor' ],
[ '--json', '-e' ],
);

for my $args (@args) {
script_runs(
[ './script/ot', @$args, 'kate', 'Open::This line 222' ] );
my $test_name = join ' ', @$args;
script_stdout_is(
'{"editor":"kate","editor_args":["--line","222","lib/Open/This.pm"],"success":true}'
. "\n", $test_name );
}
};

done_testing;

0 comments on commit 2fb5d57

Please sign in to comment.