forked from docker-library/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.pl
executable file
·199 lines (154 loc) · 5.32 KB
/
push.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env perl
use strict;
use warnings;
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::UI;
use Term::ReadLine;
my $username;
my $password;
my $batchmode;
GetOptions(
'u|username=s' => \$username,
'p|password=s' => \$password,
'batchmode!' => \$batchmode,
) or die 'bad args';
die 'no repos specified' unless @ARGV;
my $ua = Mojo::UserAgent->new->max_redirects(10);
# Mojo::UserAgent::CookieJar::find is destructive...
# this is a nondestructive version that makes the login succeed on the Hub
Mojo::Util::monkey_patch 'Mojo::UserAgent::CookieJar', find => sub {
my ($self, $url) = @_;
return unless my $domain = my $host = $url->ihost;
my $path = $url->path->to_abs_string;
my @found;
while ($domain =~ /[^.]+\.[^.]+|localhost$/) {
next unless my $old = $self->{jar}{$domain};
# Grab cookies
#my $new = $self->{jar}{$domain} = [];
for my $cookie (@$old) {
next unless $cookie->domain || $host eq $cookie->origin;
# Check if cookie has expired
my $expires = $cookie->expires;
next if $expires && time > ($expires || 0);
#push @$new, $cookie;
# Taste cookie
next if $cookie->secure && $url->protocol ne 'https';
next unless Mojo::UserAgent::CookieJar::_path($cookie->path, $path);
my $name = $cookie->name;
my $value = $cookie->value;
push @found, Mojo::Cookie::Request->new(name => $name, value => $value);
}
}
# Remove another part
continue { $domain =~ s/^[^.]+\.?// }
return @found;
};
sub get_form_bits {
my $form = shift;
my $ret = {};
$form->find('input, textarea')->grep(sub {
!$_->match('input[type=submit], input[type=reset], input[type=button]')
&& defined($_->attr('name'))
})->each(sub {
my $e = shift;
my $name = $e->attr('name');
my $val;
if ($e->type eq 'textarea') {
$val = $e->text;
}
else {
$val = $e->attr('value');
}
$val = trim('' . ($val // ''));
$val =~ s!\r\n|\r!\n!g;
$ret->{$name} = $val;
});
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;
if ($batchmode) {
$reply = 'yes';
}
else {
$reply = $term->get_reply(
prompt => 'Apply changes?',
choices => [ qw( yes vimdiff no quit ) ],
default => 'yes',
);
}
if ($reply eq 'quit') {
say 'quitting, as requested';
exit;
}
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) {
$username = $term->get_reply(prompt => 'Hub Username');
}
$loginBits->{username} = $username;
unless (defined $password) {
$password = $term->get_reply(prompt => 'Hub Password'); # TODO hide the input? O:)
}
$loginBits->{password} = $password;
$login = $ua->post($login->req->url->to_abs => {
Referer => $login->req->url->to_abs->to_string,
} => form => $loginBits);
die 'login failed' unless $login->success;
my $error = $login->res->dom('.alert-error');
if ($error->size) {
die $error->pluck('all_text')->join("\n") . "\n";
}
while (my $repo = shift) { # '/_/hylang', '/u/tianon/perl', etc
$repo =~ s!/+$!!;
$repo = '/_/' . $repo unless $repo =~ m!/!;
$repo = '/' . $repo unless $repo =~ m!^/!;
my $repoName = $repo;
$repoName =~ s!^.*/!!; # 'hylang', 'perl', etc
my $repoUrl = 'https://registry.hub.docker.com' . $repo . '/settings/';
my $repoTx = $ua->get($repoUrl);
warn 'failed to get: ' . $repoUrl and next unless $repoTx->success;
my $settingsForm = $repoTx->res->dom('form[name="repository_settings"]')->first;
die 'failed to find form on ' . $repoUrl unless $settingsForm;
my $settingsBits = get_form_bits($settingsForm);
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;
my $alert = $repoTx->res->dom('.alert-error');
if ($alert->size) {
my $text = trim $alert->pluck('all_text');
die 'update to ' . $repoUrl . ' failed:' . "\n" . $text if $text;
}
}