| 1 | package App::gh::Command::Upload; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use base qw(App::gh::Command); | ||
| 5 | use Net::GitHub::Upload; | ||
| 6 | use App::gh::Utils; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | App::gh::Command::Upload - upload file to github. | ||
| 11 | |||
| 12 | =head1 USAGE | ||
| 13 | |||
| 14 | gh upload {file} [{repo}] | ||
| 15 | |||
| 16 | gh upload App-gh.tar.gz | ||
| 17 | |||
| 18 | gh upload App-gh.tar.gz c9s/App-gh | ||
| 19 | |||
| 20 | gh upload App-gh.tar.gz --cpan # also upload to cpan | ||
| 21 | |||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | |||
| 26 | sub options { ( 'c|cpan' => 'cpanupload') } | ||
| 27 | |||
| 28 | sub run { | ||
| 29 | my ( $self, $file, $repo ) = @_; | ||
| 30 | my $gh_id = App::gh->config->github_id; | ||
| 31 | my $gh_token = App::gh->config->github_token; | ||
| 32 | |||
| 33 | unless( $gh_id && $gh_token ) { | ||
| 34 | die "Github authtoken not found.\n"; | ||
| 35 | } | ||
| 36 | |||
| 37 | if( $self->{cpanupload} ) { | ||
| 38 | print "Uploading file to CPAN.\n"; | ||
| 39 | $self->cpanupload( $file ); | ||
| 40 | } | ||
| 41 | |||
| 42 | my $gh = Net::GitHub::Upload->new( | ||
| 43 | login => $gh_id, | ||
| 44 | token => $gh_token, | ||
| 45 | ); | ||
| 46 | |||
| 47 | $repo ||= $gh_id . '/' . $self->get_current_repo(); | ||
| 48 | print "Uploading $file to Github: $repo\n"; | ||
| 49 | eval { | ||
| 50 | $gh->upload( | ||
| 51 | repos => $repo, | ||
| 52 | file => $file, | ||
| 53 | ); | ||
| 54 | }; | ||
| 55 | print $@ . ':' . $! if $@; | ||
| 56 | print "Done\n"; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub cpanupload { | ||
| 61 | my ( $self, $file ) = @_; | ||
| 62 | eval { | ||
| 63 | use CPAN::Uploader; | ||
| 64 | }; | ||
| 65 | |||
| 66 | if( $@ ) { | ||
| 67 | warn 'Can not use CPAN::Uploader, please install it.'; | ||
| 68 | return; | ||
| 69 | } | ||
| 70 | |||
| 71 | my $from_file = CPAN::Uploader->read_config_file; | ||
| 72 | |||
| 73 | my %arg; | ||
| 74 | $arg{user} = uc $from_file->{user}; | ||
| 75 | if ( ! $arg{password} | ||
| 76 | and defined $from_file->{user} | ||
| 77 | and ($arg{user} eq uc $from_file->{user}) | ||
| 78 | ) { | ||
| 79 | $arg{password} = $from_file->{password}; | ||
| 80 | } | ||
| 81 | |||
| 82 | if (! $arg{password}) { | ||
| 83 | require Term::ReadKey; | ||
| 84 | local $| = 1; | ||
| 85 | print "PAUSE Password: "; | ||
| 86 | Term::ReadKey::ReadMode('noecho'); | ||
| 87 | chop($arg{password} = <STDIN>); | ||
| 88 | Term::ReadKey::ReadMode('restore'); | ||
| 89 | print "\n"; | ||
| 90 | } | ||
| 91 | |||
| 92 | CPAN::Uploader->upload_file( | ||
| 93 | $file, | ||
| 94 | \%arg, | ||
| 95 | ); | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | |||
| 101 | 1; |