ConfigSystem.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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::ConfigSystem;
  9. use base qw{Fcm::CLI::Invoker};
  10. use Cwd qw{cwd};
  11. use Fcm::CLI::Exception;
  12. use Fcm::Config;
  13. use Fcm::Util::ClassLoader;
  14. ################################################################################
  15. # Returns a hash map to convert CLI options to system invoke options.
  16. sub get_cli2invoke_key_map {
  17. my ($self) = @_;
  18. return (
  19. wantarray() ? %{$self->{cli2invoke_key_map}}
  20. : $self->{cli2invoke_key_map}
  21. );
  22. }
  23. ################################################################################
  24. # Returns the Fcm::ConfigSystem class for invoking the sub-system.
  25. sub get_impl_class {
  26. my ($self) = @_;
  27. return $self->{impl_class};
  28. }
  29. ################################################################################
  30. # Invokes the sub-system
  31. sub invoke {
  32. my ($self) = @_;
  33. my $options_ref = $self->get_options();
  34. if (exists($options_ref->{verbose})) {
  35. Fcm::Config->instance()->verbose($options_ref->{verbose});
  36. }
  37. Fcm::Util::ClassLoader::load($self->get_impl_class());
  38. my $system = $self->get_impl_class()->new();
  39. my ($cfg_file) = $self->get_arguments();
  40. $system->cfg()->src($cfg_file ? $cfg_file : cwd());
  41. my %map = $self->get_cli2invoke_key_map();
  42. my %invoke_args = map {($map{$_}, $options_ref->{$_})} keys(%map);
  43. $system->invoke(%invoke_args);
  44. }
  45. 1;
  46. __END__
  47. =head1 NAME
  48. Fcm::CLI::Invoke::ConfigSystem
  49. =head1 SYNOPSIS
  50. use Fcm::CLI::Invoker::ConfigSystem;
  51. $invoker = Fcm::CLI::Invoker::ConfigSystem->new({
  52. command => $command,
  53. options => \%options,
  54. arguments => $arguments,
  55. impl_class => $class_name,
  56. cli2invoke_key_map => {
  57. option => 'OPTION',
  58. # ... more keys
  59. },
  60. });
  61. $invoker->invoke();
  62. =head1 DESCRIPTION
  63. This class extends L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> and inherits all its
  64. methods. An object of this class is used to invoke a
  65. L<Fcm::ConfigSystem|Fcm::ConfigSystem>, e.g. extract and build.
  66. =head1 METHODS
  67. See L<Fcm::CLI::Invoker|Fcm::CLI::Invoker> for a list of inherited methods.
  68. =over 4
  69. =item get_cli2invoke_key_map()
  70. Returns a hash containing a mapping table from the names of the relevant command
  71. line options to the names to be given to the invoke() method of the implementing
  72. L<Fcm::ConfigSystem|Fcm::ConfigSystem> object.
  73. =item get_impl_class()
  74. Returns the actual class that implements L<Fcm::ConfigSystem|Fcm::ConfigSystem>.
  75. An object of this implementation will be created and used by invoke().
  76. =item invoke()
  77. Invokes the L<Fcm::ConfigSystem|Fcm::ConfigSystem> sub-system. If a
  78. configuration file is not specified in the argument, it uses the current working
  79. directory.
  80. =back
  81. =head1 SEE ALSO
  82. L<Fcm::ConfigSystem|Fcm::ConfigSystem>,
  83. L<Fcm::CLI::Exception|Fcm::CLI::Exception>,
  84. L<Fcm::CLI::Invoker|Fcm::CLI::Invoker>,
  85. L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>
  86. =head1 COPYRIGHT
  87. E<169> Crown copyright Met Office. All rights reserved.
  88. =cut