KeywordPrinter.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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::KeywordPrinter;
  9. use base qw{Fcm::CLI::Invoker};
  10. use Carp qw{croak};
  11. use Fcm::CLI::Exception;
  12. use Fcm::Keyword;
  13. use Fcm::Keyword::Formatter::Entries;
  14. use Fcm::Keyword::Formatter::Entry::Location;
  15. use Fcm::Keyword::Exception;
  16. use Fcm::Util qw{get_url_of_wc};
  17. ################################################################################
  18. # Invokes the sub-system
  19. sub invoke {
  20. my ($self) = @_;
  21. my @targets = $self->get_arguments();
  22. if (@targets) {
  23. for my $target (@targets) {
  24. my $entry_list = Fcm::Keyword::get_location_entries_for($target);
  25. my $loc = $target;
  26. if (-e $target) {
  27. $loc = get_url_of_wc($target);
  28. if (!$loc) {
  29. croak(Fcm::Keyword::Exception->new({message => sprintf(
  30. "%s: unrecognised version control resource", $target,
  31. )}));
  32. }
  33. }
  34. my @entry_list = Fcm::Keyword::get_location_entries_for($loc);
  35. if (!@entry_list) {
  36. croak(Fcm::Keyword::Exception->new({message => sprintf(
  37. "%s: no FCM location keyword found for this target", $target,
  38. )}));
  39. }
  40. my $formatter = Fcm::Keyword::Formatter::Entry::Location->new();
  41. for my $entry (
  42. sort {$a->get_key() cmp $b->get_key()}
  43. grep {!$_->is_implied()}
  44. @entry_list
  45. ) {
  46. print($formatter->format($entry), "\n");
  47. }
  48. }
  49. }
  50. else {
  51. my $formatter = Fcm::Keyword::Formatter::Entries->new();
  52. print($formatter->format(Fcm::Keyword::get_entries()));
  53. }
  54. }
  55. 1;
  56. __END__
  57. =head1 NAME
  58. Fcm::CLI::Invoker::KeywordPrinter
  59. =head1 SYNOPSIS
  60. use Fcm::CLI::Invoker::KeywordPrinter;
  61. $invoker = Fcm::CLI::Invoker::KeywordPrinter->new({
  62. command => $command,
  63. options => \%options,
  64. arguments => $arguments,
  65. });
  66. $invoker->invoke();
  67. =head1 DESCRIPTION
  68. This class extends L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> an inherits all its
  69. methods. An object of this class is used to invoke the location keyword printer.
  70. =head1 METHODS
  71. See L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> for a list of inherited methods.
  72. =over 4
  73. =item invoke()
  74. Invokes the location keyword printer. If a namespace is specified in the
  75. argument, prints revision keywords and browser mapping templates for the
  76. specified namespace. If a namespace is not specified, prints all registered
  77. location keywords.
  78. =back
  79. =head1 DIAGNOSTICS
  80. =over 4
  81. =item L<Fcm::Keyword::Exception|Fcm::Keyword::Exception>
  82. The invoke() method can croak() with this exception if there is no matching
  83. namespace matching that of the specified.
  84. =back
  85. =head1 TO DO
  86. Unit tests.
  87. =head1 SEE ALSO
  88. L<Fcm::CLI::Exception|Fcm::CLI::Exception>,
  89. L<Fcm::CLI::Invoker|Fcm::CLI::Invoker>,
  90. L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>,
  91. L<Fcm::Keyword|Fcm::Keyword>,
  92. L<Fcm::Keyword::Formatter::Entries|Fcm::Keyword::Formatter::Entries>,
  93. L<Fcm::Keyword::Formatter::Entry::Location|Fcm::Keyword::Formatter::Entry::Location>
  94. =head1 COPYRIGHT
  95. E<169> Crown copyright Met Office. All rights reserved.
  96. =cut