fix: replace console messages with unified command.

This commit is contained in:
James Cole
2023-06-20 07:16:56 +02:00
parent f2b2c2109f
commit 42043de34f
62 changed files with 767 additions and 512 deletions

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
use PDO;
use PDOException;
@@ -33,6 +34,8 @@ use PDOException;
*/
class CreateDatabase extends Command
{
use ShowsFriendlyMessages;
/**
* The console command description.
*
@@ -54,7 +57,7 @@ class CreateDatabase extends Command
public function handle(): int
{
if ('mysql' !== env('DB_CONNECTION', 'mysql')) {
$this->info(sprintf('CreateDB does not apply to "%s", skipped.', env('DB_CONNECTION')));
$this->friendlyInfo(sprintf('CreateDB does not apply to "%s", skipped.', env('DB_CONNECTION')));
return 0;
}
@@ -67,7 +70,7 @@ class CreateDatabase extends Command
if ('' !== env('DB_SOCKET', '')) {
$dsn = sprintf('mysql:unix_socket=%s;charset=utf8mb4', env('DB_SOCKET', ''));
}
$this->info(sprintf('DSN is %s', $dsn));
$this->friendlyLine(sprintf('DSN is %s', $dsn));
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
@@ -79,7 +82,7 @@ class CreateDatabase extends Command
try {
$pdo = new PDO($dsn, env('DB_USERNAME'), env('DB_PASSWORD'), $options);
} catch (PDOException $e) {
$this->error(sprintf('Error when connecting to DB: %s', $e->getMessage()));
$this->friendlyError(sprintf('Error when connecting to DB: %s', $e->getMessage()));
}
// only continue when no error.
@@ -96,14 +99,14 @@ class CreateDatabase extends Command
}
}
if (false === $exists && true === $checked) {
$this->error(sprintf('Database "%s" does not exist.', env('DB_DATABASE')));
$this->friendlyError(sprintf('Database "%s" does not exist.', env('DB_DATABASE')));
// try to create it.
$pdo->exec(sprintf('CREATE DATABASE `%s` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;', env('DB_DATABASE')));
$this->info(sprintf('Created database "%s"', env('DB_DATABASE')));
$this->friendlyInfo(sprintf('Created database "%s"', env('DB_DATABASE')));
}
if (true === $exists && true === $checked) {
$this->info(sprintf('Database "%s" exists.', env('DB_DATABASE')));
$this->friendlyInfo(sprintf('Database "%s" exists.', env('DB_DATABASE')));
}
return 0;

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
@@ -36,6 +37,8 @@ use Str;
*/
class CreateFirstUser extends Command
{
use ShowsFriendlyMessages;
/**
* The console command description.
*
@@ -58,14 +61,14 @@ class CreateFirstUser extends Command
public function handle(): int
{
if ('testing' !== env('APP_ENV', 'local')) {
$this->error('This command only works in the testing environment.');
$this->friendlyError('This command only works in the testing environment.');
return 1;
}
$this->stupidLaravel();
$count = $this->repository->count();
if ($count > 0) {
$this->error('Already have more than zero users in DB.');
$this->friendlyError('Already have more than zero users in DB.');
return 1;
}
@@ -81,8 +84,8 @@ class CreateFirstUser extends Command
$user->save();
$user->setRememberToken(Str::random(60));
$this->info(sprintf('Created new admin user (ID #%d) with email address "%s" and password "%s".', $user->id, $user->email, $password));
$this->error('Change this password.');
$this->friendlyInfo(sprintf('Created new admin user (ID #%d) with email address "%s" and password "%s".', $user->id, $user->email, $password));
$this->friendlyWarning('Change this password.');
return 0;
}

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AutoBudget;
@@ -51,6 +52,8 @@ use Illuminate\Support\Facades\Log;
*/
class ForceDecimalSize extends Command
{
use ShowsFriendlyMessages;
protected $description = 'This command resizes DECIMAL columns in MySQL or PostgreSQL and correct amounts (only MySQL).';
protected $signature = 'firefly-iii:force-decimal-size';
private string $cast;
@@ -96,8 +99,8 @@ class ForceDecimalSize extends Command
Log::debug('Now in ForceDecimalSize::handle()');
$this->determineDatabaseType();
$this->error('Running this command is dangerous and can cause data loss.');
$this->error('Please do not continue.');
$this->friendlyError('Running this command is dangerous and can cause data loss.');
$this->friendlyError('Please do not continue.');
$question = $this->confirm('Do you want to continue?');
if (true === $question) {
$this->correctAmounts();
@@ -136,7 +139,7 @@ class ForceDecimalSize extends Command
});
$result = $query->get(['accounts.*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All accounts in %s', $currency->code));
$this->friendlyPositive(sprintf('All accounts in %s are OK', $currency->code));
return;
}
@@ -150,7 +153,7 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('Account #%d has %s with value "%s", this has been corrected to "%s".', $account->id, $field, $value, $correct));
$this->friendlyInfo(sprintf('Account #%d has %s with value "%s", this has been corrected to "%s".', $account->id, $field, $value, $correct));
Account::find($account->id)->update([$field => $correct]);
}
}
@@ -174,7 +177,7 @@ class ForceDecimalSize extends Command
}
if (!in_array((string)config('database.default'), ['mysql', 'pgsql', 'sqlite'], true)) {
$this->line(sprintf('Skip correcting amounts, does not support "%s"...', (string)config('database.default')));
$this->friendlyWarning(sprintf('Skip correcting amounts, does not support "%s"...', (string)config('database.default')));
return;
}
@@ -188,7 +191,6 @@ class ForceDecimalSize extends Command
*/
private function correctAmountsByCurrency(): void
{
$this->line('Going to correct amounts.');
/** @var Collection $enabled */
$enabled = TransactionCurrency::whereEnabled(1)->get();
/** @var TransactionCurrency $currency */
@@ -207,7 +209,6 @@ class ForceDecimalSize extends Command
*/
private function correctByCurrency(TransactionCurrency $currency): void
{
$this->line(sprintf('Going to correct amounts in currency %s ("%s").', $currency->code, $currency->name));
/**
* @var string $name
* @var array $fields
@@ -216,7 +217,7 @@ class ForceDecimalSize extends Command
switch ($name) {
default:
$message = sprintf('Cannot handle table "%s"', $name);
$this->line($message);
$this->friendlyError($message);
throw new FireflyException($message);
case 'accounts':
$this->correctAccountAmounts($currency, $fields);
@@ -279,7 +280,7 @@ class ForceDecimalSize extends Command
$result = $query->get(['*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All %s in %s', $table, $currency->code));
$this->friendlyPositive(sprintf('All %s in %s are OK', $table, $currency->code));
return;
}
@@ -293,7 +294,7 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('%s #%d has %s with value "%s", this has been corrected to "%s".', $table, $item->id, $field, $value, $correct));
$this->friendlyWarning(sprintf('%s #%d has %s with value "%s", this has been corrected to "%s".', $table, $item->id, $field, $value, $correct));
$class::find($item->id)->update([$field => $correct]);
}
}
@@ -330,7 +331,7 @@ class ForceDecimalSize extends Command
$result = $query->get(['piggy_banks.*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All piggy banks in %s', $currency->code));
$this->friendlyPositive(sprintf('All piggy banks in %s are OK', $currency->code));
return;
}
@@ -344,7 +345,7 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('Piggy bank #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
$this->friendlyWarning(sprintf('Piggy bank #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
PiggyBank::find($item->id)->update([$field => $correct]);
}
}
@@ -382,7 +383,7 @@ class ForceDecimalSize extends Command
$result = $query->get(['piggy_bank_events.*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All piggy bank events in %s', $currency->code));
$this->friendlyPositive(sprintf('All piggy bank events in %s are OK', $currency->code));
return;
}
@@ -396,7 +397,9 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('Piggy bank event #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
$this->friendlyWarning(
sprintf('Piggy bank event #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct)
);
PiggyBankEvent::find($item->id)->update([$field => $correct]);
}
}
@@ -434,7 +437,7 @@ class ForceDecimalSize extends Command
$result = $query->get(['piggy_bank_repetitions.*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All piggy bank repetitions in %s', $currency->code));
$this->friendlyPositive(sprintf('All piggy bank repetitions in %s', $currency->code));
return;
}
@@ -448,7 +451,9 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('Piggy bank repetition #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct));
$this->friendlyWarning(
sprintf('Piggy bank repetition #%d has %s with value "%s", this has been corrected to "%s".', $item->id, $field, $value, $correct)
);
PiggyBankRepetition::find($item->id)->update([$field => $correct]);
}
}
@@ -473,7 +478,7 @@ class ForceDecimalSize extends Command
$result = $query->get(['transactions.*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All transactions in %s', $currency->code));
$this->friendlyPositive(sprintf('All transactions in %s are OK', $currency->code));
}
/** @var Transaction $item */
@@ -485,7 +490,7 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('Transaction #%d has amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct));
$this->friendlyWarning(sprintf('Transaction #%d has amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct));
Transaction::find($item->id)->update(['amount' => $correct]);
}
@@ -499,7 +504,7 @@ class ForceDecimalSize extends Command
$result = $query->get(['*']);
if (0 === $result->count()) {
$this->line(sprintf('Correct: All transactions in foreign currency %s', $currency->code));
$this->friendlyPositive(sprintf('All transactions in foreign currency %s are OK', $currency->code));
return;
}
@@ -512,7 +517,9 @@ class ForceDecimalSize extends Command
// fix $field by rounding it down correctly.
$pow = pow(10, (int)$currency->decimal_places);
$correct = bcdiv((string)round($value * $pow), (string)$pow, 12);
$this->line(sprintf('Transaction #%d has foreign amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct));
$this->friendlyWarning(
sprintf('Transaction #%d has foreign amount with value "%s", this has been corrected to "%s".', $item->id, $value, $correct)
);
Transaction::find($item->id)->update(['foreign_amount' => $correct]);
}
}
@@ -538,7 +545,7 @@ class ForceDecimalSize extends Command
*/
private function updateDecimals(): void
{
$this->info('Going to force the size of DECIMAL columns. Please hold.');
$this->friendlyInfo('Going to force the size of DECIMAL columns. Please hold.');
$type = (string)config('database.default');
/**
@@ -548,11 +555,11 @@ class ForceDecimalSize extends Command
foreach ($this->tables as $name => $fields) {
/** @var string $field */
foreach ($fields as $field) {
$this->line(sprintf('Updating table "%s", field "%s"...', $name, $field));
$this->friendlyLine(sprintf('Updating table "%s", field "%s"...', $name, $field));
switch ($type) {
default:
$this->error(sprintf('Cannot handle database type "%s".', $type));
$this->friendlyError(sprintf('Cannot handle database type "%s".', $type));
return;
case 'pgsql':

View File

@@ -23,6 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Console\Commands\VerifiesAccessToken;
use FireflyIII\Exceptions\FireflyException;
use Illuminate\Console\Command;
@@ -33,6 +34,7 @@ use Illuminate\Support\Facades\Schema;
class ForceMigration extends Command
{
use ShowsFriendlyMessages;
use VerifiesAccessToken;
/**
@@ -58,13 +60,13 @@ class ForceMigration extends Command
public function handle(): int
{
if (!$this->verifyAccessToken()) {
$this->error('Invalid access token.');
$this->friendlyError('Invalid access token.');
return 1;
}
$this->error('Running this command is dangerous and can cause data loss.');
$this->error('Please do not continue.');
$this->friendlyError('Running this command is dangerous and can cause data loss.');
$this->friendlyError('Please do not continue.');
$question = $this->confirm('Do you want to continue?');
if (true === $question) {
$user = $this->getUser();
@@ -80,16 +82,16 @@ class ForceMigration extends Command
private function forceMigration(): void
{
DB::commit();
$this->line('Dropping "migrations" table...');
$this->friendlyLine('Dropping "migrations" table...');
sleep(2);
Schema::dropIfExists('migrations');
$this->line('Re-run all migrations...');
$this->friendlyLine('Re-run all migrations...');
Artisan::call('migrate', ['--seed' => true]);
sleep(2);
$this->line('');
$this->line('There is a good chance you just saw a lot of error messages.');
$this->line('No need to panic yet. First try to access Firefly III (again).');
$this->line('The issue, whatever it was, may have been solved now.');
$this->line('');
$this->friendlyLine('');
$this->friendlyWarning('There is a good chance you just saw a lot of error messages.');
$this->friendlyWarning('No need to panic yet. First try to access Firefly III (again).');
$this->friendlyWarning('The issue, whatever it was, may have been solved now.');
$this->friendlyLine('');
}
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use Crypt;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use FireflyIII\Models\Attachment;
use Illuminate\Console\Command;
use Illuminate\Contracts\Encryption\DecryptException;
@@ -37,6 +38,8 @@ use Storage;
*/
class ScanAttachments extends Command
{
use ShowsFriendlyMessages;
/**
* The console command description.
*
@@ -79,7 +82,7 @@ class ScanAttachments extends Command
$attachment->md5 = $md5;
$attachment->mime = $mime;
$attachment->save();
$this->line(sprintf('Fixed attachment #%d', $attachment->id));
$this->friendlyInfo(sprintf('Fixed attachment #%d', $attachment->id));
}
return 0;

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
/**
@@ -31,6 +32,8 @@ use Illuminate\Console\Command;
*/
class SetLatestVersion extends Command
{
use ShowsFriendlyMessages;
/**
* The console command description.
*
@@ -52,13 +55,13 @@ class SetLatestVersion extends Command
public function handle(): int
{
if (!$this->option('james-is-cool')) {
$this->error('Am too!');
$this->friendlyError('Am too!');
return 0;
}
app('fireflyconfig')->set('db_version', config('firefly.db_version'));
app('fireflyconfig')->set('ff3_version', config('firefly.version'));
$this->line('Updated version.');
$this->friendlyInfo('Updated version.');
return 0;
}

View File

@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace FireflyIII\Console\Commands\System;
use FireflyIII\Console\Commands\ShowsFriendlyMessages;
use Illuminate\Console\Command;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Log;
@@ -35,6 +36,8 @@ use Storage;
*/
class VerifySecurityAlerts extends Command
{
use ShowsFriendlyMessages;
/**
* The console command description.
*
@@ -80,23 +83,23 @@ class VerifySecurityAlerts extends Command
// depends on level
if ('info' === $array['level']) {
Log::debug('INFO level alert');
$this->info($array['message']);
$this->friendlyInfo($array['message']);
return 0;
}
if ('warning' === $array['level']) {
Log::debug('WARNING level alert');
$this->warn('------------------------ :o');
$this->warn($array['message']);
$this->warn('------------------------ :o');
$this->friendlyWarning('------------------------ :o');
$this->friendlyWarning($array['message']);
$this->friendlyWarning('------------------------ :o');
return 0;
}
if ('danger' === $array['level']) {
Log::debug('DANGER level alert');
$this->error('------------------------ :-(');
$this->error($array['message']);
$this->error('------------------------ :-(');
$this->friendlyError('------------------------ :-(');
$this->friendlyError($array['message']);
$this->friendlyError('------------------------ :-(');
return 0;
}
@@ -104,8 +107,8 @@ class VerifySecurityAlerts extends Command
return 0;
}
}
Log::debug('This version is not mentioned.');
Log::debug(sprintf('No security alerts for version %s', $version));
$this->friendlyPositive(sprintf('No security alerts for version %s', $version));
return 0;
}