| 1 | package | ||
| 2 | Pinto::Remote::SelfContained::App; # hide from PAUSE | ||
| 3 | # ABSTRACT: app class for running Pinto commands | ||
| 4 | |||
| 5 | |||
| 6 | use v5.10; | ||
| 7 | use Moo; | ||
| 8 | |||
| 9 | use Getopt::Long::Descriptive qw(describe_options); | ||
| 10 | use IO::Interactive qw(is_interactive); | ||
| 11 | use List::Util qw(max pairgrep); | ||
| 12 | use Path::Tiny qw(path); | ||
| 13 | use Pinto::Remote::SelfContained; | ||
| 14 | use Pinto::Remote::SelfContained::Chrome; | ||
| 15 | use Pinto::Remote::SelfContained::Types qw(Uri); | ||
| 16 | use Pinto::Remote::SelfContained::Util qw(current_username get_password); | ||
| 17 | use Types::Standard qw(ArrayRef Bool HashRef Int Maybe Str); | ||
| 18 | |||
| 19 | use namespace::clean; | ||
| 20 | |||
| 21 | our $VERSION = '1.999'; # TRIAL VERSION | ||
| 22 | |||
| 23 | has root => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => Uri, | ||
| 26 | coerce => 1, | ||
| 27 | default => sub { | ||
| 28 | $ENV{PINTO_REPOSITORY_ROOT} || die "root is required, either via --root or by setting PINTO_REPOSITORY_ROOT='...'" | ||
| 29 | } | ||
| 30 | ); | ||
| 31 | |||
| 32 | has authenticate => ( | ||
| 33 | is => 'ro', | ||
| 34 | isa => Bool, | ||
| 35 | default => sub { $ENV{PINTO_AUTHENTICATE} // !!length($ENV{PINTO_PASSEXEC}) }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | has username => ( | ||
| 39 | is => 'ro', | ||
| 40 | isa => Str, | ||
| 41 | default => sub { current_username() }, | ||
| 42 | ); | ||
| 43 | |||
| 44 | has passexec => ( | ||
| 45 | is => 'lazy', | ||
| 46 | isa => Maybe[Str], | ||
| 47 | default => sub { $ENV{PINTO_PASSEXEC} }, | ||
| 48 | ); | ||
| 49 | |||
| 50 | has password => ( | ||
| 51 | is => 'lazy', | ||
| 52 | isa => Maybe[Str], | ||
| 53 | ); | ||
| 54 | sub _build_password { | ||
| 55 | my $self = shift; | ||
| 56 | return $self->authenticate ? ( | ||
| 57 | $self->passexec ? password_exec($self->passexec) : get_password() | ||
| 58 | ) : (); | ||
| 59 | } | ||
| 60 | |||
| 61 | has quiet => (is => 'ro', isa => Bool); | ||
| 62 | has verbose => (is => 'ro', isa => Int, default => 1); | ||
| 63 | |||
| 64 | has action_name => (is => 'ro', isa => Str, required => 1); | ||
| 65 | has args => (is => 'ro', isa => HashRef, default => sub { +{} }); | ||
| 66 | |||
| 67 | my @ATTRS_FROM_OPTIONS = qw(root username passexec authenticate quiet verbose); | ||
| 68 | |||
| 69 | sub command_info { | ||
| 70 | # The "args" is a string listing argument names, each optionally suffixed | ||
| 71 | # by one of ?+* to indicate optional, required-slurpy, optional-slurpy. | ||
| 72 | # Optional items must follow all required ones, with the exception that | ||
| 73 | # 'foo? bar' is a permitted spec; this isn't verified. | ||
| 74 | my %ret = ( | ||
| 75 | add => { | ||
| 76 | summary => 'add local archives to the repository', | ||
| 77 | usage_desc => '%c %o ARCHIVE', | ||
| 78 | args => sub { | ||
| 79 | my (undef, $usage) = splice @_, 0, 2; | ||
| 80 | $usage->die({ pre_text => "Need exactly one archive\n\n" }) | ||
| 81 | if @_ != 1; | ||
| 82 | my $filename = $_[0]; | ||
| 83 | die "Archive filename $filename must be a tarball\n" | ||
| 84 | if $filename !~ /\.tar\.gz/; | ||
| 85 | return archives => [{ | ||
| 86 | name => 'archives', | ||
| 87 | filename => $filename, | ||
| 88 | type => 'application/x-tar', | ||
| 89 | encoding => 'gzip', | ||
| 90 | }]; | ||
| 91 | }, | ||
| 92 | opt_spec => [ | ||
| 93 | [ 'author=s' => 'The ID of the archive author' ], | ||
| 94 | [ 'cascade' => 'Always pick latest upstream package' ], | ||
| 95 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 96 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 97 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 98 | [ 'no-fail' => 'Do not fail when there is an error' ], | ||
| 99 | [ 'no-index|x=s@' => 'Do not index matching packages' ], | ||
| 100 | [ 'recurse!' => 'Recursively pull prereqs (negatable)' ], | ||
| 101 | [ 'pin' => 'Pin packages to the stack' ], | ||
| 102 | [ 'skip-missing-prerequisite|k=s@' => 'Skip missing prereq (repeatable)' ], | ||
| 103 | [ 'skip-all-missing-prerequisites|K' => 'Skip all missing prereqs' ], | ||
| 104 | [ 'stack|s=s' => 'Put packages into this stack' ], | ||
| 105 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 106 | [ 'with-development-prerequisites|wd' => 'Also pull prereqs for development' ], | ||
| 107 | ], | ||
| 108 | }, | ||
| 109 | clean => { | ||
| 110 | summary => 'remove orphaned distribution archives', | ||
| 111 | usage_desc => '%c %o', | ||
| 112 | opt_spec => [], | ||
| 113 | }, | ||
| 114 | commands => { | ||
| 115 | pos => 10, | ||
| 116 | summary => q[list the application's commands], | ||
| 117 | usage_desc => '%c %o', | ||
| 118 | opt_spec => [], | ||
| 119 | }, | ||
| 120 | copy => { | ||
| 121 | summary => 'create a new stack by copying another', | ||
| 122 | usage_desc => '%c %o FROM-STACK TO-STACK', | ||
| 123 | args => 'stack to_stack', | ||
| 124 | opt_spec => [ | ||
| 125 | [ 'default' => 'Make the new stack the default stack' ], | ||
| 126 | [ 'description|d=s' => 'Brief description of the stack' ], | ||
| 127 | [ 'lock' => 'Lock the new stack to prevent changes' ], | ||
| 128 | ], | ||
| 129 | }, | ||
| 130 | default => { | ||
| 131 | summary => 'mark the default stack', | ||
| 132 | usage_desc => '%c %o (--none | STACK)', | ||
| 133 | args => sub { | ||
| 134 | my ($opts, $usage) = splice @_, 0, 2; | ||
| 135 | $usage->die({ pre_text => "You must specify either a stack or --none\n\n" }) | ||
| 136 | if !@_ && !$opts->{none}; | ||
| 137 | $usage->die({ pre_text => "You cannot specify both a stack and --none\n\n" }) | ||
| 138 | if @_ && $opts->{none}; | ||
| 139 | $usage->die({ pre_text => "Too many arguments\n\n" }) | ||
| 140 | if @_ > 1; | ||
| 141 | return @_ ? (stack => $_[0]) : (); | ||
| 142 | }, | ||
| 143 | opt_spec => [ | ||
| 144 | [ 'none' => 'Unmark the default stack' ], | ||
| 145 | ], | ||
| 146 | }, | ||
| 147 | delete => { | ||
| 148 | summary => 'permanently remove an archive', | ||
| 149 | usage_desc => '%c %o TARGET...', | ||
| 150 | args => 'targets*', | ||
| 151 | opt_spec => [ | ||
| 152 | [ 'force' => 'Delete even if packages are pinned' ], | ||
| 153 | ], | ||
| 154 | }, | ||
| 155 | diff => { | ||
| 156 | summary => 'show difference between two stacks', | ||
| 157 | usage_desc => '%c %o [LEFT] RIGHT', | ||
| 158 | args => 'left? right', | ||
| 159 | opt_spec => [ | ||
| 160 | [ 'diff-style=s' => 'Diff style (concise|detailed)' ], | ||
| 161 | [ 'format=s' => 'Format specification' ], | ||
| 162 | ], | ||
| 163 | }, | ||
| 164 | help => { | ||
| 165 | pos => 20, | ||
| 166 | summary => q[display a command's help screen], | ||
| 167 | usage_desc => '%c %o [SUBCOMMAND]...', | ||
| 168 | opt_spec => [], | ||
| 169 | }, | ||
| 170 | install => { | ||
| 171 | summary => 'install stuff from the repository', | ||
| 172 | usage_desc => '%c %o TARGET...', | ||
| 173 | args => sub { | ||
| 174 | my ($opts) = splice @_, 0, 2; | ||
| 175 | if (my $cpanm_options = $opts->{cpanm_options}) { | ||
| 176 | my %new; | ||
| 177 | for my $item (@$cpanm_options) { | ||
| 178 | my ($name, $value) = $item =~ /\A--?(.+?)(?:=(.*?))?\z/ms; | ||
| 179 | $new{$name} = $value; | ||
| 180 | } | ||
| 181 | $opts->{cpanm_options} = \%new; | ||
| 182 | } | ||
| 183 | $opts->{cpanm_options}{'local-lib'} = $_ for grep defined, delete $opts->{local_lib}; | ||
| 184 | $opts->{cpanm_options}{'local-lib-contained'} = $_ for grep defined, delete $opts->{local_lib_contained}; | ||
| 185 | return targets => [@_]; | ||
| 186 | }, | ||
| 187 | opt_spec => [ | ||
| 188 | [ 'cascade' => 'Always pick latest upstream package' ], | ||
| 189 | [ 'cpanm-exe|cpanm=s' => 'Path to the cpanm executable' ], | ||
| 190 | [ 'cpanm-options|o=s@' => 'name=value pairs of cpanm options' ], | ||
| 191 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 192 | [ 'local-lib|l=s' => 'install into a local lib directory' ], | ||
| 193 | [ 'local-lib-contained|L=s' => 'install into a contained local lib directory' ], | ||
| 194 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 195 | [ 'do-pull' => 'pull missing prereqs onto the stack first' ], | ||
| 196 | [ 'stack|s=s' => 'Install modules from this stack' ], | ||
| 197 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 198 | ], | ||
| 199 | }, | ||
| 200 | kill => { | ||
| 201 | summary => 'permanently delete a stack', | ||
| 202 | usage_desc => '%c %o STACK', | ||
| 203 | args => 'stack', | ||
| 204 | opt_spec => [ | ||
| 205 | [ 'force' => 'Kill even if stack is locked' ], | ||
| 206 | ], | ||
| 207 | }, | ||
| 208 | list => { | ||
| 209 | summary => 'show the packages in a stack', | ||
| 210 | usage_desc => '%c %o [STACK]', | ||
| 211 | args => 'stack?', | ||
| 212 | opt_spec => [ | ||
| 213 | [ 'all|a' => 'List everything in the repository'], | ||
| 214 | [ 'authors|A=s' => 'Limit to matching author identities' ], | ||
| 215 | [ 'distributions|D=s' => 'Limit to matching distribution names' ], | ||
| 216 | [ 'packages|P=s' => 'Limit to matching package names' ], | ||
| 217 | [ 'pinned!' => 'Limit to pinned packages (negatable)' ], | ||
| 218 | [ 'format=s' => 'Format specification' ], | ||
| 219 | [ 'stack|s=s' => 'List contents of this stack' ], | ||
| 220 | ], | ||
| 221 | }, | ||
| 222 | lock => { | ||
| 223 | summary => 'mark a stack as read-only', | ||
| 224 | usage_desc => '%c %o [STACK]', | ||
| 225 | args => 'stack?', | ||
| 226 | opt_spec => [ | ||
| 227 | [ 'stack|s=s' => 'Lock this stack' ], | ||
| 228 | ], | ||
| 229 | }, | ||
| 230 | log => { | ||
| 231 | summary => 'show the revision logs of a stack', | ||
| 232 | usage_desc => '%c %o [STACK]', | ||
| 233 | args => 'stack?', | ||
| 234 | opt_spec => [ | ||
| 235 | [ 'stack|s=s' => 'Show history for this stack' ], | ||
| 236 | [ 'with-diffs|d' => 'Show a diff for each revision'], | ||
| 237 | [ 'diff-style=s' => 'Diff style (concise|detailed)' ], | ||
| 238 | ], | ||
| 239 | }, | ||
| 240 | look => { | ||
| 241 | summary => 'unpack and explore distributions with your shell', | ||
| 242 | usage_desc => '%c %o TARGET...', | ||
| 243 | args => 'targets+', | ||
| 244 | opt_spec => [ | ||
| 245 | [ 'stack|s=s' => 'Resolve targets against this stack' ], | ||
| 246 | ], | ||
| 247 | }, | ||
| 248 | new => { | ||
| 249 | summary => 'create a new empty stack', | ||
| 250 | usage_desc => '%c %o STACK', | ||
| 251 | args => 'stack', | ||
| 252 | opt_spec => [ | ||
| 253 | [ 'default' => 'Make the new stack the default stack' ], | ||
| 254 | [ 'description|d=s' => 'Brief description of the stack' ], | ||
| 255 | [ 'target-perl-version|tpv=s' => 'Target Perl version for this stack' ], | ||
| 256 | ], | ||
| 257 | }, | ||
| 258 | nop => { | ||
| 259 | summary => 'do nothing', | ||
| 260 | usage_desc => '%c %o', | ||
| 261 | opt_spec => [ | ||
| 262 | [ 'sleep=i' => 'seconds to sleep before exiting' ], | ||
| 263 | ], | ||
| 264 | }, | ||
| 265 | pin => { | ||
| 266 | summary => 'force a package to stay in a stack', | ||
| 267 | usage_desc => '%c %o TARGET...', | ||
| 268 | args => 'targets*', | ||
| 269 | opt_spec => [ | ||
| 270 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 271 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 272 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 273 | [ 'stack|s=s' => 'Pin targets to this stack' ], | ||
| 274 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 275 | ], | ||
| 276 | }, | ||
| 277 | props => { | ||
| 278 | summary => 'show or set stack properties', | ||
| 279 | usage_desc => '%c %o [STACK]', | ||
| 280 | args => 'stack?', | ||
| 281 | opt_spec => [ | ||
| 282 | [ 'format=s' => 'Format specification' ], | ||
| 283 | [ 'properties|prop|P=s%' => 'name=value pairs of properties' ], | ||
| 284 | ], | ||
| 285 | }, | ||
| 286 | pull => { | ||
| 287 | summary => 'pull archives from upstream repositories', | ||
| 288 | usage_desc => '%c %o TARGET...', | ||
| 289 | args => 'targets*', | ||
| 290 | opt_spec => [ | ||
| 291 | [ 'cascade' => 'Always pick latest upstream package' ], | ||
| 292 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 293 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 294 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 295 | [ 'no-fail' => 'Do not fail when there is an error' ], | ||
| 296 | [ 'recurse!' => 'Recursively pull prereqs (negatable)' ], | ||
| 297 | [ 'pin' => 'Pin the packages to the stack' ], | ||
| 298 | [ 'skip-missing-prerequisite|k=s@' => 'Skip missing prereq (repeatable)' ], | ||
| 299 | [ 'skip-all-missing-prerequisites|K' => 'Skip all missing prereqs' ], | ||
| 300 | [ 'stack|s=s' => 'Put packages into this stack' ], | ||
| 301 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 302 | [ 'with-development-prerequisites|wd' => 'Also pull prereqs for development' ], | ||
| 303 | ], | ||
| 304 | }, | ||
| 305 | register => { | ||
| 306 | summary => 'put existing packages on a stack', | ||
| 307 | usage_desc => '%c %o TARGET...', | ||
| 308 | args => 'targets*', | ||
| 309 | opt_spec => [ | ||
| 310 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 311 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 312 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 313 | [ 'pin' => 'Pin packages to the stack' ], | ||
| 314 | [ 'stack|s=s' => 'Remove packages from this stack' ], | ||
| 315 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 316 | ], | ||
| 317 | }, | ||
| 318 | rename => { | ||
| 319 | summary => 'change the name of a stack', | ||
| 320 | usage_desc => '%c %o STACK TO-STACK', | ||
| 321 | args => 'stack to_stack', | ||
| 322 | opt_spec => [], | ||
| 323 | }, | ||
| 324 | reset => { | ||
| 325 | summary => 'reset stack to a prior revision', | ||
| 326 | usage_desc => '%c %o [STACK] REVISION', | ||
| 327 | args => 'stack? revision', | ||
| 328 | opt_spec => [ | ||
| 329 | [ 'force' => 'Reset even if revision is not ancestor' ], | ||
| 330 | [ 'stack|s=s' => 'Reset this stack' ], | ||
| 331 | ], | ||
| 332 | }, | ||
| 333 | revert => { | ||
| 334 | summary => 'revert stack to a prior revision', | ||
| 335 | usage_desc => '%c %o [STACK] REVISION', | ||
| 336 | args => 'stack? revision', | ||
| 337 | opt_spec => [ | ||
| 338 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 339 | [ 'force' => 'Revert even if revision is not ancestor' ], | ||
| 340 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 341 | [ 'stack|s=s' => 'Revert this stack' ], | ||
| 342 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 343 | ], | ||
| 344 | }, | ||
| 345 | roots => { | ||
| 346 | summary => 'show the roots of a stack', | ||
| 347 | usage_desc => '%c %o [STACK]', | ||
| 348 | args => 'stack?', | ||
| 349 | opt_spec => [ | ||
| 350 | [ 'format=s' => 'Format specification' ], | ||
| 351 | [ 'stack|s=s' => 'Show roots of this stack' ], | ||
| 352 | ], | ||
| 353 | }, | ||
| 354 | stacks => { | ||
| 355 | summary => 'show available stacks', | ||
| 356 | usage_desc => '%c %o', | ||
| 357 | opt_spec => [ | ||
| 358 | [ 'format=s' => 'Format of the listing' ], | ||
| 359 | ], | ||
| 360 | }, | ||
| 361 | statistics => { | ||
| 362 | summary => 'report statistics about the repository', | ||
| 363 | usage_desc => '%c %o [STACK]', | ||
| 364 | args => 'stack?', | ||
| 365 | opt_spec => [], | ||
| 366 | }, | ||
| 367 | unlock => { | ||
| 368 | summary => 'mark a stack as writable', | ||
| 369 | usage_desc => '%c %o [STACK]', | ||
| 370 | args => 'stack?', | ||
| 371 | opt_spec => [ | ||
| 372 | [ 'stack|s=s' => 'Unlock this stack' ], | ||
| 373 | ], | ||
| 374 | }, | ||
| 375 | unpin => { | ||
| 376 | summary => 'free packages that have been pinned', | ||
| 377 | usage_desc => '%c %o TARGET...', | ||
| 378 | args => 'targets*', | ||
| 379 | opt_spec => [ | ||
| 380 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 381 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 382 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 383 | [ 'stack|s=s' => 'Unpin targets from this stack' ], | ||
| 384 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 385 | ], | ||
| 386 | }, | ||
| 387 | unregister => { | ||
| 388 | summary => 'remove packages from a stack', | ||
| 389 | usage_desc => '%c %o TARGET...', | ||
| 390 | args => 'targets*', | ||
| 391 | opt_spec => [ | ||
| 392 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 393 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 394 | [ 'force' => 'Remove packages even if pinned' ], | ||
| 395 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 396 | [ 'stack|s=s' => 'Remove packages from this stack' ], | ||
| 397 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 398 | ], | ||
| 399 | }, | ||
| 400 | update => { | ||
| 401 | summary => 'update packages to latest versions', | ||
| 402 | usage_desc => '%c %o TARGET...', | ||
| 403 | args => 'targets*', | ||
| 404 | opt_spec => [ | ||
| 405 | [ 'all' => 'Update all packages in the stack' ], | ||
| 406 | [ 'cascade' => 'Always pick latest upstream package' ], | ||
| 407 | [ 'diff-style=s' => 'Set style of diff reports' ], | ||
| 408 | [ 'dry-run' => 'Do not commit any changes' ], | ||
| 409 | [ 'force' => 'Force update, even if pinned' ], | ||
| 410 | [ 'message|m=s' => 'Message to describe the change' ], | ||
| 411 | [ 'no-fail' => 'Do not fail when there is an error' ], | ||
| 412 | [ 'recurse!' => 'Recursively pull prereqs (negatable)' ], | ||
| 413 | [ 'pin' => 'Pin the packages to the stack' ], | ||
| 414 | [ 'roots' => 'Update all root packages in the stack' ], | ||
| 415 | [ 'skip-missing-prerequisite|k=s@' => 'Skip missing prereq (repeatable)' ], | ||
| 416 | [ 'skip-all-missing-prerequisites|K' => 'Skip all missing prereqs' ], | ||
| 417 | [ 'stack|s=s' => 'Update packages in this stack' ], | ||
| 418 | [ 'use-default-message|M' => 'Use the generated message' ], | ||
| 419 | [ 'with-development-prerequisites|wd' => 'Also pull prereqs for development' ], | ||
| 420 | ], | ||
| 421 | }, | ||
| 422 | verify => { | ||
| 423 | summary => 'report archives that are missing', | ||
| 424 | usage_desc => '%c %o', | ||
| 425 | opt_spec => [], | ||
| 426 | }, | ||
| 427 | ); | ||
| 428 | for my $cmd (keys %ret) { | ||
| 429 | $ret{$cmd}{usage_desc} =~ s/^%c\K/ $cmd/; | ||
| 430 | $ret{$cmd}{usage_desc} .= " - $ret{$cmd}{summary}"; | ||
| 431 | } | ||
| 432 | return \%ret; | ||
| 433 | } | ||
| 434 | |||
| 435 | sub command_alias { | ||
| 436 | +{ | ||
| 437 | cp => 'copy', | ||
| 438 | del => 'delete', | ||
| 439 | history => 'log', | ||
| 440 | ls => 'list', | ||
| 441 | mv => 'rename', | ||
| 442 | remove => 'delete', | ||
| 443 | rm => 'delete', | ||
| 444 | stats => 'statistics', | ||
| 445 | up => 'update', | ||
| 446 | }; | ||
| 447 | } | ||
| 448 | |||
| 449 | sub global_opt_spec { | ||
| 450 | return ( | ||
| 451 | [ 'root|r=s' => 'Path to your repository root directory (required)' ], | ||
| 452 | [ 'color|colour!' => '(Currently ignored)' ], | ||
| 453 | [ 'authenticate|auth' => 'Enable authentication'], | ||
| 454 | [ 'passexec|p=s' => 'Command to use get retrieve the password, implies authenticate', { implies => [qw(authenticate)] } ], | ||
| 455 | [ 'username|u=s' => 'Username for server authentication' ], | ||
| 456 | [ 'quiet|q' => 'Only report fatal errors' ], | ||
| 457 | [ 'verbose|v+' => 'More diagnostic output (repeatable)' ], | ||
| 458 | [], | ||
| 459 | [ 'help|?' => 'Print usage message and exit', { shortcircuit => 1 }], | ||
| 460 | ); | ||
| 461 | } | ||
| 462 | |||
| 463 | sub help_summary { | ||
| 464 | my ($class) = @_; | ||
| 465 | |||
| 466 | my %command_info = %{ $class->command_info }; | ||
| 467 | my $len = max(map length, keys %command_info); | ||
| 468 | my $fmt = " %${len}s: %s"; | ||
| 469 | return join '', | ||
| 470 | "Available commands:\n\n", | ||
| 471 | map defined() ? sprintf(" %*s: %s\n", $len, $_, $command_info{$_}{summary}) : '', ( | ||
| 472 | (sort { $command_info{$a}{pos} <=> $command_info{$b}{pos} } | ||
| 473 | grep defined $command_info{$_}{pos}, keys %command_info), | ||
| 474 | undef, | ||
| 475 | (grep !defined $command_info{$_}{pos}, sort keys %command_info), | ||
| 476 | ); | ||
| 477 | } | ||
| 478 | |||
| 479 | sub parse_from_argv { | ||
| 480 | my ($class, $argv) = @_; | ||
| 481 | |||
| 482 | my $orig_cmd = do { | ||
| 483 | local @ARGV = @$argv; | ||
| 484 | () = describe_options('', $class->global_opt_spec, { getopt_conf => ['gnu_getopt', 'pass_through'] }); | ||
| 485 | print($class->help_summary), exit | ||
| 486 | if !@ARGV; # "pintor", "pintor --help", "pintor -r URL", "pintor --username fred", etc | ||
| 487 | $ARGV[0]; | ||
| 488 | }; | ||
| 489 | |||
| 490 | my $cmd = $class->command_alias->{$orig_cmd} // $orig_cmd; | ||
| 491 | my $command_info = $class->command_info; | ||
| 492 | my $info = $command_info->{$cmd} // do { | ||
| 493 | print $class->help_summary; | ||
| 494 | exit 2; | ||
| 495 | }; | ||
| 496 | |||
| 497 | my $usage_desc = $info->{usage_desc}; | ||
| 498 | my @opt_spec = (@{ $info->{opt_spec} }, [], $class->global_opt_spec); | ||
| 499 | |||
| 500 | local @ARGV = @$argv; | ||
| 501 | my ($opt, $usage) = describe_options($usage_desc, @opt_spec, { getopt_conf => ['gnu_getopt'] }); | ||
| 502 | |||
| 503 | die "BUG; cmd=$cmd but not next in argv" | ||
| 504 | if !@ARGV || shift(@ARGV) ne $orig_cmd; | ||
| 505 | |||
| 506 | if ($opt->help || $cmd eq 'help' && !@ARGV) { | ||
| 507 | say $usage->text; | ||
| 508 | exit 0; | ||
| 509 | } | ||
| 510 | elsif ($cmd eq 'commands') { | ||
| 511 | print $class->help_summary; | ||
| 512 | exit 0; | ||
| 513 | } | ||
| 514 | elsif ($cmd eq 'help') { | ||
| 515 | my $exit_status = $class->run_help_command($usage, @ARGV); | ||
| 516 | exit $exit_status; | ||
| 517 | } | ||
| 518 | elsif (my @missing = grep !defined $opt->{$_}, qw(root)) { | ||
| 519 | foreach my $missing (@missing) { | ||
| 520 | next if $missing eq 'root' and $ENV{PINTO_REPOSITORY_ROOT}; | ||
| 521 | $usage->die({ pre_text => "Required options not found: --$missing\n\n" }); | ||
| 522 | } | ||
| 523 | } | ||
| 524 | |||
| 525 | my %parsed = $class->parse_arguments($info, $opt, $usage, @ARGV); | ||
| 526 | my %args = (%$opt, %parsed); | ||
| 527 | my %attrs = (action_name => $cmd); | ||
| 528 | for my $attr (@ATTRS_FROM_OPTIONS) { | ||
| 529 | next if !exists $args{$attr}; | ||
| 530 | $attrs{$attr} = delete $args{$attr}; | ||
| 531 | } | ||
| 532 | |||
| 533 | return %attrs, args => \%args; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub run_help_command { | ||
| 537 | my ($class, $parent_usage, @argv) = @_; | ||
| 538 | |||
| 539 | my $command_info = $class->command_info; | ||
| 540 | (my $usage_text = $parent_usage->text) =~ s/\n\K/\n Global options:\n/; | ||
| 541 | say $usage_text; | ||
| 542 | |||
| 543 | my $exit_status = 0; | ||
| 544 | my $command_alias = $class->command_alias; | ||
| 545 | for my $arg (@argv) { | ||
| 546 | my $cmd = $command_alias->{$arg} // $arg; | ||
| 547 | my $info = $command_info->{$cmd} // do { | ||
| 548 | warn "No command '$cmd' found\n\n"; | ||
| 549 | $exit_status = 2; | ||
| 550 | next; | ||
| 551 | }; | ||
| 552 | my (undef, $usage) = do { | ||
| 553 | local @ARGV; | ||
| 554 | describe_options($info->{usage_desc}, [], @{ $info->{opt_spec} }, { | ||
| 555 | getopt_conf => ['gnu_getopt', 'pass_through'], | ||
| 556 | }); | ||
| 557 | }; | ||
| 558 | say $usage->text; | ||
| 559 | } | ||
| 560 | return $exit_status; | ||
| 561 | } | ||
| 562 | |||
| 563 | sub parse_arguments { | ||
| 564 | my ($class, $info, $opt, $usage, @remaining_argv) = @_; | ||
| 565 | if (!$info->{args}) { | ||
| 566 | $usage->die({ pre_text => "Too many arguments\n\n" }) | ||
| 567 | if @remaining_argv; | ||
| 568 | return; | ||
| 569 | } | ||
| 570 | elsif (ref $info->{args}) { | ||
| 571 | return $info->{args}->($opt, $usage, @remaining_argv); | ||
| 572 | } | ||
| 573 | elsif ($info->{args} =~ /^([a-z]+)\? ([a-z]+)\z/) { | ||
| 574 | my ($optional, $required) = ($1, $2); | ||
| 575 | $usage->die({ pre_text => "You must specify at least one argument\n\n" }) | ||
| 576 | if !@remaining_argv; | ||
| 577 | $usage->die({ pre_text => "You must specify at most two arguments\n\n" }) | ||
| 578 | if @remaining_argv > 2; | ||
| 579 | my %ret = ($required => pop @remaining_argv); | ||
| 580 | $ret{$optional} = $remaining_argv[0] if @remaining_argv; | ||
| 581 | $usage->die({ pre_text => "\u$_ specified as both option and argument\n\n" }) | ||
| 582 | for grep $opt->{$_}, sort keys %ret; | ||
| 583 | return %ret; | ||
| 584 | } | ||
| 585 | else { | ||
| 586 | my @items = split / /, $info->{args}; | ||
| 587 | my $slurpy = $items[-1] =~ /[*+]\z/ ? pop @items : undef; | ||
| 588 | $usage->die({ pre_text => "Not enough arguments\n\n" }) | ||
| 589 | if @remaining_argv < grep !/\?\z/, @items; | ||
| 590 | $usage->die({ pre_text => "Too many arguments\n\n" }) | ||
| 591 | if !defined $slurpy && @remaining_argv > @items; | ||
| 592 | my %ret; | ||
| 593 | for my $arg_name (@items) { | ||
| 594 | $arg_name =~ s/(?<!\?)\z/?/ if exists $opt->{$arg_name}; | ||
| 595 | last if $arg_name =~ s/\?\z// && !@remaining_argv; | ||
| 596 | $usage->die({ pre_text => "No $arg_name argument supplied\n\n" }) | ||
| 597 | if !@remaining_argv; | ||
| 598 | $usage->die({ pre_text => "\u$arg_name supplied as both option and argument\n\n" }) | ||
| 599 | if exists $opt->{$arg_name}; | ||
| 600 | $ret{$arg_name} = shift @remaining_argv; | ||
| 601 | } | ||
| 602 | if (defined $slurpy) { | ||
| 603 | $slurpy =~ s/\*\z//; | ||
| 604 | $usage->die({ pre_text => "Need at least one $slurpy argument\n\n" }) | ||
| 605 | if $slurpy =~ s/\+\z// && !@remaining_argv; | ||
| 606 | $usage->die({ pre_text => "$slurpy supplied as both option and argument\n\n" }) | ||
| 607 | if exists $opt->{$slurpy}; | ||
| 608 | $ret{$slurpy} = [@remaining_argv] if @remaining_argv; | ||
| 609 | } | ||
| 610 | return %ret; | ||
| 611 | } | ||
| 612 | } | ||
| 613 | |||
| 614 | sub new_from_argv { | ||
| 615 | my ($class, $argv, %attrs) = @_; | ||
| 616 | return $class->new({ %attrs, $class->parse_from_argv($argv) }); | ||
| 617 | } | ||
| 618 | |||
| 619 | sub run { | ||
| 620 | my ($self) = @_; | ||
| 621 | my $action_name = $self->action_name; | ||
| 622 | my $remote = $self->make_remote_instance; | ||
| 623 | my $result = $remote->run($action_name, $self->args); | ||
| 624 | exit $result->exit_status; | ||
| 625 | } | ||
| 626 | |||
| 627 | sub make_remote_instance { | ||
| 628 | my ($self) = @_; | ||
| 629 | return Pinto::Remote::SelfContained->new( | ||
| 630 | pairgrep { defined $b } | ||
| 631 | root => $self->root, | ||
| 632 | username => $self->username, | ||
| 633 | password => $self->password, | ||
| 634 | chrome => $self->make_chrome_instance, | ||
| 635 | ); | ||
| 636 | } | ||
| 637 | |||
| 638 | sub make_chrome_instance { | ||
| 639 | my ($self) = @_; | ||
| 640 | return Pinto::Remote::SelfContained::Chrome->new( | ||
| 641 | verbose => $self->verbose, | ||
| 642 | quiet => $self->quiet, | ||
| 643 | ); | ||
| 644 | } | ||
| 645 | |||
| 646 | 1; | ||
| 647 | |||
| 648 | __END__ |