[1980] | 1 | # ------------------------------------------------------------------------------ |
---|
| 2 | # (C) Crown copyright Met Office. All rights reserved. |
---|
| 3 | # For further details please refer to the file COPYRIGHT.txt |
---|
| 4 | # which you should have received as part of this distribution. |
---|
| 5 | # ------------------------------------------------------------------------------ |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | |
---|
| 9 | package Fcm::CLI::Invoker::Browser; |
---|
| 10 | use base qw{Fcm::CLI::Invoker}; |
---|
| 11 | |
---|
| 12 | use Carp qw{croak}; |
---|
| 13 | use Fcm::CLI::Exception; |
---|
| 14 | use Fcm::Config; |
---|
| 15 | use Fcm::Keyword; |
---|
| 16 | use Fcm::Util qw{expand_tilde get_url_of_wc is_wc run_command}; |
---|
| 17 | |
---|
| 18 | ################################################################################ |
---|
| 19 | # Invokes the sub-system |
---|
| 20 | sub invoke { |
---|
| 21 | my ($self) = @_; |
---|
| 22 | my $config = Fcm::Config->instance(); |
---|
| 23 | my $browser |
---|
| 24 | = $self->get_options()->{browser} ? $self->get_options()->{browser} |
---|
| 25 | : $config->setting(qw/WEB_BROWSER/) |
---|
| 26 | ; |
---|
| 27 | my ($target) = $self->get_arguments(); |
---|
| 28 | if (!$target) { |
---|
| 29 | if (is_wc()) { |
---|
| 30 | $target = q{.}; |
---|
| 31 | } |
---|
| 32 | else { |
---|
| 33 | croak(Fcm::CLI::Exception->new({ |
---|
| 34 | message => 'no TARGET specified and . not a working copy', |
---|
| 35 | })); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | $target = expand_tilde($target); |
---|
| 39 | if (-e $target) { |
---|
| 40 | $target = get_url_of_wc($target); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | my $browser_url = Fcm::Keyword::get_browser_url($target); |
---|
| 44 | my @command = (split(qr{\s+}xms, $browser), $browser_url); |
---|
| 45 | run_command(\@command, METHOD => 'exec', PRINT => 1); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | 1; |
---|
| 49 | __END__ |
---|
| 50 | |
---|
| 51 | =head1 NAME |
---|
| 52 | |
---|
| 53 | Fcm::CLI::Invoker::Browser |
---|
| 54 | |
---|
| 55 | =head1 SYNOPSIS |
---|
| 56 | |
---|
| 57 | use Fcm::CLI::Invoker::Browser; |
---|
| 58 | $invoker = Fcm::CLI::Invoker::Browser->new({ |
---|
| 59 | command => $command, |
---|
| 60 | options => \%options, |
---|
| 61 | arguments => $arguments, |
---|
| 62 | }); |
---|
| 63 | $invoker->invoke(); |
---|
| 64 | |
---|
| 65 | =head1 DESCRIPTION |
---|
| 66 | |
---|
| 67 | This class extends L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> an inherits all its |
---|
| 68 | methods. An object of this class is used to invoke a web browser of a VC |
---|
| 69 | location. |
---|
| 70 | |
---|
| 71 | =head1 METHODS |
---|
| 72 | |
---|
| 73 | See L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> for a list of inherited methods. |
---|
| 74 | |
---|
| 75 | =over 4 |
---|
| 76 | |
---|
| 77 | =item invoke() |
---|
| 78 | |
---|
| 79 | Invokes a web browser for a VC target, if it can be mapped to a browser URL. If |
---|
| 80 | a target is not specified in arguments, it uses the current working directory |
---|
| 81 | as the target. |
---|
| 82 | |
---|
| 83 | If the browser option is set, it is used as the browser command. Otherwise, the |
---|
| 84 | default browser is used. |
---|
| 85 | |
---|
| 86 | =back |
---|
| 87 | |
---|
| 88 | =head1 DIAGNOSTICS |
---|
| 89 | |
---|
| 90 | =over 4 |
---|
| 91 | |
---|
| 92 | =item L<Fcm::CLI::Exception|Fcm::CLI::Exception> |
---|
| 93 | |
---|
| 94 | The invoke() method can croak() with this exception if no target is specified |
---|
| 95 | and a target cannot be deduced from the current working directory. |
---|
| 96 | |
---|
| 97 | =item L<Fcm::Keyword::Exception|Fcm::Keyword::Exception> |
---|
| 98 | |
---|
| 99 | The invoke() method can croak() with this exception if the target cannot be |
---|
| 100 | mapped to a browser URL. |
---|
| 101 | |
---|
| 102 | =back |
---|
| 103 | |
---|
| 104 | =head1 TO DO |
---|
| 105 | |
---|
| 106 | Unit tests. |
---|
| 107 | |
---|
| 108 | =head1 SEE ALSO |
---|
| 109 | |
---|
| 110 | L<Fcm::CLI::Exception|Fcm::CLI::Exception>, |
---|
| 111 | L<Fcm::CLI::Invoker|Fcm::CLI::Invoker>, |
---|
| 112 | L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>, |
---|
| 113 | L<Fcm::Keyword|Fcm::Keyword> |
---|
| 114 | |
---|
| 115 | =head1 COPYRIGHT |
---|
| 116 | |
---|
| 117 | E<169> Crown copyright Met Office. All rights reserved. |
---|
| 118 | |
---|
| 119 | =cut |
---|