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