File Coverage

blib/lib/WWW/GoDaddy/REST/Shell.pm
Criterion Covered Total %
statement 37 99 37.3
branch 0 24 0.0
condition 0 23 0.0
subroutine 13 19 68.4
pod 0 6 0.0
total 50 171 29.2


line stmt bran cond sub pod time code
1             package WWW::GoDaddy::REST::Shell;
2 1     1   1182 use base qw(Term::Shell);
  1         2  
  1         463  
3              
4 1     1   14692 use warnings;
  1         2  
  1         22  
5 1     1   3 use strict;
  1         1  
  1         19  
6              
7 1     1   644 use Getopt::Long;
  1         8141  
  1         5  
8 1     1   590 use Config::Any;
  1         6447  
  1         28  
9 1     1   6 use List::Util qw(shuffle first);
  1         2  
  1         58  
10 1     1   494 use Pod::Usage;
  1         34517  
  1         136  
11              
12 1     1   9 use WWW::GoDaddy::REST;
  1         2  
  1         16  
13 1     1   446 use WWW::GoDaddy::REST::Shell::DocsCommand;
  1         2  
  1         6  
14 1     1   742 use WWW::GoDaddy::REST::Shell::GetCommand;
  1         2  
  1         5  
15 1     1   699 use WWW::GoDaddy::REST::Shell::ListCommand;
  1         2  
  1         28  
16 1     1   710 use WWW::GoDaddy::REST::Shell::QueryCommand;
  1         2  
  1         6  
17              
18 1     1   358 eval "use WWW::GoDaddy::REST::Shell::GraphCommand";
  0            
  0            
19             if ($@) {
20             warn("Install the GraphViz module to enable the 'graph' command");
21             }
22              
23             sub init {
24 0     0 0   my $self = shift;
25              
26 0           my %options;
27              
28 0           my $res
29             = GetOptions( \%options, "url=s", "config=s", "username=s", "password=s", "man", "help" );
30 0 0         pod2usage(2) if !$res;
31 0 0         pod2usage(1) if $options{help};
32 0 0         pod2usage( -verbose => 2 ) if $options{man};
33              
34 0   0       my $conf_file = $options{config} || '';
35 0 0         if ( -e $conf_file ) {
36 0           my $config = Config::Any->load_files(
37             { 'files' => [$conf_file],
38             'use_ext' => 1
39             }
40             )->[0];
41              
42 0           foreach (qw/ url username password /) {
43 0   0       $options{$_} ||= $config->{$conf_file}->{$_};
44             }
45              
46             # some config files specify the opts like this
47 0   0       $options{password} ||= $config->{$conf_file}->{basic_password};
48 0   0       $options{username} ||= $config->{$conf_file}->{basic_username};
49             }
50              
51 0 0 0       if ( $options{username} or $options{password} ) {
52 0 0         if ( !$options{password} ) {
53 0           pod2usage("You a username must also be provided");
54             }
55 0 0         if ( !$options{username} ) {
56 0           pod2usage("You a password must also be provided");
57             }
58             }
59              
60 0           my $client_settings = { 'url' => $options{url} };
61 0 0 0       if ( $options{username} && $options{password} ) {
62 0           $client_settings->{'basic_username'} = $options{username};
63 0           $client_settings->{'basic_password'} = $options{password};
64             }
65              
66 0 0         if ( !$options{url} ) {
67 0           pod2usage("The --url must be provided either on the command line or in the config file");
68             }
69              
70 0           my $client = eval { return WWW::GoDaddy::REST->new($client_settings) };
  0            
71 0 0         if ($@) {
72 0           pod2usage($@);
73             }
74              
75 0           $self->client($client);
76              
77 0           return 1;
78             }
79              
80             sub preloop {
81 0     0 0   my ($self) = @_;
82              
83 0           my $client = $self->client;
84              
85 0           my $random_schema
86 0           = ( shuffle map { $_->id } grep { $_->is_queryable } @{ $client->schemas } )[0];
  0            
  0            
87 0           my $url = $client->url;
88              
89 0           print "Tab completion is your friend.\n\n";
90 0           $self->run('list');
91 0           print "\n";
92 0           print "Some sample commands to get you started:\n";
93 0           print " list\n";
94 0           print " man $random_schema\n";
95 0           print " get $random_schema 1234\n";
96 0           print " get $url/schemas\n";
97              
98             }
99              
100             sub client {
101 0     0 0   my ( $self, $new ) = @_;
102 0 0         if ($new) {
103 0           $self->{SHELL}->{GDCLIENT} = $new;
104             }
105 0           return $self->{SHELL}->{GDCLIENT};
106             }
107              
108             sub prompt_str {
109 0     0 0   "api> ";
110             }
111              
112             sub schema_completion {
113 0     0 0   my ( $self, $word, $line, $start ) = @_;
114 0           my @words = $self->line_parsed($line);
115              
116 0 0 0       if ( @words > 2 or @words == 2 and $start == length($line) ) {
      0        
117 0           return ();
118             }
119              
120 0           return grep { index( $_, $word ) == 0 } $self->schema_names();
  0            
121             }
122              
123             sub schema_names {
124 0     0 0   my ($self) = @_;
125 0           return sort map { $_->id } @{ $self->client->schemas };
  0            
  0            
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