From 5f529b31c87c354395acac3ee376e4ccfc8f862a Mon Sep 17 00:00:00 2001 From: Torsten Raudssus Date: Tue, 11 Jun 2013 18:59:41 +0200 Subject: [PATCH] Added poupload command --- lib/App/DuckPAN/Cmd/Poupload.pm | 61 +++++++++++++++++++++++++++++++++ lib/App/DuckPAN/DDG.pm | 12 +++++++ lib/App/DuckPAN/Help.pm | 4 +++ 3 files changed, 77 insertions(+) create mode 100644 lib/App/DuckPAN/Cmd/Poupload.pm diff --git a/lib/App/DuckPAN/Cmd/Poupload.pm b/lib/App/DuckPAN/Cmd/Poupload.pm new file mode 100644 index 0000000..992121e --- /dev/null +++ b/lib/App/DuckPAN/Cmd/Poupload.pm @@ -0,0 +1,61 @@ +package App::DuckPAN::Cmd::Poupload; +# ABSTRACT: Command for uploading .po files to the DuckDuckGo Community Platform + +use Moo; +with qw( App::DuckPAN::Cmd ); + +use MooX::Options; +use HTTP::Request::Common qw(POST); +use LWP::UserAgent; +use Path::Class; +use Dist::Zilla::Util; + +option domain => ( + is => 'ro', + format => 's', + predicate => 1, +); + +option upload_uri => ( + is => 'ro', + format => 's', + lazy => 1, + builder => 1, +); + +sub _build_upload_uri { 'https://dukgo.com/translate/po/upload' } + +sub get_request { + my ( $self, $file ) = @_; + my ( $user, $pass ) = $self->app->ddg->get_dukgo_user_pass; + my $req = POST( + $self->upload_uri, + Content_Type => 'form-data', + Content => { + CAN_MULTIPART => 1, + HIDDENNAME => $user, + po_upload => [ $file ], + $self->has_domain ? ( token_domain => $self->domain ) : (), + }, + ); + $req->authorization_basic($user, $pass); + return $req; +} + +sub run { + my ( $self, @args ) = @_; + for (@args) { + $self->upload($_); + } +} + +sub upload { + my ( $self, $file ) = @_; + die "File not found" unless -f $file; + print "Uploading ".$file."... "; + my $response = $self->app->http->request($self->get_request($file)); + die "Error: ".$response->code if $response->is_error || $response->is_redirect; + print "success!\n"; +} + +1; diff --git a/lib/App/DuckPAN/DDG.pm b/lib/App/DuckPAN/DDG.pm index 96b3d86..5ae8079 100644 --- a/lib/App/DuckPAN/DDG.pm +++ b/lib/App/DuckPAN/DDG.pm @@ -7,6 +7,18 @@ with 'App::DuckPAN::HasApp'; use Module::Pluggable::Object; use Class::Load ':all'; +sub get_dukgo_user_pass { + my ( $self ) = @_; + my $config = $self->app->perl->get_dzil_config; + unless (defined $config->{'%DUKGO'}) { + shift->app->print_text( + "[ERROR] No configuration found for your https://dukgo.com/ username and password, please use: 'dzil setup' first!", + ); + exit 1; + } + return $config->{'%DUKGO'}->{username}, $config->{'%DUKGO'}->{password}; +} + sub get_blocks_from_current_dir { my ( $self, @args ) = @_; unless ($self->app->get_local_ddg_version) { diff --git a/lib/App/DuckPAN/Help.pm b/lib/App/DuckPAN/Help.pm index afde70f..0745933 100644 --- a/lib/App/DuckPAN/Help.pm +++ b/lib/App/DuckPAN/Help.pm @@ -48,6 +48,10 @@ duckpan env rm --------------------- Remove an environment variable from duckpan +duckpan poupload +--------------- + Upload a po file to the Community Platform (Translation manager only) + duckpan release --------------- Release the project of the current directory to DuckPAN [TODO]