Skip to content

Commit

Permalink
Reorganize "push.pl" to put the "vimdiff" logic in one place for cool…
Browse files Browse the repository at this point in the history
… dispatch-conf-alike behavior
  • Loading branch information
tianon committed Apr 20, 2015
1 parent b1bbd39 commit 2633e18
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM perl:5.20

RUN apt-get update && apt-get install -y git vim --no-install-recommends && rm -rf /var/lib/apt/lists/*

# secure by default ♥ (thanks to sri!)
ENV PERL_CPANM_OPT --verbose --mirror https://cpan.metacpan.org
# TODO find a way to make --mirror-only / SSL work with backpan too :(
Expand All @@ -17,9 +19,7 @@ RUN cpanm IO::Socket::IP
RUN cpanm --notest IO::Socket::SSL
# the tests for IO::Socket::SSL like to hang... :(

RUN cpanm Term::ReadKey

RUN apt-get update && apt-get install -y git vim --no-install-recommends && rm -rf /var/lib/apt/lists/*
RUN cpanm Term::UI

ENV LANG C.UTF-8

Expand Down
83 changes: 45 additions & 38 deletions push.pl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
use 5.010;
use open ':encoding(utf8)';

use File::Basename qw(fileparse);
use File::Temp;
use Getopt::Long;
use Mojo::UserAgent;
use Mojo::Util qw(decode encode slurp spurt trim);
use Term::ReadKey;

use Term::UI;
use Term::ReadLine;

my $username;
my $password;
my $all;
GetOptions(
'u|username=s' => \$username,
'p|password=s' => \$password,
'a|all' => \$all,
) or die 'bad args';

die 'no repos specified' unless @ARGV;
Expand Down Expand Up @@ -88,26 +89,54 @@ sub get_form_bits {
return $ret;
}

my $term = Term::ReadLine->new('docker-library-docs-push');

sub prompt_for_edit {
my ($currentText, $proposedFile) = @_;

my $proposedText = slurp $proposedFile or warn 'missing ' . $proposedFile;
$proposedText = trim(decode('UTF-8', $proposedText));

return $currentText if $currentText eq $proposedText;

my @proposedFileBits = fileparse($proposedFile, qr!\.[^.]*!);
my $file = File::Temp->new(SUFFIX => $proposedFileBits[2]);
my $filename = $file->filename;
spurt encode('UTF-8', $currentText . "\n"), $filename;

system(qw(git --no-pager diff --no-index), $filename, $proposedFile);

my $reply = $term->get_reply(
prompt => 'Apply changes?',
choices => [ qw( yes vimdiff no ) ],
default => 'yes',
);

if ($reply eq 'yes') {
return $proposedText;
}

if ($reply eq 'vimdiff') {
system('vimdiff', $filename, $proposedFile) == 0 or die "vimdiff on $filename and $proposedFile failed";
return trim(decode('UTF-8', slurp($filename)));
}

return $currentText;
}

my $login = $ua->get('https://registry.hub.docker.com/account/login/');
die 'login failed' unless $login->success;

my $loginForm = $login->res->dom('#form-login')->first;
my $loginBits = get_form_bits($loginForm);

unless (defined $username) {
print 'Hub Username: ';
$username = ReadLine 0;
chomp $username;
$username = $term->get_reply(prompt => 'Hub Username');
}
$loginBits->{username} = $username;

unless (defined $password) {
print 'Hub Password: ';
ReadMode 2;
$password = ReadLine 0;
chomp $password;
ReadMode 0;
print "\n";
$password = $term->get_reply(prompt => 'Hub Password'); # TODO hide the input? O:)
}
$loginBits->{password} = $password;

Expand All @@ -128,14 +157,6 @@ sub get_form_bits {
my $repoName = $repo;
$repoName =~ s!^.*/!!; # 'hylang', 'perl', etc

my $shortFile = $repoName . '/README-short.txt';
my $short = slurp $shortFile or warn 'missing ' . $shortFile;
$short = trim(decode('UTF-8', $short));

my $longFile = $repoName . '/README.md';
my $long = slurp $longFile or warn 'missing ' . $longFile;
$long = trim(decode('UTF-8', $long));

my $repoUrl = 'https://registry.hub.docker.com' . $repo . '/settings/';
my $repoTx = $ua->get($repoUrl);
die 'failed to get: ' . $repoUrl unless $repoTx->success;
Expand All @@ -144,30 +165,16 @@ sub get_form_bits {
die 'failed to find form on ' . $repoUrl unless $settingsForm;
my $settingsBits = get_form_bits($settingsForm);

my $hubShort = $settingsBits->{description};
my $hubLong = $settingsBits->{full_description};

if ($hubShort ne $short) {
my $file = File::Temp->new(SUFFIX => '.txt');
my $filename = $file->filename;
spurt encode('UTF-8', $hubShort . "\n"), $filename;
system('vimdiff', $filename, $shortFile) == 0 or die "vimdiff on $filename and $shortFile failed";
$hubShort = trim(decode('UTF-8', slurp($filename)));
}

if ($hubLong ne $long) {
my $file = File::Temp->new(SUFFIX => '.md');
my $filename = $file->filename;
spurt encode('UTF-8', $hubLong . "\n"), $filename;
system('vimdiff', $filename, $longFile) == 0 or die "vimdiff on $filename and $longFile failed";
$hubLong = trim(decode('UTF-8', slurp($filename)));
}
my $hubShort = prompt_for_edit($settingsBits->{description}, $repoName . '/README-short.txt');
my $hubLong = prompt_for_edit($settingsBits->{full_description}, $repoName . '/README.md');

say 'no change to ' . $repoName . '; skipping' and next if $settingsBits->{description} eq $hubShort and $settingsBits->{full_description} eq $hubLong;

$settingsBits->{description} = $hubShort;
$settingsBits->{full_description} = $hubLong;

say 'updating ' . $repoName;

$repoTx = $ua->post($repoUrl => { Referer => $repoUrl } => form => $settingsBits);
die 'post to ' . $repoUrl . ' failed' unless $repoTx->success;

Expand Down

0 comments on commit 2633e18

Please sign in to comment.