| 1 | package Amazon::API::CLI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Amazon::API::BuildInfo; | ||
| 7 | use Carp; | ||
| 8 | use Amazon::API::Utils qw(slurp slurp_json); | ||
| 9 | use Amazon::API::Constants qw($TRUE $SUCCESS); | ||
| 10 | use Cwd qw(getcwd); | ||
| 11 | use Data::Dumper; | ||
| 12 | use English qw(-no_match_vars); | ||
| 13 | use File::ShareDir qw(dist_file); | ||
| 14 | use File::Path qw(make_path); | ||
| 15 | use charnames qw(:full); | ||
| 16 | |||
| 17 | use Role::Tiny::With; | ||
| 18 | with 'Amazon::API::Role::Botocore'; | ||
| 19 | with 'Amazon::API::Role::Services'; | ||
| 20 | with 'Amazon::API::Role::ModuleNames'; | ||
| 21 | |||
| 22 | our @ISA = qw(CLI::Simple); | ||
| 23 | |||
| 24 | if ( $ENV{MODULINO_WRAPPER} ) { | ||
| 25 | eval { | ||
| 26 | require CLI::Simple; | ||
| 27 | 1; | ||
| 28 | } or die "CLI::Simple is required to use the Amazon::API CLI\n"; | ||
| 29 | |||
| 30 | __PACKAGE__->use_log4perl( level => 'info' ); | ||
| 31 | } | ||
| 32 | |||
| 33 | caller or exit __PACKAGE__->main(); | ||
| 34 | |||
| 35 | ######################################################################## | ||
| 36 | sub cmd_describe_service { | ||
| 37 | ######################################################################## | ||
| 38 | my ($self) = @_; | ||
| 39 | |||
| 40 | my ($service) = $self->get_args; | ||
| 41 | |||
| 42 | croak "ERROR: usage: describe-service service-name\n" | ||
| 43 | if !$service; | ||
| 44 | |||
| 45 | my $boto_services = $self->fetch_boto_services; | ||
| 46 | |||
| 47 | my @services = $service eq 'all' ? keys %{$boto_services} : $service; | ||
| 48 | |||
| 49 | my $description = $self->get_service_descriptions( $boto_services, @services ); | ||
| 50 | |||
| 51 | print JSON->new->pretty->encode($description); | ||
| 52 | |||
| 53 | return $TRUE; | ||
| 54 | } | ||
| 55 | |||
| 56 | ######################################################################## | ||
| 57 | sub cmd_get_module_name { | ||
| 58 | ######################################################################## | ||
| 59 | my ($self) = @_; | ||
| 60 | |||
| 61 | my ($service) = $self->get_args; | ||
| 62 | |||
| 63 | croak "ERROR: usage: module-name service-name\n" | ||
| 64 | if !$service; | ||
| 65 | |||
| 66 | my $module_name = $self->module_name($service); | ||
| 67 | |||
| 68 | # just returns module name suffixe | ||
| 69 | ($module_name) = $module_name =~ /::([^:]+)$/xsm; | ||
| 70 | |||
| 71 | print {*STDOUT} $module_name; | ||
| 72 | |||
| 73 | return $SUCCESS; | ||
| 74 | } | ||
| 75 | |||
| 76 | ######################################################################## | ||
| 77 | sub cmd_create_stub { | ||
| 78 | ######################################################################## | ||
| 79 | my ($self) = @_; | ||
| 80 | |||
| 81 | my ($service) = $self->get_args; | ||
| 82 | $service //= $self->get_service; | ||
| 83 | |||
| 84 | die "ERROR: usage: create-stub service-name\n" | ||
| 85 | if !$service; | ||
| 86 | |||
| 87 | $service = lc $service; | ||
| 88 | |||
| 89 | my $module_name = $self->get_module_name // $self->module_name($service); | ||
| 90 | |||
| 91 | my $output_path = $self->get_output_path // getcwd; | ||
| 92 | |||
| 93 | my ($module_suffix) = $module_name =~ /(?:::)?([^:]+)$/xsm; | ||
| 94 | |||
| 95 | $self->create_stub( | ||
| 96 | output_path => $output_path, | ||
| 97 | service => $service, | ||
| 98 | module_name => $module_suffix, | ||
| 99 | ); | ||
| 100 | |||
| 101 | return $SUCCESS; | ||
| 102 | } | ||
| 103 | |||
| 104 | ######################################################################## | ||
| 105 | sub main { | ||
| 106 | ######################################################################## | ||
| 107 | |||
| 108 | my %commands = ( | ||
| 109 | 'create-stub' => \&cmd_create_stub, | ||
| 110 | 'describe-service' => \&cmd_describe_service, | ||
| 111 | 'create-module-names' => \&cmd_create_module_names, | ||
| 112 | 'create-services' => \&cmd_create_services, | ||
| 113 | 'get-module-name' => \&cmd_get_module_name, | ||
| 114 | 'list-services' => \&cmd_list_services, | ||
| 115 | ); | ||
| 116 | |||
| 117 | my $default_options = { | ||
| 118 | pod => $TRUE, | ||
| 119 | pager => $TRUE, | ||
| 120 | output_path => getcwd, | ||
| 121 | metadata => $TRUE, | ||
| 122 | format => 'json', | ||
| 123 | }; | ||
| 124 | |||
| 125 | my @option_specs = qw( | ||
| 126 | botocore-path|b=s | ||
| 127 | file|f=s | ||
| 128 | filter=s | ||
| 129 | format=s | ||
| 130 | help|h | ||
| 131 | limit|l=i | ||
| 132 | module-name|m=s | ||
| 133 | output-path|o=s | ||
| 134 | metadata! | ||
| 135 | pager|P! | ||
| 136 | service|s=s | ||
| 137 | ); | ||
| 138 | |||
| 139 | my $cli = __PACKAGE__->new( | ||
| 140 | commands => \%commands, | ||
| 141 | option_specs => \@option_specs, | ||
| 142 | default_options => $default_options, | ||
| 143 | extra_options => [qw()], | ||
| 144 | abbreviations => $TRUE, | ||
| 145 | ); | ||
| 146 | |||
| 147 | return $cli->run(); | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ |