| 1 | # ABSTRACT: ponapi demo running command line utility | ||
| 2 | package PONAPI::CLI::Command::demo; | ||
| 3 | |||
| 4 | use PONAPI::CLI -command; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | sub abstract { "Run a DEMO PONAPI server" } | ||
| 10 | sub description { "This tool will run a demo server with a mock DB" } | ||
| 11 | |||
| 12 | sub opt_spec { | ||
| 13 | return ( | ||
| 14 | [ "s|server", "Run a local PONAPI demo server" ], | ||
| 15 | [ "q|query:s", "Send a random/provided query to local server" ], | ||
| 16 | [ "p|port=i", "Specify a port for the server (default=5000)" ], | ||
| 17 | [ "j|json", "JSON-only output" ], | ||
| 18 | ); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub validate_args { | ||
| 22 | my ( $self, $opt, $args ) = @_; | ||
| 23 | |||
| 24 | $self->usage_error("(only) one of server (-s) or query (-q [STR]) is required.\n") | ||
| 25 | unless exists $opt->{s} xor exists $opt->{q}; | ||
| 26 | |||
| 27 | $self->{port} = $opt->{port} || $opt->{p} || 5000; | ||
| 28 | |||
| 29 | $self->{query_string} = ""; | ||
| 30 | |||
| 31 | if ( exists $opt->{q} and $opt->{q} ) { | ||
| 32 | $opt->{q} =~ s|^/||; | ||
| 33 | $self->{query_string} = | ||
| 34 | ( $opt->{q} !~ /^http/ ? 'http://localhost:' . $self->{port} . '/' : '' ) | ||
| 35 | . $opt->{q}; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | $self->{only_json} = !!( $opt->{json} || $opt->{j} ); | ||
| 40 | $self->usage_error("JSON-only works just in Query mode") | ||
| 41 | if $self->{only_json} and !exists $opt->{q}; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub execute { | ||
| 45 | my ( $self, $opt, $args ) = @_; | ||
| 46 | |||
| 47 | $self->run_server() if exists $opt->{s}; | ||
| 48 | $self->run_query() if exists $opt->{q}; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub run_server { | ||
| 52 | my $self = shift; | ||
| 53 | require PONAPI::CLI::RunServer; | ||
| 54 | PONAPI::CLI::RunServer::run( $self->{port} ); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub run_query { | ||
| 58 | my $self = shift; | ||
| 59 | require PONAPI::CLI::RunQuery; | ||
| 60 | PONAPI::CLI::RunQuery::run( $self->{port}, $self->{query_string}, $self->{only_json} ); | ||
| 61 | } | ||
| 62 | |||
| 63 | 1; | ||
| 64 | |||
| 65 | __END__ | ||
| 66 | |||
| 67 | =pod | ||
| 68 | |||
| 69 | =encoding UTF-8 | ||
| 70 | |||
| 71 | =head1 NAME | ||
| 72 | |||
| 73 | PONAPI::CLI::Command::demo - ponapi demo running command line utility | ||
| 74 | |||
| 75 | =head1 VERSION | ||
| 76 | |||
| 77 | version 0.003002 | ||
| 78 | |||
| 79 | =head1 AUTHORS | ||
| 80 | |||
| 81 | =over 4 | ||
| 82 | |||
| 83 | =item * | ||
| 84 | |||
| 85 | Mickey Nasriachi <mickey@cpan.org> | ||
| 86 | |||
| 87 | =item * | ||
| 88 | |||
| 89 | Stevan Little <stevan@cpan.org> | ||
| 90 | |||
| 91 | =item * | ||
| 92 | |||
| 93 | Brian Fraser <hugmeir@cpan.org> | ||
| 94 | |||
| 95 | =back | ||
| 96 | |||
| 97 | =head1 COPYRIGHT AND LICENSE | ||
| 98 | |||
| 99 | This software is copyright (c) 2017 by Mickey Nasriachi, Stevan Little, Brian Fraser. | ||
| 100 | |||
| 101 | This is free software; you can redistribute it and/or modify it under | ||
| 102 | the same terms as the Perl 5 programming language system itself. | ||
| 103 | |||
| 104 | =cut |