| 1 | package Amazon::API::Help; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Amazon::API::BuildInfo; | ||
| 7 | use CLI::Simple::Constants qw(:booleans :chars); | ||
| 8 | use CLI::Simple::Utils qw(slurp slurp_json choose); | ||
| 9 | use Carp; | ||
| 10 | use Data::Dumper; | ||
| 11 | use English qw(-no_match_vars); | ||
| 12 | use File::ShareDir qw(dist_file dist_dir); | ||
| 13 | use IO::Uncompress::Gunzip qw(gunzip); | ||
| 14 | use List::Util qw(max none); | ||
| 15 | use Pod::HTML2Pod; | ||
| 16 | use Pod::Text; | ||
| 17 | use Storable qw(retrieve); | ||
| 18 | use Template; | ||
| 19 | |||
| 20 | use parent qw(CLI::Simple); | ||
| 21 | |||
| 22 | our $VERSION = '1.0.3'; | ||
| 23 | |||
| 24 | caller or exit __PACKAGE__->main(); | ||
| 25 | |||
| 26 | ######################################################################## | ||
| 27 | sub init { | ||
| 28 | ######################################################################## | ||
| 29 | my ($self) = @_; | ||
| 30 | |||
| 31 | my $command = $self->command; | ||
| 32 | |||
| 33 | if ( none { $command eq $_ } keys %{ $self->commands } ) { | ||
| 34 | $self->set_args( [ $command, $self->get_args ] ); | ||
| 35 | $self->command('help'); | ||
| 36 | } | ||
| 37 | |||
| 38 | if ( $self->command eq 'help' ) { | ||
| 39 | |||
| 40 | my @candidates = ( 'botocore-services.api.gz', dist_file( 'Amazon-API-Help', 'botocore-services.api.gz' ) ); | ||
| 41 | |||
| 42 | my ($services_file) = grep { defined $_ && -e $_ } @candidates; | ||
| 43 | |||
| 44 | my $services; | ||
| 45 | gunzip( $services_file, \$services ); | ||
| 46 | |||
| 47 | my $service_meta = retrieve( \$services ); | ||
| 48 | |||
| 49 | $self->set_services($service_meta); | ||
| 50 | } | ||
| 51 | |||
| 52 | my $version = Amazon::API::BuildInfo->version(); | ||
| 53 | |||
| 54 | my $botocore_version = $version->{botocore_version}; | ||
| 55 | |||
| 56 | $self->set_version($version); | ||
| 57 | |||
| 58 | $self->set_provenance( | ||
| 59 | { version => $botocore_version->{version} // 'unknown', | ||
| 60 | commit => $botocore_version->{commit} // 'unknown' | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | |||
| 64 | return; | ||
| 65 | } | ||
| 66 | |||
| 67 | ######################################################################## | ||
| 68 | sub cmd_help { | ||
| 69 | ######################################################################## | ||
| 70 | my ( $self, $init_flag ) = @_; | ||
| 71 | |||
| 72 | if ( $self->get_help ) { | ||
| 73 | $self->usage(); | ||
| 74 | } | ||
| 75 | |||
| 76 | return | ||
| 77 | if $init_flag; | ||
| 78 | |||
| 79 | my ( $service_name, $method_or_shape ) = $self->get_args; | ||
| 80 | |||
| 81 | if ($service_name) { | ||
| 82 | my $service_meta = $self->get_services->{$service_name} | ||
| 83 | or die "ERROR: no such service '$service_name'\n"; | ||
| 84 | |||
| 85 | if ($method_or_shape) { | ||
| 86 | my $meta = $service_meta->{metadata}; | ||
| 87 | |||
| 88 | if ( $self->get_shape ) { | ||
| 89 | return $self->_shape_help( $service_name, $service_meta, $method_or_shape ) | ||
| 90 | if $meta->{shapes}{$method_or_shape}; | ||
| 91 | |||
| 92 | if ( $meta->{operations}{$method_or_shape} ) { | ||
| 93 | die "ERROR: no shape '$method_or_shape'. If you meant the operation '$method_or_shape' drop --shape\n"; | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | die "ERROR: no shape '$method_or_shape' in service '$service_name'\n"; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return $self->_operation_help( $service_name, $service_meta, $method_or_shape ) | ||
| 101 | if $meta->{operations}{$method_or_shape}; | ||
| 102 | |||
| 103 | return $self->_shape_help( $service_name, $service_meta, $method_or_shape ) | ||
| 104 | if $meta->{shapes}{$method_or_shape}; | ||
| 105 | |||
| 106 | die "ERROR: '$method_or_shape' is not an operation or shape in service '$service_name'\n"; | ||
| 107 | } | ||
| 108 | |||
| 109 | return $self->_service_help( $service_meta, $service_name ); | ||
| 110 | } | ||
| 111 | |||
| 112 | my $all = $self->get_services; | ||
| 113 | |||
| 114 | $self->_show_pod( | ||
| 115 | template => 'all-services.tt', | ||
| 116 | stash => { | ||
| 117 | services => [ sort keys %{$all} ], | ||
| 118 | modules => [ sort map { $all->{$_}{metadata}{metadata}{perl_module_name} // '(unresolved)' } keys %{$all} ], | ||
| 119 | }, | ||
| 120 | ); | ||
| 121 | |||
| 122 | return $SUCCESS; | ||
| 123 | } | ||
| 124 | |||
| 125 | ######################################################################## | ||
| 126 | sub _operation_help { | ||
| 127 | ######################################################################## | ||
| 128 | my ( $self, $service, $service_meta, $operation_name ) = @_; | ||
| 129 | |||
| 130 | my $meta = $service_meta->{metadata}; | ||
| 131 | my $operation = $meta->{operations}{$operation_name} | ||
| 132 | or die "ERROR: no such operation '$operation_name' in service '$service'\n"; | ||
| 133 | |||
| 134 | my $shapes = $meta->{shapes}; | ||
| 135 | |||
| 136 | my $input_shape = $operation->{input} ? $operation->{input}{shape} : undef; | ||
| 137 | my $output_shape = $operation->{output} ? $operation->{output}{shape} : undef; | ||
| 138 | |||
| 139 | my $input_members = $input_shape ? ( $shapes->{$input_shape}{members} // {} ) : {}; | ||
| 140 | my $output_members = $output_shape ? ( $shapes->{$output_shape}{members} // {} ) : {}; | ||
| 141 | |||
| 142 | my @errors; | ||
| 143 | |||
| 144 | foreach my $e ( @{ $operation->{errors} // [] } ) { | ||
| 145 | my $name = $e->{shape}; | ||
| 146 | my $eshape = $shapes->{$name} // {}; | ||
| 147 | my $detail = $eshape->{error}; | ||
| 148 | |||
| 149 | push @errors, | ||
| 150 | { | ||
| 151 | name => $name, | ||
| 152 | documentation => $eshape->{documentation} // $EMPTY, | ||
| 153 | detail => $detail | ||
| 154 | ? [ map { { key => $_, value => $detail->{$_} } } sort keys %{$detail} ] | ||
| 155 | : [], | ||
| 156 | }; | ||
| 157 | } | ||
| 158 | |||
| 159 | my %seen; | ||
| 160 | my @see_also = sort grep { !$seen{$_}++ } | ||
| 161 | grep {defined} ( $input_shape, $output_shape, map { $_->{shape} } @{ $operation->{errors} // [] } ); | ||
| 162 | |||
| 163 | my $http = $operation->{http} // {}; | ||
| 164 | $_->{documentation} //= $EMPTY for values %{$input_members}, values %{$output_members}; | ||
| 165 | |||
| 166 | $self->_show_pod( | ||
| 167 | template => 'operation.tt', | ||
| 168 | stash => { | ||
| 169 | service => $service, | ||
| 170 | method => $operation_name, | ||
| 171 | documentation => $operation->{documentation} // $EMPTY, | ||
| 172 | input_members => $input_members, | ||
| 173 | output_members => $output_members, | ||
| 174 | errors => \@errors, | ||
| 175 | http_method => $http->{method} // $EMPTY, | ||
| 176 | request_uri => $http->{requestUri} // $EMPTY, | ||
| 177 | see_also => \@see_also, | ||
| 178 | provenance => $self->get_provenance, | ||
| 179 | package_name => $meta->{metadata}{perl_module_name} // '(unresolved)', | ||
| 180 | }, | ||
| 181 | ); | ||
| 182 | |||
| 183 | return $SUCCESS; | ||
| 184 | } | ||
| 185 | |||
| 186 | ######################################################################## | ||
| 187 | sub _shape_help { | ||
| 188 | ######################################################################## | ||
| 189 | my ( $self, $service, $service_meta, $shape_name ) = @_; | ||
| 190 | |||
| 191 | my $shape = $service_meta->{metadata}{shapes}{$shape_name} | ||
| 192 | or die "ERROR: no such shape '$shape_name' in service '$service'\n"; | ||
| 193 | |||
| 194 | my %seen; | ||
| 195 | my @see_also | ||
| 196 | = sort grep { !$seen{$_}++ } ( $shape->{members} ? map { $shape->{members}{$_}{shape} } keys %{ $shape->{members} } : () ), | ||
| 197 | ( $shape->{member} ? $shape->{member}{shape} : () ), | ||
| 198 | ( $shape->{key} ? $shape->{key}{shape} : () ), | ||
| 199 | ( $shape->{value} ? $shape->{value}{shape} : () ); | ||
| 200 | |||
| 201 | my @limits = map { { key => $_, value => $shape->{$_} } } | ||
| 202 | grep { defined $shape->{$_} } qw(min max pattern); | ||
| 203 | |||
| 204 | my $is_structure = ( $shape->{type} // $EMPTY ) eq 'structure'; | ||
| 205 | my $show_synopsis = $is_structure && !( $shape->{members} && $shape->{members}{message} ); | ||
| 206 | |||
| 207 | $shape->{documentation} //= $EMPTY; | ||
| 208 | $shape->{members} //= {} if $is_structure; | ||
| 209 | $_->{documentation} //= $EMPTY for values %{ $shape->{members} // {} }; | ||
| 210 | |||
| 211 | $self->_show_pod( | ||
| 212 | template => 'shape.tt', | ||
| 213 | stash => { | ||
| 214 | class => sprintf( 'Amazon::API::Botocore::Shape::%s::%s', $service, $shape_name ), | ||
| 215 | lc_name => snake_case($shape_name), | ||
| 216 | service => $service, | ||
| 217 | shape => $shape, | ||
| 218 | limits => \@limits, | ||
| 219 | see_also => \@see_also, | ||
| 220 | provenance => $self->get_provenance, | ||
| 221 | show_synopsis => $show_synopsis, | ||
| 222 | enum => [ @{ $shape->{enum} // [] } ], | ||
| 223 | }, | ||
| 224 | ); | ||
| 225 | |||
| 226 | return $SUCCESS; | ||
| 227 | } | ||
| 228 | |||
| 229 | ######################################################################## | ||
| 230 | sub _service_help { | ||
| 231 | ######################################################################## | ||
| 232 | my ( $self, $service_meta, $service ) = @_; | ||
| 233 | |||
| 234 | $self->_show_pod( | ||
| 235 | template => 'service.tt', | ||
| 236 | stash => { | ||
| 237 | service => $service, | ||
| 238 | documentation => $service_meta->{metadata}{documentation} // $EMPTY, | ||
| 239 | operations => [ sort keys %{ $service_meta->{metadata}{operations} } ], | ||
| 240 | botocore_version => $self->get_provenance->{version}, | ||
| 241 | }, | ||
| 242 | ); | ||
| 243 | |||
| 244 | return $SUCCESS; | ||
| 245 | } | ||
| 246 | |||
| 247 | ######################################################################## | ||
| 248 | sub snake_case { | ||
| 249 | ######################################################################## | ||
| 250 | my ($name) = @_; | ||
| 251 | |||
| 252 | while ( $name =~ s/([[:upper:]])([[:lower:]])/lc("_$1").$2/xsme ) { } | ||
| 253 | |||
| 254 | $name =~ s/^_//xsm; | ||
| 255 | $name =~ s/([[:lower:]])([[:upper:]])/$1_$2/gxsm; | ||
| 256 | |||
| 257 | return $name; | ||
| 258 | } | ||
| 259 | |||
| 260 | ######################################################################## | ||
| 261 | sub _html2pod { | ||
| 262 | ######################################################################## | ||
| 263 | my ($html) = @_; | ||
| 264 | |||
| 265 | my $pod = Pod::HTML2Pod::convert( | ||
| 266 | a_href => $TRUE, | ||
| 267 | a_name => $TRUE, | ||
| 268 | content => $html // $EMPTY, | ||
| 269 | ); | ||
| 270 | |||
| 271 | $pod =~ s/^=pod//xsm; | ||
| 272 | $pod =~ s/^=cut//xsm; | ||
| 273 | $pod =~ s/^[#].*$//gxsm; | ||
| 274 | |||
| 275 | $pod =~ s/\A\s+//xsm; # template owns leading/trailing blank lines | ||
| 276 | $pod =~ s/\s+\z//xsm; | ||
| 277 | |||
| 278 | return $pod; | ||
| 279 | } | ||
| 280 | |||
| 281 | ######################################################################## | ||
| 282 | sub _show_pod { | ||
| 283 | ######################################################################## | ||
| 284 | my ( $self, %args ) = @_; | ||
| 285 | |||
| 286 | my $tt = $self->_template_engine; | ||
| 287 | |||
| 288 | my $pod = $EMPTY; | ||
| 289 | |||
| 290 | $tt->process( $args{template}, $args{stash}, \$pod ) | ||
| 291 | or die sprintf "ERROR: rendering %s: %s\n", $args{template}, $tt->error; | ||
| 292 | |||
| 293 | my $paged; | ||
| 294 | |||
| 295 | if ( $self->get_cli_pager ) { | ||
| 296 | $paged = eval { | ||
| 297 | require IO::Pager; | ||
| 298 | IO::Pager::open( *STDOUT, '|-:utf8', 'Unbuffered' ); | ||
| 299 | return $TRUE; | ||
| 300 | }; | ||
| 301 | } | ||
| 302 | |||
| 303 | binmode STDOUT, ':encoding(UTF-8)' | ||
| 304 | if !$paged; | ||
| 305 | |||
| 306 | Pod::Text->new->parse_string_document($pod); | ||
| 307 | |||
| 308 | return; | ||
| 309 | } | ||
| 310 | |||
| 311 | ######################################################################## | ||
| 312 | sub _template_engine { | ||
| 313 | ######################################################################## | ||
| 314 | my ($self) = @_; | ||
| 315 | |||
| 316 | my $tt = $self->get_tt; | ||
| 317 | |||
| 318 | return $tt | ||
| 319 | if $tt; | ||
| 320 | |||
| 321 | $tt = Template->new( | ||
| 322 | { INCLUDE_PATH => [ dist_dir('Amazon-API-Help') ], | ||
| 323 | STRICT => $TRUE, | ||
| 324 | ENCODING => 'utf8', | ||
| 325 | FILTERS => { html2pod => \&_html2pod }, | ||
| 326 | } | ||
| 327 | ) or die sprintf "ERROR: could not create template engine: %s\n", Template->error; | ||
| 328 | |||
| 329 | $self->set_tt($tt); | ||
| 330 | |||
| 331 | return $tt; | ||
| 332 | } | ||
| 333 | |||
| 334 | ######################################################################## | ||
| 335 | sub cmd_dump_service { | ||
| 336 | ######################################################################## | ||
| 337 | my ($self) = @_; | ||
| 338 | |||
| 339 | my ($service) = $self->get_args; | ||
| 340 | |||
| 341 | die "ERROR: usage dump-service service-name\n" | ||
| 342 | if !$service; | ||
| 343 | |||
| 344 | my $service_meta = $self->get_services->{$service}; | ||
| 345 | |||
| 346 | die "ERROR: no such service '$service'\n" | ||
| 347 | if !$service_meta; | ||
| 348 | |||
| 349 | print {*STDOUT} JSON->new->pretty->encode($service_meta); | ||
| 350 | |||
| 351 | return $SUCCESS; | ||
| 352 | } | ||
| 353 | |||
| 354 | ######################################################################## | ||
| 355 | sub cmd_version { | ||
| 356 | ######################################################################## | ||
| 357 | my ($self) = @_; | ||
| 358 | |||
| 359 | my $version = Amazon::API::BuildInfo->version(); | ||
| 360 | |||
| 361 | require Text::ASCIITable; | ||
| 362 | |||
| 363 | my $t = Text::ASCIITable->new( { headingText => 'Amazon::API Version Info' } ); | ||
| 364 | |||
| 365 | $t->setCols( q{}, qw(Version Commit) ); | ||
| 366 | $t->addRow( 'Amazon::API', $version->{version}, $version->{commit} ); | ||
| 367 | $t->addRow( 'Botocore', $version->{botocore_version}->{version}, $version->{botocore_version}->{commit} ); | ||
| 368 | |||
| 369 | print {*STDOUT} $t; | ||
| 370 | |||
| 371 | return $SUCCESS; | ||
| 372 | } | ||
| 373 | |||
| 374 | ######################################################################## | ||
| 375 | sub main { | ||
| 376 | ######################################################################## | ||
| 377 | my %commands = ( | ||
| 378 | 'dump-service' => \&cmd_dump_service, | ||
| 379 | 'help' => \&cmd_help, | ||
| 380 | 'version' => \&cmd_version, | ||
| 381 | 'default' => 'help', | ||
| 382 | ); | ||
| 383 | |||
| 384 | my @option_specs = qw( | ||
| 385 | help|h | ||
| 386 | cli-pager! | ||
| 387 | shape | ||
| 388 | ); | ||
| 389 | |||
| 390 | my $cli = __PACKAGE__->new( | ||
| 391 | commands => \%commands, | ||
| 392 | option_specs => \@option_specs, | ||
| 393 | extra_options => [qw(services tt provenance version)], | ||
| 394 | default_options => { cli_pager => 1 }, | ||
| 395 | validate_command => $FALSE, | ||
| 396 | ); | ||
| 397 | |||
| 398 | return $cli->run(); | ||
| 399 | } | ||
| 400 | |||
| 401 | 1; | ||
| 402 | |||
| 403 | __END__ |