Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predefined output formats - ELK, cron. New demo and docs. #15

Merged
merged 53 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
276449a
Predefined output formats
Jul 31, 2023
d6799bf
Predefined output formats
Jul 31, 2023
6d1aadf
Predefined output formats
Aug 3, 2023
8c54a84
Predefined output formats
Aug 3, 2023
e147e94
Predefined output formats
Aug 3, 2023
7f9c5b8
Predefined output formats
Aug 5, 2023
d314941
Predefined output formats
Aug 5, 2023
0aea49f
Predefined output formats
Aug 5, 2023
cfd1e83
Predefined output formats
Aug 5, 2023
f693172
Predefined output formats
Aug 5, 2023
d500e0f
Predefined output formats
Aug 5, 2023
103f312
Predefined output formats
Aug 5, 2023
2eac1b4
Predefined output formats
Aug 5, 2023
f8cde22
Predefined output formats
Aug 5, 2023
dbe7761
Predefined output formats
Aug 5, 2023
158fb26
Predefined output formats
Aug 5, 2023
6e72689
Predefined output formats
Aug 5, 2023
81e2785
Predefined output formats
Aug 5, 2023
83aaa8d
Predefined output formats
Aug 5, 2023
37f7d0c
Predefined output formats
Aug 6, 2023
e8c57f6
Predefined output formats
Aug 6, 2023
1d93e12
Predefined output formats
Aug 6, 2023
36ced06
Predefined output formats
Aug 6, 2023
b120721
Predefined output formats
Aug 6, 2023
90597da
Predefined output formats
Aug 6, 2023
257d459
Predefined output formats
Aug 6, 2023
c3a98c0
Predefined output formats
Aug 6, 2023
9983c62
Predefined output formats
Aug 6, 2023
2ee6855
Predefined output formats
Aug 6, 2023
a24ea0e
Predefined output formats
Aug 6, 2023
6d741bd
Predefined output formats
Aug 6, 2023
9e1ce78
Predefined output formats
Aug 6, 2023
3830c85
Predefined output formats
Aug 7, 2023
5dc6a3a
Predefined output formats
Aug 7, 2023
cff2b01
Predefined output formats
Aug 8, 2023
96c3084
Predefined output formats
Aug 9, 2023
17b8215
Predefined output formats
Aug 9, 2023
7ee1120
Predefined output formats
Aug 10, 2023
23c298c
Predefined output formats
Aug 10, 2023
c1f28c3
Predefined output formats
Aug 10, 2023
e5d9c9b
Predefined output formats
Aug 10, 2023
1d54c16
Predefined output formats
Aug 10, 2023
8f1fd11
Predefined output formats
Aug 10, 2023
78e3bba
Predefined output formats
Aug 10, 2023
60a5e8a
Predefined output formats
Aug 10, 2023
7a0beda
Predefined output formats
Aug 10, 2023
6454071
Predefined output formats
Aug 10, 2023
acbcd14
Predefined output formats
Aug 10, 2023
6ed8815
Predefined output formats
Aug 10, 2023
40f8a84
Predefined output formats
Aug 10, 2023
ba09777
Predefined output formats
Aug 10, 2023
5cd6264
Predefined output formats
Aug 10, 2023
0358e1e
Predefined output formats
Aug 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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