Skip to content

Commit a9c9c3c

Browse files
committed
refactor: enhance command output handling and improve user feedback
1 parent af17ad7 commit a9c9c3c

31 files changed

+306
-221
lines changed

resources/views/terminal/fail.blade.php

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
2-
31
<div class="mx-2 my-1">
42
@foreach($items as $item)
53
<div class="flex space-x-1">
6-
<span class="text-yellow">{{$item['handler']}}</span>
7-
<span class="text-gray">{{$item['pattern']}}</span>
8-
<span class="flex-1 content-repeat-['.']"></span>
4+
<span class="text-yellow w-{{$handlerWidth}}">{{$item['handler']}}</span>
5+
@if($item['pattern'])
6+
<span class="text-gray">{{$item['pattern']}}</span>
7+
@endif
8+
<span class="flex-1 text-blue content-repeat-['.']"></span>
99
<span class="text-blue">{{$item['callable']}}</span>
1010
</div>
1111
@endforeach
12+
<div class="text-right font-bold text-blue mt-1 w-full">
13+
Showing [{{$items->count()}}] handlers
14+
</div>
1215
</div>

resources/views/terminal/success.blade.php

Lines changed: 0 additions & 1 deletion
This file was deleted.

resources/views/terminal/table.blade.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/Console/BaseMakeCommand.php

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,44 @@
33
namespace Nutgram\Laravel\Console;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Contracts\Console\PromptsForMissingInput;
67
use Illuminate\Support\Facades\File;
78
use Illuminate\Support\Str;
89
use RuntimeException;
910

10-
abstract class BaseMakeCommand extends Command
11+
abstract class BaseMakeCommand extends Command implements PromptsForMissingInput
1112
{
1213
protected $signature = 'nutgram:make:? {name : ? name}';
1314

1415
protected $description = 'Create a new Nutgram ?';
1516

1617
public function handle(): int
1718
{
18-
//get the file name
19-
$name = $this->argument('name');
20-
21-
//get stub content
22-
$stub = $this->getStubContent($this->getStubPath(), $this->getStubVariables());
23-
24-
//get destination path
2519
$path = sprintf("%s%s%s%s%s.php",
2620
config('nutgram.namespace'),
2721
DIRECTORY_SEPARATOR,
2822
$this->getSubDirName(),
2923
DIRECTORY_SEPARATOR,
30-
$name
24+
$this->argument('name'),
3125
);
3226

33-
//create directory if it doesn't exist
3427
$this->makeDirectory($path);
3528

36-
//check if file already exists
3729
if (File::exists($path)) {
38-
$relativePath = Str::after($path, base_path());
39-
$this->error(sprintf("%s already exists.", $relativePath));
40-
return 1;
30+
$this->outputComponents()->error(sprintf("%s already exists.", Str::after($path, base_path())));
31+
32+
return self::FAILURE;
4133
}
4234

43-
//write stub to file
44-
File::put($path, $stub);
35+
File::put($path, $this->getStubContent($this->getStubPath(), $this->getStubVariables()));
36+
37+
$this->outputComponents()->success('Nutgram '.Str::singular($this->getSubDirName()).' created successfully.');
4538

46-
$this->info('Nutgram '.Str::singular($this->getSubDirName()).' created successfully.');
47-
return 0;
39+
return self::SUCCESS;
4840
}
4941

5042
/**
51-
* Return the sub directory name
43+
* Return the subdirectory name
5244
* @return string
5345
*/
5446
abstract protected function getSubDirName(): string;
@@ -61,8 +53,7 @@ abstract protected function getStubPath(): string;
6153

6254
/**
6355
* Map the stub variables present in stub to its value
64-
*
65-
* @return array
56+
* @return array<string, string>
6657
*/
6758
protected function getStubVariables(): array
6859
{
@@ -78,7 +69,7 @@ protected function getStubVariables(): array
7869
/**
7970
* Replace the stub variables with the desire value
8071
* @param string $path
81-
* @param array $variables
72+
* @param array<string, string> $variables
8273
* @return string
8374
*/
8475
protected function getStubContent(string $path, array $variables = []): string
@@ -91,7 +82,7 @@ protected function getStubContent(string $path, array $variables = []): string
9182
}
9283

9384
/**
94-
* Build the directory for the class if necessary.
85+
* Build the directory for the class if necessary
9586
* @param string $path
9687
* @return void
9788
*/
@@ -109,9 +100,7 @@ protected function makeDirectory(string $path): void
109100
}
110101

111102
/**
112-
* Get the full namespace for a given class, without the class name.
113-
*
114-
*
103+
* Get the full namespace for a given class, without the class name
115104
* @param string $name
116105
* @return string
117106
*/
@@ -128,8 +117,7 @@ protected function getNamespace(string $name): string
128117

129118

130119
/**
131-
* remove duplicated slashes
132-
*
120+
* Remove duplicated slashes
133121
* @param string $name
134122
* @param string $replacement
135123
* @return string
@@ -140,8 +128,7 @@ protected function slashesTrim(string $name, string $replacement = '\\'): string
140128
}
141129

142130
/**
143-
* returns namespace from full class name
144-
*
131+
* Returns namespace from the full class name
145132
* @param string $name
146133
* @return string
147134
*/

src/Console/HookInfoCommand.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
use JsonException;
88
use SergiX44\Nutgram\Nutgram;
99
use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;
10-
use function Termwind\{render};
11-
10+
use function Laravel\Prompts\table;
1211

1312
class HookInfoCommand extends Command
1413
{
@@ -23,37 +22,49 @@ class HookInfoCommand extends Command
2322
*/
2423
public function handle(Nutgram $bot): int
2524
{
25+
$this->newLine();
26+
2627
$webhookInfo = $bot->getWebhookInfo();
2728

2829
if ($webhookInfo === null) {
29-
render(view('terminal::fail', ['value' => 'Unable to get webhook info']));
30-
return 1;
30+
$this->outputComponents()->error('Unable to get webhook info');
31+
return self::FAILURE;
3132
}
3233

33-
$lastErrorDate = null;
34-
if ($webhookInfo->last_error_date !== null) {
35-
$lastErrorDate = date('Y-m-d H:i:s', $webhookInfo->last_error_date).' UTC';
36-
}
34+
$lastErrorDate = $this->getParsedDate($webhookInfo->last_error_date);
35+
$lastSynchronizationErrorDate = $this->getParsedDate($webhookInfo->last_synchronization_error_date);
36+
$allowedUpdates = $webhookInfo->allowed_updates ?: [];
3737

38-
$lastSynchronizationErrorDate = null;
39-
if ($webhookInfo->last_synchronization_error_date !== null) {
40-
$lastSynchronizationErrorDate = date('Y-m-d H:i:s', $webhookInfo->last_synchronization_error_date).' UTC';
41-
}
38+
$this->outputComponents()->twoColumnDetail('<fg=green;options=bold>Webhook Info</>');
39+
$this->outputComponents()->twoColumnDetail('url', $webhookInfo->url);
40+
$this->outputComponents()->twoColumnDetail(
41+
first: 'has_custom_certificate',
42+
second: $this->boolToConsoleString($webhookInfo->has_custom_certificate),
43+
);
44+
$this->outputComponents()->twoColumnDetail('pending_update_count', $webhookInfo->pending_update_count);
45+
$this->outputComponents()->twoColumnDetail('ip_address', $webhookInfo->ip_address);
46+
$this->outputComponents()->twoColumnDetail('last_error_date', $lastErrorDate);
47+
$this->outputComponents()->twoColumnDetail('last_error_message', $webhookInfo->last_error_message);
48+
$this->outputComponents()->twoColumnDetail('last_synchronization_error_date', $lastSynchronizationErrorDate);
49+
$this->outputComponents()->twoColumnDetail('max_connections', $webhookInfo->max_connections);
50+
$this->outputComponents()->twoColumnDetail('allowed_updates', count($allowedUpdates));
51+
52+
$this->newLine();
53+
$this->outputComponents()->twoColumnDetail('<fg=green;options=bold>Allowed Updates</>');
54+
table(rows: collect($allowedUpdates)->chunk(5)->toArray());
55+
56+
$this->newLine();
4257

43-
render(view('terminal::table', [
44-
'items' => [
45-
'url' => $webhookInfo->url,
46-
'has_custom_certificate' => $webhookInfo->has_custom_certificate ? 'true' : 'false',
47-
'pending_update_count' => $webhookInfo->pending_update_count,
48-
'ip_address' => $webhookInfo->ip_address,
49-
'last_error_date' => $lastErrorDate,
50-
'last_error_message' => $webhookInfo->last_error_message,
51-
'last_synchronization_error_date' => $lastSynchronizationErrorDate,
52-
'max_connections' => $webhookInfo->max_connections,
53-
'allowed_updates' => implode(', ', $webhookInfo->allowed_updates ?: []),
54-
]
55-
]));
56-
57-
return 0;
58+
return self::SUCCESS;
59+
}
60+
61+
protected function getParsedDate(?string $date): ?string
62+
{
63+
return $date !== null ? date('Y-m-d H:i:s', $date).' UTC' : null;
64+
}
65+
66+
protected function boolToConsoleString(bool $value): string
67+
{
68+
return $value ? '<fg=green;options=bold>TRUE</>' : '<fg=yellow;options=bold>FALSE</>';
5869
}
5970
}

src/Console/HookRemoveCommand.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,27 @@
22

33
namespace Nutgram\Laravel\Console;
44

5-
use GuzzleHttp\Exception\GuzzleException;
65
use Illuminate\Console\Command;
7-
use JsonException;
86
use SergiX44\Nutgram\Nutgram;
9-
use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;
107

118
class HookRemoveCommand extends Command
129
{
1310
protected $signature = 'nutgram:hook:remove {--d|drop-pending-updates}';
1411

1512
protected $description = 'Remove the bot webhook';
1613

17-
/**
18-
* @throws TelegramException
19-
* @throws GuzzleException
20-
* @throws JsonException
21-
*/
2214
public function handle(Nutgram $bot): int
2315
{
2416
$dropPendingUpdates = $this->option('drop-pending-updates');
2517

2618
$bot->deleteWebhook($dropPendingUpdates);
2719

2820
if ($dropPendingUpdates) {
29-
$this->info('Pending updates dropped.');
21+
$this->outputComponents()->info('Pending updates dropped.');
3022
}
31-
$this->info('Bot webhook removed.');
3223

33-
return 0;
24+
$this->outputComponents()->info('Bot webhook removed.');
25+
26+
return self::SUCCESS;
3427
}
3528
}

src/Console/HookSetCommand.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,50 @@
22

33
namespace Nutgram\Laravel\Console;
44

5-
use GuzzleHttp\Exception\GuzzleException;
65
use Illuminate\Console\Command;
7-
use JsonException;
6+
use Illuminate\Contracts\Console\PromptsForMissingInput;
87
use SergiX44\Nutgram\Nutgram;
98
use SergiX44\Nutgram\Telegram\Exceptions\TelegramException;
109

11-
class HookSetCommand extends Command
10+
class HookSetCommand extends Command implements PromptsForMissingInput
1211
{
1312
protected $signature = 'nutgram:hook:set {url} {--ip=} {--max-connections=50}';
1413

1514
protected $description = 'Set the bot webhook';
1615

17-
/**
18-
* @throws TelegramException
19-
* @throws GuzzleException
20-
* @throws JsonException
21-
*/
2216
public function handle(Nutgram $bot): int
2317
{
2418
/** @var string $url */
2519
$url = $this->argument('url');
2620

27-
/** @var ?string $ip_address */
28-
$ip_address = $this->option('ip') ?: null;
21+
try {
22+
$bot->setWebhook(
23+
url: $url,
24+
ip_address: $this->option('ip') ?: null,
25+
max_connections: $this->getMaxConnections(),
26+
secret_token: $this->getSecretToken(),
27+
);
28+
} catch (TelegramException $e) {
29+
$this->outputComponents()->error($e->getMessage());
30+
31+
return self::FAILURE;
32+
}
2933

30-
/** @var ?string $max_connections */
31-
$max_connections = $this->option('max-connections') ?: null;
34+
$this->outputComponents()->success('Bot webhook set.');
3235

33-
//cast to int if not null
34-
if (is_numeric($max_connections)) {
35-
$max_connections = (int)$max_connections;
36-
}
36+
return self::SUCCESS;
37+
}
3738

38-
$bot->setWebhook(
39-
url: $url,
40-
ip_address: $ip_address,
41-
max_connections: $max_connections,
42-
secret_token: config('nutgram.safe_mode', false) ? md5(config('app.key')) : null
43-
);
39+
protected function getMaxConnections(): ?int
40+
{
41+
/** @var ?string $max_connections */
42+
$max_connections = $this->option('max-connections') ?: null;
4443

45-
$this->info("Bot webhook set with url: $url");
44+
return is_numeric($max_connections) ? (int)$max_connections : null;
45+
}
4646

47-
return 0;
47+
protected function getSecretToken(): ?string
48+
{
49+
return config('nutgram.safe_mode', false) ? md5(config('app.key')) : null;
4850
}
4951
}

src/Console/IdeGenerateCommand.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ class IdeGenerateCommand extends Command
1313

1414
public function handle(): int
1515
{
16-
$this->warn('Generating IDE helper...');
16+
File::put(
17+
path: base_path('_ide_helper_nutgram.php'),
18+
contents: file_get_contents(__DIR__.'/../Stubs/Ide.stub'),
19+
);
1720

18-
$helper = file_get_contents(__DIR__.'/../Stubs/Ide.stub');
21+
$this->outputComponents()->success('IDE helper generated successfully.');
1922

20-
File::put(base_path('_ide_helper_nutgram.php'), $helper);
21-
22-
$this->info('Done!');
23-
24-
return 0;
23+
return self::SUCCESS;
2524
}
2625
}

0 commit comments

Comments
 (0)