Browser.pm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. package Fcm::CLI::Invoker::Browser;
  9. use base qw{Fcm::CLI::Invoker};
  10. use Carp qw{croak};
  11. use Fcm::CLI::Exception;
  12. use Fcm::Config;
  13. use Fcm::Keyword;
  14. use Fcm::Util qw{expand_tilde get_url_of_wc is_wc run_command};
  15. ################################################################################
  16. # Invokes the sub-system
  17. sub invoke {
  18. my ($self) = @_;
  19. my $config = Fcm::Config->instance();
  20. my $browser
  21. = $self->get_options()->{browser} ? $self->get_options()->{browser}
  22. : $config->setting(qw/WEB_BROWSER/)
  23. ;
  24. my ($target) = $self->get_arguments();
  25. if (!$target) {
  26. if (is_wc()) {
  27. $target = q{.};
  28. }
  29. else {
  30. croak(Fcm::CLI::Exception->new({
  31. message => 'no TARGET specified and . not a working copy',
  32. }));
  33. }
  34. }
  35. $target = expand_tilde($target);
  36. if (-e $target) {
  37. $target = get_url_of_wc($target);
  38. }
  39. my $browser_url = Fcm::Keyword::get_browser_url($target);
  40. my @command = (split(qr{\s+}xms, $browser), $browser_url);
  41. run_command(\@command, METHOD => 'exec', PRINT => 1);
  42. }
  43. 1;
  44. __END__
  45. =head1 NAME
  46. Fcm::CLI::Invoker::Browser
  47. =head1 SYNOPSIS
  48. use Fcm::CLI::Invoker::Browser;
  49. $invoker = Fcm::CLI::Invoker::Browser->new({
  50. command => $command,
  51. options => \%options,
  52. arguments => $arguments,
  53. });
  54. $invoker->invoke();
  55. =head1 DESCRIPTION
  56. This class extends L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> an inherits all its
  57. methods. An object of this class is used to invoke a web browser of a VC
  58. location.
  59. =head1 METHODS
  60. See L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> for a list of inherited methods.
  61. =over 4
  62. =item invoke()
  63. Invokes a web browser for a VC target, if it can be mapped to a browser URL. If
  64. a target is not specified in arguments, it uses the current working directory
  65. as the target.
  66. If the browser option is set, it is used as the browser command. Otherwise, the
  67. default browser is used.
  68. =back
  69. =head1 DIAGNOSTICS
  70. =over 4
  71. =item L<Fcm::CLI::Exception|Fcm::CLI::Exception>
  72. The invoke() method can croak() with this exception if no target is specified
  73. and a target cannot be deduced from the current working directory.
  74. =item L<Fcm::Keyword::Exception|Fcm::Keyword::Exception>
  75. The invoke() method can croak() with this exception if the target cannot be
  76. mapped to a browser URL.
  77. =back
  78. =head1 TO DO
  79. Unit tests.
  80. =head1 SEE ALSO
  81. L<Fcm::CLI::Exception|Fcm::CLI::Exception>,
  82. L<Fcm::CLI::Invoker|Fcm::CLI::Invoker>,
  83. L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>,
  84. L<Fcm::Keyword|Fcm::Keyword>
  85. =head1 COPYRIGHT
  86. E<169> Crown copyright Met Office. All rights reserved.
  87. =cut