Skip to content

Commit

Permalink
Predefined output formats - ELK, cron. New demo and docs. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis authored Aug 10, 2023
1 parent 7d061ae commit ddf37e7
Show file tree
Hide file tree
Showing 66 changed files with 4,342 additions and 1,925 deletions.
Binary file removed .github/assets/ExamplesOutput-vvv.png
Binary file not shown.
Binary file removed .github/assets/ExamplesProfile--timestamp.png
Binary file not shown.
Binary file added .github/assets/helpers.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/logs-cron.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/logs-logstash-exception.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/logs-simple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/output-full-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/output-styles.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added .github/assets/progress-default-example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .github/assets/progress-full-example.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions .phan.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
'vendor/jbzoo/event',

'vendor/symfony/console',
'vendor/symfony/console/Command',
'vendor/symfony/process',
'vendor/bluepsyduck/symfony-process-manager/src',
'vendor/psr/log',
'vendor/monolog/monolog',
],
]);
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ test-all: ##@Project Run all project tests at once
@make test
@make codestyle
@make codestyle PATH_SRC=./demo


toc: ##@Project Generate table of contents
@echo "Generate table of contents"
@gh-md-toc --insert --no-backup --skip-header ./README.md
428 changes: 312 additions & 116 deletions README.md

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"cli",
"command-line",
"console-application",
"cron",
"crontab",
"elk",
"elk-stack",
"elastic",
"logstash",
"symfony",
"process",
"symfony-console",
Expand All @@ -30,13 +36,14 @@
"require" : {
"php" : "^8.1",

"jbzoo/utils" : "^7.0",
"jbzoo/utils" : "^7.1",
"jbzoo/event" : "^7.0",

"symfony/process" : ">=5.4",
"symfony/console" : ">=5.4",
"symfony/lock" : ">=5.4",
"bluepsyduck/symfony-process-manager" : ">=1.3.3"
"bluepsyduck/symfony-process-manager" : ">=1.3.3",
"monolog/monolog" : "^3.4"
},

"require-dev" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@

use function JBZoo\Cli\cli;

class ExamplesHelpers extends CliCommand
class DemoHelpers extends CliCommand
{
protected function configure(): void
{
$this
->setName('examples:helpers')
->setName('helpers')
->setDescription('Examples of new CLI helpers');

parent::configure();
}

/**
* {@inheritDoc}
*/
protected function executeAction(): int
{
$yourName = $this->ask("What's your name?", 'idk');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,14 @@
use JBZoo\Cli\CliCommand;
use Symfony\Component\Console\Input\InputOption;

class ExamplesOptionsStrictTypes extends CliCommand
class DemoOptionsStrictTypes extends CliCommand
{
protected function configure(): void
{
$this
->setName('examples:options-strict-types')
->setName('options-strict-types')
->setDescription('Show description of command.')
->setHelp(
"Full description and usage of command.\n" .
'You can use severla lines.',
)
->setHelp("Full description and usage of command.\nYou can use severla lines.")

// None
->addOption('opt', 'o', InputOption::VALUE_NONE, 'Just a boolean flag')
Expand Down Expand Up @@ -88,9 +85,6 @@ protected function configure(): void
parent::configure();
}

/**
* {@inheritDoc}
*/
protected function executeAction(): int
{
// //////////////////////////////////////// Just a boolean flag
Expand Down Expand Up @@ -157,7 +151,7 @@ protected function executeAction(): int
// ./my-app examples:agruments -aQwerty -aAsd
$this->getOpt('opt-array-req-default'); // 'Asd'

$input = $this->helper->getInput();
$input = $this->outputMode->getInput();
// //////////////////////////////////////// Arguments
// ./my-app examples:agruments
$input->getArgument('arg-req'); // null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,18 @@

use function JBZoo\Cli\cli;

class ExamplesOutput extends CliCommand
class DemoOutput extends CliCommand
{
protected function configure(): void
{
$this
->setName('examples:output')
->setName('output')
->setDescription('Examples of output and error reporting')
->addOption('throw-custom-exception', 'e', InputOption::VALUE_NONE, 'Throw the exception');

parent::configure();
}

/**
* {@inheritDoc}
*/
protected function executeAction(): int
{
$code = static fn (string $flag): string => "<black-b>`cli(\$text, {$flag})`</black-b>";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,17 @@

use function JBZoo\Cli\cli;

class ExamplesProfile extends CliCommand
class DemoProfile extends CliCommand
{
protected function configure(): void
{
$this
->setName('examples:profile')
->setName('profile')
->setDescription('Examples of memory and time profiling');

parent::configure();
}

/**
* {@inheritDoc}
*/
protected function executeAction(): int
{
cli('Start cycles');
Expand Down
158 changes: 158 additions & 0 deletions demo/Commands/DemoProgressBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

/**
* JBZoo Toolbox - Cli.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Cli
*/

declare(strict_types=1);

namespace DemoApp\Commands;

use Faker\Factory;
use JBZoo\Cli\CliCommand;
use JBZoo\Cli\ProgressBars\ExceptionBreak;
use JBZoo\Utils\Slug;
use Symfony\Component\Console\Input\InputOption;

class DemoProgressBar extends CliCommand
{
private const CASE_SIMPLE = 'simple';
private const CASE_MESSAGES = 'messages';
private const CASE_ARRAY = 'array';
private const CASE_BREAK = 'break';
private const CASE_EXCEPTION = 'exception';
private const CASE_EXCEPTION_LIST = 'exception-list';
private const CASE_MILLION = 'million';

protected function configure(): void
{
$this
->setName('progress-bar')
->setDescription('Examples of progress bar')
->addOption('case', 'c', InputOption::VALUE_REQUIRED, 'Case name.')
->addOption('no-sleep', 't', InputOption::VALUE_NONE, 'Disable sleep timer.');

parent::configure();
}

protected function executeAction(): int
{
$listOfUsers = $this->prepareListOfDemoUsers();
$caseName = $this->getOptString('case', '', [
self::CASE_SIMPLE,
self::CASE_MESSAGES,
self::CASE_ARRAY,
self::CASE_BREAK,
self::CASE_EXCEPTION,
self::CASE_EXCEPTION_LIST,
self::CASE_MILLION,
]);

// Just 5 simple steps /////////////////////////////////////////////////////////////////////////////////////////
if ($caseName === self::CASE_SIMPLE) {
$this->progressBar(5, function (): void {
$this->sleep();
});
}

// Simple progress with custom message based on callback arguments /////////////////////////////////////////////
if ($caseName === self::CASE_MESSAGES) {
$this->progressBar($listOfUsers, function ($value, $key, $step) {
$this->sleep();

return "<c>Callback Args</c> \$value=<i>{$value}</i>, \$key=<i>{$key}</i>, \$step=<i>{$step}</i>";
}, 'Custom messages based on callback arguments');
}

// Use the associated array as a data source ///////////////////////////////////////////////////////////////////
if ($caseName === self::CASE_ARRAY) {
dump($listOfUsers);
$this->progressBar($listOfUsers, function ($value, $key, $step) {
$this->sleep();

return "<c>Callback Args</c> \$value=<i>{$value}</i>, \$key=<i>{$key}</i>, \$step=<i>{$step}</i>";
}, 'Handling associated array as a data source');
}

// Exit the loop programmatically //////////////////////////////////////////////////////////////////////////////
if ($caseName === self::CASE_BREAK) {
dump($listOfUsers);
$this->progressBar($listOfUsers, function ($value, $key, $step) {
$this->sleep();
if ($step === 3) {
throw new ExceptionBreak("Something went wrong with \$value={$value}");
}

return "<c>Callback Args</c> \$value=<i>{$value}</i>, \$key=<i>{$key}</i>, \$step=<i>{$step}</i>";
}, 'Exit the loop programmatically');
}

// Exception handling //////////////////////////////////////////////////////////////////////////////////////////
if ($caseName === self::CASE_EXCEPTION) {
$this->progressBar(5, function ($value) {
$this->sleep();
if ($value === 1) {
throw new Exception("Something went really wrong on step #{$value}");
}

return "\$value=<i>{$value}</i>";
}, 'Exception handling', false);
}

// Ignoring and collecting exceptions. Throw an error only at the end. /////////////////////////////////////////
if ($caseName === self::CASE_EXCEPTION_LIST) {
$this->progressBar(10, function ($value): void {
$this->sleep();
if ($value % 3 === 0) {
throw new Exception("Something went really wrong on step #{$value}");
}
}, 'Ignoring and collecting exceptions. Throw them only at the end.', true);
}

// 1 000 000 Items Benchmark ///////////////////////////////////////////////////////////////////////////////////
if ($caseName === self::CASE_MILLION) {
$this->progressBar(
1_000_000,
static fn ($value, $key, $step) => '<c>Callback Args</c> ' .
"\$value=<i>{$value}</i>, \$key=<i>{$key}</i>, \$step=<i>{$step}</i>",
'1 000 000 items benchmark',
);
}

return self::SUCCESS;
}

private function prepareListOfDemoUsers(): array
{
$faker = Factory::create();

$users = [];

for ($i = 0; $i < 5; $i++) {
$firstName = $faker->firstName;
$lastName = $faker->lastName;
$email = Slug::filter($firstName) . '@site.com';

$users[$email] = $firstName . ' ' . $lastName;
}

return $users;
}

private function sleep(): void
{
if ($this->getOptBool('no-sleep')) {
return;
}

\usleep(\random_int(500, 1200) * 1000);
}
}
5 changes: 1 addition & 4 deletions demo/Commands/Simple.php → demo/Commands/DemoSimple.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use JBZoo\Cli\CliCommand;

class Simple extends CliCommand
class DemoSimple extends CliCommand
{
protected function configure(): void
{
Expand All @@ -28,9 +28,6 @@ protected function configure(): void
parent::configure();
}

/**
* {@inheritDoc}
*/
protected function executeAction(): int
{
// Your code here
Expand Down
Loading

0 comments on commit ddf37e7

Please sign in to comment.