File Coverage

blib/lib/App/GitHub/create.pm
Criterion Covered Total %
statement 19 78 24.3
branch 0 36 0.0
condition 0 23 0.0
subroutine 7 10 70.0
pod 0 3 0.0
total 26 150 17.3


line stmt bran cond sub pod time code
1             package App::GitHub::create;
2             BEGIN {
3 1     1   92886 $App::GitHub::create::VERSION = '0.0012';
4             }
5             # ABSTRACT: Create a github repository from the commandline
6              
7              
8 1     1   9 use strict;
  1         2  
  1         30  
9 1     1   4 use warnings;
  1         2  
  1         26  
10              
11 1     1   931 use Config::Identity::GitHub;
  1         69497  
  1         12  
12 1     1   1159 use LWP::UserAgent;
  1         50833  
  1         15  
13 1     1   1347 use Getopt::Long qw/ GetOptions /;
  1         12647  
  1         8  
14             my $agent = LWP::UserAgent->new;
15              
16             sub create {
17 0     0 0   my $self = shift;
18 0           my %given = @_;
19 0           my ( $login, $token, $name, $description, $homepage, $public );
20              
21 0           ( $name, $description, $homepage, $public ) =
22             @given{qw/ name description homepage public /};
23 0   0       defined $_ && length $_ or die "Missing name\n" for $name;
      0        
24              
25 0           ( $login, $token ) = @given{qw/ login token /};
26 0 0 0       unless( defined $token && length $token ) {
27 0           my %identity = Config::Identity::GitHub->load;
28 0           ( $login, $token ) = @identity{qw/ login token /};
29             }
30              
31              
32 0           my @arguments;
33 0 0         push @arguments, 'name' => $name if defined $name;
34 0 0         push @arguments, 'description' => $description if defined $description;
35 0 0         push @arguments, 'homepage' => $homepage if defined $homepage;
36 0 0 0       push @arguments, 'public' => $public if defined $public and $public;
37              
38 0           my $uri = "https://github.com/api/v2/json/repos/create";
39 0           my $response = $agent->post( $uri,
40             [ login => $login, token => $token, @arguments ] );
41              
42 0 0         unless ( $response->is_success ) {
43 0           die $response->status_line, ": ", $response->decoded_content, "\n";
44             }
45              
46 0           return $response;
47             }
48              
49             sub usage (;$) {
50 0     0 0   my $error = shift;
51 0           my $exit = 0;
52 0 0         if ( defined $error ) {
53 0 0         if ( $error ) {
54 0 0         if ( $error =~ m/^\-?\d+$/ ) { $exit = $error }
  0            
55             else {
56 0           chomp $error;
57 0           warn $error, "\n";
58 0           $exit = -1;
59             }
60             }
61             }
62 0           warn <<_END_;
63              
64             Usage: github-create [options]
65              
66             --login ... Your github login
67             --token ... The github token associated with the given login
68              
69             Although required, if a login/token are not given,
70             github-create will attempt to load it from
71             \$HOME/.github or \$HOME/.github-identity (see
72             Config::Identity for more information)
73              
74             --name ... The name of the repository to create (required)
75             --description ... A description of the repository (optional)
76             --homepage ... A homepage for the repository (optional)
77             --private The repository is private (default)
78             --public The repository is public
79              
80             --help, -h, -? This help
81              
82             _END_
83              
84 0           exit $exit;
85             }
86              
87             sub run {
88 0     0 0   my $self = shift;
89 0           my @arguments = @_;
90              
91 0           my ( $login, $token, $help );
92 0           my ( $name, $homepage, $description, $private, $public, $dry_run );
93              
94 0 0         usage 0 unless @arguments;
95              
96             {
97 0           local @ARGV = @arguments;
  0            
98 0           GetOptions(
99             'help|h|?' => \$help,
100              
101             'login=s' => \$login,
102             'token=s' => \$token,
103              
104             'name=s' => \$name,
105             'description=s' => \$description,
106             'homepage=s' => \$homepage,
107            
108             'private' => \$private,
109             'public' => \$public,
110              
111             'dry-run' => \$dry_run,
112             );
113 0           @arguments = @ARGV;
114             }
115              
116 0 0         usage 0 if $help;
117              
118 0 0 0       $name = $arguments[0] unless defined $name && length $name;
119 0 0 0       unless ( defined $name && length $name ) {
120 0           usage <<_END_;
121             github-create: Missing name (--name)
122             _END_
123             }
124              
125 0 0 0       if ( $private and $public ) {
126 0           usage <<_END_;
127             github-create: Repository cannot be both private AND public
128             _END_
129             }
130 0 0         $public = $public ? 1 : 0;
131              
132 0           eval {
133 0           my $owner = $login;
134 0 0         $owner = '' unless defined $owner;
135 0 0         unless ( $dry_run ) {
136              
137 0           my $response = $self->create(
138             login => $login, token => $token,
139             name => $name, description => $description, homepage => $homepage, public => $public,
140             );
141              
142 0           print $response->as_string;
143              
144 0           eval {
145 1     1   853 no warnings;
  1         3  
  1         236  
146 0           require JSON;
147 0           my $data = JSON->new->decode( $response->decoded_content );
148 0   0       $_ and $owner = $_ for $data->{repository}->{owner};
149             };
150             }
151              
152 0           print <<_END_;
153              
154             Repository $name created. To track it, run the following:
155              
156             # git remote add origin git\@github.com:$owner/$name.git
157             # git push origin master
158              
159             _END_
160             };
161 0 0         if ($@) {
162 0           usage <<_END_;
163             github-create: $@
164             _END_
165             }
166             }
167              
168             # for my $option (keys %options) {
169             # next unless $options{$option};
170             # push @arguments, "values[has_$option]" => 'true';
171             # }
172              
173             # --enable ... Enable wiki, issues, and/or the downloads page
174             # Can be a series of options separated by a comma:
175             #
176             # all Enable everything
177             # none Disable everything
178             # wiki Enable the wiki
179             # issues Enable issues
180             # downloads Enable downloads
181              
182             # The default is 'none'
183              
184             # my %options;
185             # my @enable = split m/\s*,\s*/, $enable;
186             # for ( @enable ) {
187             # s/^\s*//, s/\s*$//;
188             # next unless $_;
189             # if ( m/^none$/i ) { undef %options }
190             # elsif ( m/^all$/i ) { %options = qw/ wiki 1 issues 1 downloads 1 / }
191             # elsif ( m/^wiki$/i ) { $options{lc $_} = 1 }
192             # elsif ( m/^issues$/i ) { $options{lc $_} = 1 }
193             # elsif ( m/^downloads$/i ) { $options{lc $_} = 1 }
194             # else { usage <<_END_ }
195             #github-create: Unknown enable option: $_
196             #_END_
197             # }
198            
199             1;
200              
201             __END__