File Coverage

blib/lib/WWW/GoDaddy/REST/Shell.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package WWW::GoDaddy::REST::Shell;
2 1     1   992 use base qw(Term::Shell);
  1         1  
  1         667  
3              
4 1     1   18497 use warnings;
  1         1  
  1         30  
5 1     1   3 use strict;
  1         1  
  1         34  
6              
7 1     1   789 use Getopt::Long;
  1         10132  
  1         4  
8 1     1   731 use Config::Any;
  1         8742  
  1         36  
9 1     1   9 use List::Util qw(shuffle first);
  1         1  
  1         132  
10 1     1   811 use Pod::Usage;
  1         48890  
  1         173  
11              
12 1     1   58 use WWW::GoDaddy::REST;
  0            
  0            
13             use WWW::GoDaddy::REST::Shell::DocsCommand;
14             use WWW::GoDaddy::REST::Shell::GetCommand;
15             use WWW::GoDaddy::REST::Shell::ListCommand;
16             use WWW::GoDaddy::REST::Shell::QueryCommand;
17              
18             eval "use WWW::GoDaddy::REST::Shell::GraphCommand";
19             if ($@) {
20             warn("Install the GraphViz module to enable the 'graph' command");
21             }
22              
23             sub init {
24             my $self = shift;
25              
26             my %options;
27              
28             my $res
29             = GetOptions( \%options, "url=s", "config=s", "username=s", "password=s", "man", "help" );
30             pod2usage(2) if !$res;
31             pod2usage(1) if $options{help};
32             pod2usage( -verbose => 2 ) if $options{man};
33              
34             my $conf_file = $options{config} || '';
35             if ( -e $conf_file ) {
36             my $config = Config::Any->load_files(
37             { 'files' => [$conf_file],
38             'use_ext' => 1
39             }
40             )->[0];
41              
42             foreach (qw/ url username password /) {
43             $options{$_} ||= $config->{$conf_file}->{$_};
44             }
45              
46             # some config files specify the opts like this
47             $options{password} ||= $config->{$conf_file}->{basic_password};
48             $options{username} ||= $config->{$conf_file}->{basic_username};
49             }
50              
51             if ( $options{username} or $options{password} ) {
52             if ( !$options{password} ) {
53             pod2usage("You a username must also be provided");
54             }
55             if ( !$options{username} ) {
56             pod2usage("You a password must also be provided");
57             }
58             }
59              
60             my $client_settings = { 'url' => $options{url} };
61             if ( $options{username} && $options{password} ) {
62             $client_settings->{'basic_username'} = $options{username};
63             $client_settings->{'basic_password'} = $options{password};
64             }
65              
66             if ( !$options{url} ) {
67             pod2usage("The --url must be provided either on the command line or in the config file");
68             }
69              
70             my $client = eval { return WWW::GoDaddy::REST->new($client_settings) };
71             if ($@) {
72             pod2usage($@);
73             }
74              
75             $self->client($client);
76              
77             return 1;
78             }
79              
80             sub preloop {
81             my ($self) = @_;
82              
83             my $client = $self->client;
84              
85             my $random_schema
86             = ( shuffle map { $_->id } grep { $_->is_queryable } @{ $client->schemas } )[0];
87             my $url = $client->url;
88              
89             print "Tab completion is your friend.\n\n";
90             $self->run('list');
91             print "\n";
92             print "Some sample commands to get you started:\n";
93             print " list\n";
94             print " man $random_schema\n";
95             print " get $random_schema 1234\n";
96             print " get $url/schemas\n";
97              
98             }
99              
100             sub client {
101             my ( $self, $new ) = @_;
102             if ($new) {
103             $self->{SHELL}->{GDCLIENT} = $new;
104             }
105             return $self->{SHELL}->{GDCLIENT};
106             }
107              
108             sub prompt_str {
109             "api> ";
110             }
111              
112             sub schema_completion {
113             my ( $self, $word, $line, $start ) = @_;
114             my @words = $self->line_parsed($line);
115              
116             if ( @words > 2 or @words == 2 and $start == length($line) ) {
117             return ();
118             }
119              
120             return grep { index( $_, $word ) == 0 } $self->schema_names();
121             }
122              
123             sub schema_names {
124             my ($self) = @_;
125             return sort map { $_->id } @{ $self->client->schemas };
126             }
127              
128             1;
129              
130             =head1 AUTHOR
131              
132             David Bartle, C<< <davidb@mediatemple.net> >>
133              
134             =head1 COPYRIGHT & LICENSE
135              
136             Copyright (c) 2014 Go Daddy Operating Company, LLC
137              
138             Permission is hereby granted, free of charge, to any person obtaining a
139             copy of this software and associated documentation files (the "Software"),
140             to deal in the Software without restriction, including without limitation
141             the rights to use, copy, modify, merge, publish, distribute, sublicense,
142             and/or sell copies of the Software, and to permit persons to whom the
143             Software is furnished to do so, subject to the following conditions:
144              
145             The above copyright notice and this permission notice shall be included in
146             all copies or substantial portions of the Software.
147              
148             THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
149             IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
150             FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
151             THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
152             LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
153             FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
154             DEALINGS IN THE SOFTWARE.
155              
156             =cut
157