| 1 | package Zing::Cli; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use feature 'say'; | ||
| 9 | |||
| 10 | use lib '.'; | ||
| 11 | |||
| 12 | use registry 'Zing::Types'; | ||
| 13 | use routines; | ||
| 14 | |||
| 15 | use Data::Object::Class; | ||
| 16 | use Data::Object::ClassHas; | ||
| 17 | |||
| 18 | extends 'Data::Object::Cli'; | ||
| 19 | |||
| 20 | our $VERSION = '0.26'; # VERSION | ||
| 21 | |||
| 22 | # ATTRIBUTES | ||
| 23 | |||
| 24 | has app => ( | ||
| 25 | is => 'ro', | ||
| 26 | isa => 'App', | ||
| 27 | new => 1, | ||
| 28 | ); | ||
| 29 | |||
| 30 | fun new_app($self) { | ||
| 31 | require Zing::App; | ||
| 32 | require Zing::Env; | ||
| 33 | |||
| 34 | Zing::App->new( | ||
| 35 | env => Zing::Env->new( | ||
| 36 | $self->opt_appdir ? ( | ||
| 37 | appdir => $self->opt_appdir | ||
| 38 | ) : (), | ||
| 39 | $self->opt_handle ? ( | ||
| 40 | handle => $self->opt_handle | ||
| 41 | ) : (), | ||
| 42 | $self->opt_level ? ( | ||
| 43 | level => $self->opt_level | ||
| 44 | ) : (), | ||
| 45 | $self->opt_piddir ? ( | ||
| 46 | piddir => $self->opt_piddir | ||
| 47 | ) : (), | ||
| 48 | $self->opt_target ? ( | ||
| 49 | target => $self->opt_target | ||
| 50 | ) : (), | ||
| 51 | ) | ||
| 52 | ) | ||
| 53 | } | ||
| 54 | |||
| 55 | has arg_app => ( | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'Maybe[Str]', | ||
| 58 | new => 1, | ||
| 59 | ); | ||
| 60 | |||
| 61 | fun new_arg_app($self) { | ||
| 62 | $self->args->app | ||
| 63 | } | ||
| 64 | |||
| 65 | has cartridge => ( | ||
| 66 | is => 'ro', | ||
| 67 | isa => 'Cartridge', | ||
| 68 | new => 1, | ||
| 69 | ); | ||
| 70 | |||
| 71 | fun new_cartridge($self) { | ||
| 72 | $self->app->cartridge( | ||
| 73 | name => $self->arg_app, | ||
| 74 | $self->opt_appdir ? ( | ||
| 75 | appdir => $self->opt_appdir | ||
| 76 | ) : (), | ||
| 77 | $self->opt_libdir ? ( | ||
| 78 | libdir => $self->opt_libdir | ||
| 79 | ) : (), | ||
| 80 | $self->opt_piddir ? ( | ||
| 81 | piddir => $self->opt_piddir | ||
| 82 | ) : (), | ||
| 83 | ) | ||
| 84 | } | ||
| 85 | |||
| 86 | has daemon => ( | ||
| 87 | is => 'ro', | ||
| 88 | isa => 'Daemon', | ||
| 89 | new => 1, | ||
| 90 | ); | ||
| 91 | |||
| 92 | fun new_daemon($self) { | ||
| 93 | $self->app->daemon( | ||
| 94 | cartridge => $self->cartridge, | ||
| 95 | $self->opt_backlog ? ( | ||
| 96 | log_reset => $self->opt_backlog | ||
| 97 | ) : (), | ||
| 98 | $self->opt_level ? ( | ||
| 99 | log_level => $self->opt_level | ||
| 100 | ) : (), | ||
| 101 | $self->opt_process ? ( | ||
| 102 | log_filter_from => $self->opt_process | ||
| 103 | ) : (), | ||
| 104 | $self->opt_search ? ( | ||
| 105 | log_filter_queries => $self->$self->opt_search | ||
| 106 | ) : (), | ||
| 107 | $self->opt_tag ? ( | ||
| 108 | log_filter_tag => $self->opt_tag | ||
| 109 | ) : (), | ||
| 110 | $self->opt_verbose ? ( | ||
| 111 | log_verbose => $self->opt_verbose | ||
| 112 | ) : (), | ||
| 113 | ) | ||
| 114 | } | ||
| 115 | |||
| 116 | has opt_appdir => ( | ||
| 117 | is => 'ro', | ||
| 118 | isa => 'Maybe[Str]', | ||
| 119 | new => 1, | ||
| 120 | ); | ||
| 121 | |||
| 122 | fun new_opt_appdir($self) { | ||
| 123 | $self->opts->appdir | ||
| 124 | } | ||
| 125 | |||
| 126 | has opt_backlog => ( | ||
| 127 | is => 'ro', | ||
| 128 | isa => 'Maybe[Bool]', | ||
| 129 | new => 1, | ||
| 130 | ); | ||
| 131 | |||
| 132 | fun new_opt_backlog($self) { | ||
| 133 | $self->opts->backlog | ||
| 134 | } | ||
| 135 | |||
| 136 | has opt_target => ( | ||
| 137 | is => 'ro', | ||
| 138 | isa => 'Maybe[Str]', | ||
| 139 | new => 1, | ||
| 140 | ); | ||
| 141 | |||
| 142 | fun new_opt_target($self) { | ||
| 143 | $self->opts->target | ||
| 144 | } | ||
| 145 | |||
| 146 | has opt_handle => ( | ||
| 147 | is => 'ro', | ||
| 148 | isa => 'Maybe[Str]', | ||
| 149 | new => 1, | ||
| 150 | ); | ||
| 151 | |||
| 152 | fun new_opt_handle($self) { | ||
| 153 | $self->opts->handle | ||
| 154 | } | ||
| 155 | |||
| 156 | has opt_level => ( | ||
| 157 | is => 'ro', | ||
| 158 | isa => 'Maybe[Str]', | ||
| 159 | new => 1, | ||
| 160 | ); | ||
| 161 | |||
| 162 | fun new_opt_level($self) { | ||
| 163 | $self->opts->level | ||
| 164 | } | ||
| 165 | |||
| 166 | has opt_libdir => ( | ||
| 167 | is => 'ro', | ||
| 168 | isa => 'Maybe[ArrayRef[Str]]', | ||
| 169 | new => 1, | ||
| 170 | ); | ||
| 171 | |||
| 172 | fun new_opt_libdir($self) { | ||
| 173 | $self->opts->libdir | ||
| 174 | } | ||
| 175 | |||
| 176 | has opt_package => ( | ||
| 177 | is => 'ro', | ||
| 178 | isa => 'Maybe[Str]', | ||
| 179 | new => 1, | ||
| 180 | ); | ||
| 181 | |||
| 182 | fun new_opt_package($self) { | ||
| 183 | $self->opts->package | ||
| 184 | } | ||
| 185 | |||
| 186 | has opt_piddir => ( | ||
| 187 | is => 'ro', | ||
| 188 | isa => 'Maybe[Str]', | ||
| 189 | new => 1, | ||
| 190 | ); | ||
| 191 | |||
| 192 | fun new_opt_piddir($self) { | ||
| 193 | $self->opts->piddir | ||
| 194 | } | ||
| 195 | |||
| 196 | has opt_process => ( | ||
| 197 | is => 'ro', | ||
| 198 | isa => 'Maybe[Str]', | ||
| 199 | new => 1, | ||
| 200 | ); | ||
| 201 | |||
| 202 | fun new_opt_process($self) { | ||
| 203 | $self->opts->process | ||
| 204 | } | ||
| 205 | |||
| 206 | has opt_search => ( | ||
| 207 | is => 'ro', | ||
| 208 | isa => 'Maybe[ArrayRef[Str]]', | ||
| 209 | new => 1, | ||
| 210 | ); | ||
| 211 | |||
| 212 | fun new_opt_search($self) { | ||
| 213 | $self->opts->search | ||
| 214 | } | ||
| 215 | |||
| 216 | has opt_tag => ( | ||
| 217 | is => 'ro', | ||
| 218 | isa => 'Maybe[Str]', | ||
| 219 | new => 1, | ||
| 220 | ); | ||
| 221 | |||
| 222 | fun new_opt_tag($self) { | ||
| 223 | $self->opts->tag | ||
| 224 | } | ||
| 225 | |||
| 226 | has opt_verbose => ( | ||
| 227 | is => 'ro', | ||
| 228 | isa => 'Maybe[Bool]', | ||
| 229 | new => 1, | ||
| 230 | ); | ||
| 231 | |||
| 232 | fun new_opt_verbose($self) { | ||
| 233 | $self->opts->verbose | ||
| 234 | } | ||
| 235 | |||
| 236 | # USAGE | ||
| 237 | |||
| 238 | our $name = 'zing <{command}> [<{app}>] [options]'; | ||
| 239 | |||
| 240 | # CONFIGURATION | ||
| 241 | |||
| 242 | fun auto() { | ||
| 243 | { | ||
| 244 | install => 'handle_install', | ||
| 245 | logs => 'handle_logs', | ||
| 246 | monitor => 'handle_monitor', | ||
| 247 | pid => 'handle_pid', | ||
| 248 | restart => 'handle_restart', | ||
| 249 | start => 'handle_start', | ||
| 250 | stop => 'handle_stop', | ||
| 251 | update => 'handle_update', | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | fun subs() { | ||
| 256 | { | ||
| 257 | install => 'Create an application cartridge', | ||
| 258 | logs => 'Tap logs and output to STDOUT', | ||
| 259 | monitor => 'Monitor the specified application (start if not started)', | ||
| 260 | pid => 'Display an application process ID', | ||
| 261 | restart => 'Restart the specified application', | ||
| 262 | start => 'Start the specified application', | ||
| 263 | stop => 'Stop the specified application', | ||
| 264 | update => 'Hot-reload application processes', | ||
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | fun spec() { | ||
| 269 | { | ||
| 270 | appdir => { | ||
| 271 | desc => 'Directory of the app file', | ||
| 272 | type => 'string', | ||
| 273 | flag => 'a', | ||
| 274 | }, | ||
| 275 | backlog => { | ||
| 276 | desc => 'Produce log output using the backlog', | ||
| 277 | type => 'flag', | ||
| 278 | flag => 'b', | ||
| 279 | }, | ||
| 280 | handle => { | ||
| 281 | desc => 'Provide a handle (namespace)', | ||
| 282 | type => 'string', | ||
| 283 | flag => 'h', | ||
| 284 | }, | ||
| 285 | process => { | ||
| 286 | desc => 'Reduce log output by process name', | ||
| 287 | type => 'string', | ||
| 288 | flag => 'p', | ||
| 289 | }, | ||
| 290 | level => { | ||
| 291 | desc => 'Reduce log output by log-level', | ||
| 292 | type => 'string', | ||
| 293 | flag => 'l', | ||
| 294 | }, | ||
| 295 | libdir => { | ||
| 296 | desc => 'Directory for @INC', | ||
| 297 | type => 'string', | ||
| 298 | flag => 'i', | ||
| 299 | args => '@', | ||
| 300 | }, | ||
| 301 | package => { | ||
| 302 | desc => 'Provide a process package name', | ||
| 303 | type => 'string', | ||
| 304 | flag => 'n', | ||
| 305 | }, | ||
| 306 | piddir => { | ||
| 307 | desc => 'Directory for the pid file', | ||
| 308 | type => 'string', | ||
| 309 | flag => 'd', | ||
| 310 | }, | ||
| 311 | search => { | ||
| 312 | desc => 'Reduce log output by search string', | ||
| 313 | type => 'string', | ||
| 314 | flag => 's', | ||
| 315 | args => '@', | ||
| 316 | }, | ||
| 317 | target => { | ||
| 318 | desc => 'Provide a target (package)', | ||
| 319 | type => 'string', | ||
| 320 | flag => 'r', | ||
| 321 | }, | ||
| 322 | tag => { | ||
| 323 | desc => 'Reduce log output by process tag', | ||
| 324 | type => 'string', | ||
| 325 | flag => 't', | ||
| 326 | }, | ||
| 327 | verbose => { | ||
| 328 | desc => 'Produce verbose log output', | ||
| 329 | type => 'flag', | ||
| 330 | flag => 'v', | ||
| 331 | }, | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | # METHODS | ||
| 336 | |||
| 337 | method handle_install() { | ||
| 338 | if (!$self->arg_app) { | ||
| 339 | $self->output('error: app required'); | ||
| 340 | exit(1); | ||
| 341 | } | ||
| 342 | if (!$self->opt_package) { | ||
| 343 | $self->output('error: package required'); | ||
| 344 | exit(1); | ||
| 345 | } | ||
| 346 | else { | ||
| 347 | my $space = Data::Object::Space->new($self->opt_package); | ||
| 348 | my %seen = map {$_, 1} @INC; | ||
| 349 | for my $dir (@{$self->opt_libdir}) { | ||
| 350 | push @INC, $dir if !$seen{$dir}++; | ||
| 351 | } | ||
| 352 | if (!$space->load->can('install')) { | ||
| 353 | $self->output('error: uninstallable'); | ||
| 354 | exit(1); | ||
| 355 | } | ||
| 356 | else { | ||
| 357 | $self->output('installing ...'); | ||
| 358 | $self->cartridge->install($space->build->install); | ||
| 359 | exit(0); | ||
| 360 | } | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | method handle_logs() { | ||
| 365 | if (!$self->arg_app) { | ||
| 366 | $self->output('error: app required'); | ||
| 367 | exit(1); | ||
| 368 | } | ||
| 369 | else { | ||
| 370 | $self->daemon->logs(fun($line) { | ||
| 371 | $self->output($line); | ||
| 372 | }); | ||
| 373 | exit(0); | ||
| 374 | } | ||
| 375 | } | ||
| 376 | |||
| 377 | method handle_monitor() { | ||
| 378 | if (!$self->arg_app) { | ||
| 379 | $self->output('error: app required'); | ||
| 380 | exit(1); | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | $self->output('monitoring ...'); | ||
| 384 | } | ||
| 385 | if (!-e $self->cartridge->pidfile) { | ||
| 386 | $self->daemon->start; | ||
| 387 | } | ||
| 388 | $self->handle_logs; | ||
| 389 | exit(0); | ||
| 390 | } | ||
| 391 | |||
| 392 | method handle_pid() { | ||
| 393 | if (!$self->arg_app) { | ||
| 394 | $self->output('error: app required'); | ||
| 395 | exit(1); | ||
| 396 | } | ||
| 397 | else { | ||
| 398 | if (my $pid = $self->cartridge->pid) { | ||
| 399 | $self->output($pid); | ||
| 400 | exit(0); | ||
| 401 | } | ||
| 402 | else { | ||
| 403 | $self->output('error: no pid'); | ||
| 404 | exit(1); | ||
| 405 | } | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | method handle_restart() { | ||
| 410 | if (!$self->arg_app) { | ||
| 411 | $self->output('error: app required'); | ||
| 412 | exit(1); | ||
| 413 | } | ||
| 414 | elsif (!-f $self->cartridge->appfile) { | ||
| 415 | $self->output('error: app not found'); | ||
| 416 | exit(1); | ||
| 417 | } | ||
| 418 | else { | ||
| 419 | $self->output('restarting ...'); | ||
| 420 | } | ||
| 421 | exit(!$self->daemon->restart); | ||
| 422 | } | ||
| 423 | |||
| 424 | method handle_start() { | ||
| 425 | if (!$self->arg_app) { | ||
| 426 | $self->output('error: app required'); | ||
| 427 | exit(1); | ||
| 428 | } | ||
| 429 | elsif (!-f $self->cartridge->appfile) { | ||
| 430 | $self->output('error: app not found'); | ||
| 431 | exit(1); | ||
| 432 | } | ||
| 433 | else { | ||
| 434 | $self->output('starting ...'); | ||
| 435 | } | ||
| 436 | exit(!$self->daemon->start); | ||
| 437 | } | ||
| 438 | |||
| 439 | method handle_stop() { | ||
| 440 | if (!$self->arg_app) { | ||
| 441 | $self->output('error: app required'); | ||
| 442 | exit(1); | ||
| 443 | } | ||
| 444 | elsif (!-f $self->cartridge->appfile) { | ||
| 445 | $self->output('error: app not found'); | ||
| 446 | exit(1); | ||
| 447 | } | ||
| 448 | else { | ||
| 449 | $self->output('stopping ...'); | ||
| 450 | } | ||
| 451 | exit(!$self->daemon->stop); | ||
| 452 | } | ||
| 453 | |||
| 454 | method handle_update() { | ||
| 455 | if (!$self->arg_app) { | ||
| 456 | $self->output('error: app required'); | ||
| 457 | exit(1); | ||
| 458 | } | ||
| 459 | elsif (!-f $self->cartridge->appfile) { | ||
| 460 | $self->output('error: app not found'); | ||
| 461 | exit(1); | ||
| 462 | } | ||
| 463 | else { | ||
| 464 | $self->output('updating ...'); | ||
| 465 | } | ||
| 466 | exit(!$self->daemon->update); | ||
| 467 | } | ||
| 468 | |||
| 469 | method output(Str @args) { | ||
| 470 | say $_ for @args; return $self; | ||
| 471 | } | ||
| 472 | |||
| 473 | 1; | ||
| 474 | |||
| 475 | |||
| 476 | =encoding utf8 | ||
| 477 | |||
| 478 | =head1 NAME | ||
| 479 | |||
| 480 | Zing::Cli - Command-line Interface | ||
| 481 | |||
| 482 | =cut | ||
| 483 | |||
| 484 | =head1 ABSTRACT | ||
| 485 | |||
| 486 | Command-line Process Management | ||
| 487 | |||
| 488 | =cut | ||
| 489 | |||
| 490 | =head1 SYNOPSIS | ||
| 491 | |||
| 492 | use Zing::Cli; | ||
| 493 | |||
| 494 | my $cli = Zing::Cli->new; | ||
| 495 | |||
| 496 | # $cli->handle('main'); | ||
| 497 | |||
| 498 | =cut | ||
| 499 | |||
| 500 | =head1 DESCRIPTION | ||
| 501 | |||
| 502 | This package provides a command-line interface for managing L<Zing> | ||
| 503 | applications. See the L<zing> documentation for interface arguments and | ||
| 504 | options. | ||
| 505 | |||
| 506 | =cut | ||
| 507 | |||
| 508 | =head1 INHERITS | ||
| 509 | |||
| 510 | This package inherits behaviors from: | ||
| 511 | |||
| 512 | L<Data::Object::Cli> | ||
| 513 | |||
| 514 | =cut | ||
| 515 | |||
| 516 | =head1 LIBRARIES | ||
| 517 | |||
| 518 | This package uses type constraints from: | ||
| 519 | |||
| 520 | L<Zing::Types> | ||
| 521 | |||
| 522 | =cut | ||
| 523 | |||
| 524 | =head1 METHODS | ||
| 525 | |||
| 526 | This package implements the following methods: | ||
| 527 | |||
| 528 | =cut | ||
| 529 | |||
| 530 | =head2 main | ||
| 531 | |||
| 532 | main() : Any | ||
| 533 | |||
| 534 | The main method executes the command-line interface and displays help text or | ||
| 535 | launches applications. | ||
| 536 | |||
| 537 | =over 4 | ||
| 538 | |||
| 539 | =item main example #1 | ||
| 540 | |||
| 541 | # given: synopsis | ||
| 542 | |||
| 543 | # e.g. | ||
| 544 | # zing start once -I t/lib -a t/app | ||
| 545 | # pass | ||
| 546 | |||
| 547 | $cli->handle('main'); | ||
| 548 | |||
| 549 | =back | ||
| 550 | |||
| 551 | =over 4 | ||
| 552 | |||
| 553 | =item main example #2 | ||
| 554 | |||
| 555 | # given: synopsis | ||
| 556 | |||
| 557 | # e.g. | ||
| 558 | # zing start unce -I t/lib -a t/app | ||
| 559 | # fail (not exist) | ||
| 560 | |||
| 561 | $cli->handle('main'); | ||
| 562 | |||
| 563 | =back | ||
| 564 | |||
| 565 | =cut | ||
| 566 | |||
| 567 | =head1 AUTHOR | ||
| 568 | |||
| 569 | Al Newkirk, C<awncorp@cpan.org> | ||
| 570 | |||
| 571 | =head1 LICENSE | ||
| 572 | |||
| 573 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 574 | |||
| 575 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 576 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 577 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 578 | |||
| 579 | =head1 PROJECT | ||
| 580 | |||
| 581 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 582 | |||
| 583 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 584 | |||
| 585 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 586 | |||
| 587 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 588 | |||
| 589 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 590 | |||
| 591 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 592 | |||
| 593 | =cut | ||
| 594 | __DATA__ |