Browse Source

Exit code as enum

pull/708/head
James Cole 11 months ago
parent
commit
b70bb807eb
No known key found for this signature in database GPG Key ID: B49A324B7EAD6D80
  1. 12
      app/Console/AutoImports.php
  2. 23
      app/Console/Commands/AutoImport.php
  3. 31
      app/Console/Commands/Import.php
  4. 42
      app/Enums/ExitCode.php

12
app/Console/AutoImports.php

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace App\Console;
use App\Enums\ExitCode;
use App\Events\ImportedTransactions;
use App\Exceptions\ImporterErrorException;
use App\Services\Camt\Conversion\RoutineManager as CamtRoutineManager;
@ -112,7 +113,7 @@ trait AutoImports
/**
* This method only works on files with an extension with exactly three letters
* (ie. "csv", "xml").
* (i.e. "csv", "xml").
*/
private function hasJsonConfiguration(string $directory, string $file): bool
{
@ -284,13 +285,16 @@ trait AutoImports
event(new ImportedTransactions($messages, $warnings, $errors, $this->conversionRateLimits));
if (count($this->importErrors) > 0 || count($this->conversionRateLimits) > 0) {
return 1;
app('log')->error(sprintf('Exit code is %s.', ExitCode::GENERAL_ERROR->name));
return ExitCode::GENERAL_ERROR->value;
}
if (0 === count($messages) && 0 === count($warnings) && 0 === count($errors)) {
return 73;
app('log')->error(sprintf('Exit code is %s.', ExitCode::NOTHING_WAS_IMPORTED->name));
return ExitCode::NOTHING_WAS_IMPORTED->value;
}
return 0;
app('log')->error(sprintf('Exit code is %s.', ExitCode::SUCCESS->name));
return ExitCode::SUCCESS->value;
}
/**

23
app/Console/Commands/AutoImport.php

@ -27,6 +27,7 @@ namespace App\Console\Commands;
use App\Console\AutoImports;
use App\Console\HaveAccess;
use App\Console\VerifyJSON;
use App\Enums\ExitCode;
use Illuminate\Console\Command;
/**
@ -60,8 +61,8 @@ final class AutoImport extends Command
$access = $this->haveAccess();
if (false === $access) {
$this->error(sprintf('[a] No access, or no connection is possible to your local Firefly III instance at %s.', config('importer.url')));
return 64;
app('log')->error(sprintf('Exit code is %s.', ExitCode::NO_CONNECTION->name));
return ExitCode::NO_CONNECTION->value;
}
$argument = (string) ($this->argument('directory') ?? './');
@ -70,13 +71,14 @@ final class AutoImport extends Command
$directory = realpath($argument);
if (false === $directory) {
$this->error(sprintf('Path "%s" is not a valid location.', $argument));
return 65;
app('log')->error(sprintf('Exit code is %s.', ExitCode::INVALID_PATH->name));
return ExitCode::INVALID_PATH->value;
}
if (!$this->isAllowedPath($directory)) {
$this->error(sprintf('Path "%s" is not in the list of allowed paths (IMPORT_DIR_ALLOWLIST).', $directory));
return 66;
app('log')->error(sprintf('Exit code is %s.', ExitCode::NOT_ALLOWED_PATH->name));
return ExitCode::NOT_ALLOWED_PATH->value;
}
$this->line(sprintf('Going to automatically import everything found in %s (%s)', $directory, $argument));
@ -86,7 +88,8 @@ final class AutoImport extends Command
$this->info('To learn more about this process, read the docs:');
$this->info('https://docs.firefly-iii.org/');
return 67;
app('log')->error(sprintf('Exit code is %s.', ExitCode::NO_FILES_FOUND->name));
return ExitCode::NO_FILES_FOUND->value;
}
$this->line(sprintf('Found %d (importable +) JSON file sets in %s', count($files), $directory));
@ -101,10 +104,10 @@ final class AutoImport extends Command
foreach ($result as $file => $code) {
$this->warn(sprintf('File %s returned code #%d', $file, $code));
}
return 1;
app('log')->error(sprintf('Exit code is %s.', ExitCode::GENERAL_ERROR->name));
return ExitCode::GENERAL_ERROR->value;
}
return 0;
app('log')->error(sprintf('Exit code is %s.', ExitCode::SUCCESS->name));
return ExitCode::SUCCESS->value;
}
}

31
app/Console/Commands/Import.php

@ -27,10 +27,12 @@ namespace App\Console\Commands;
use App\Console\AutoImports;
use App\Console\HaveAccess;
use App\Console\VerifyJSON;
use App\Enums\ExitCode;
use App\Events\ImportedTransactions;
use App\Exceptions\ImporterErrorException;
use App\Services\Shared\Configuration\Configuration;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
/**
* Class Import
@ -68,8 +70,8 @@ final class Import extends Command
$access = $this->haveAccess();
if (false === $access) {
$this->error(sprintf('No access granted, or no connection is possible to your local Firefly III instance at %s.', config('importer.url')));
return 64;
app('log')->error(sprintf('Exit code is %s.', ExitCode::NO_CONNECTION->name));
return ExitCode::NO_CONNECTION->value;
}
$this->info(sprintf('Welcome to the Firefly III data importer, v%s', config('importer.version')));
@ -82,8 +84,8 @@ final class Import extends Command
$directory = dirname($config);
if (!$this->isAllowedPath($directory)) {
$this->error(sprintf('Path "%s" is not in the list of allowed paths (IMPORT_DIR_ALLOWLIST).', $directory));
return 65;
app('log')->error(sprintf('Exit code is %s.', ExitCode::INVALID_PATH->name));
return ExitCode::INVALID_PATH->value;
}
}
@ -92,8 +94,8 @@ final class Import extends Command
$directory = dirname($file);
if (!$this->isAllowedPath($directory)) {
$this->error(sprintf('Path "%s" is not in the list of allowed paths (IMPORT_DIR_ALLOWLIST).', $directory));
return 66;
app('log')->error(sprintf('Exit code is %s.', ExitCode::NOT_ALLOWED_PATH->name));
return ExitCode::NOT_ALLOWED_PATH->value;
}
}
@ -101,16 +103,16 @@ final class Import extends Command
$message = sprintf('The importer can\'t import: configuration file "%s" does not exist or could not be read.', $config);
$this->error($message);
app('log')->error($message);
return 68;
app('log')->error(sprintf('Exit code is %s.', ExitCode::CANNOT_READ_CONFIG->name));
return ExitCode::CANNOT_READ_CONFIG->value;
}
$jsonResult = $this->verifyJSON($config);
if (false === $jsonResult) {
$message = 'The importer can\'t import: could not decode the JSON in the config file.';
$this->error($message);
return 69;
app('log')->error(sprintf('Exit code is %s.', ExitCode::CANNOT_PARSE_CONFIG->name));
return ExitCode::CANNOT_PARSE_CONFIG->value;
}
$configuration = Configuration::fromArray(json_decode(file_get_contents($config), true));
if ('file' === $configuration->getFlow() && (!file_exists($file) || (file_exists($file) && !is_file($file)))) {
@ -118,7 +120,8 @@ final class Import extends Command
$this->error($message);
app('log')->error($message);
return 70;
app('log')->error(sprintf('Exit code is %s.', ExitCode::IMPORTABLE_FILE_NOT_FOUND->name));
return ExitCode::IMPORTABLE_FILE_NOT_FOUND->value;
}
$configuration->updateDateRange();
@ -137,7 +140,7 @@ final class Import extends Command
$exitCode = 0;
if (0 !== count($this->conversionErrors)) {
$this->error('There are many errors in the data conversion. The import will stop here.');
$exitCode = 72;
$exitCode = ExitCode::TOO_MANY_ERRORS_PROCESSING->value;
}
if (0 === count($this->conversionErrors)) {
$this->line(sprintf('Done converting from file %s using configuration %s.', $file, $config));
@ -155,10 +158,10 @@ final class Import extends Command
event(new ImportedTransactions($messages, $warnings, $errors, $this->conversionRateLimits));
if (0 !== count($this->importErrors)) {
$exitCode = 1;
$exitCode = ExitCode::GENERAL_ERROR->value;
}
if (0 === count($messages) && 0 === count($warnings) && 0 === count($errors)) {
$exitCode = 73;
$exitCode = ExitCode::NOTHING_WAS_IMPORTED->value;
}
return $exitCode;

42
app/Enums/ExitCode.php

@ -0,0 +1,42 @@
<?php
/*
* ExitCode.php
* Copyright (c) 2024 james@firefly-iii.org
*
* This file is part of the Firefly III Data Importer
* (https://github.com/firefly-iii/data-importer).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Enums;
enum ExitCode: int
{
case SUCCESS = 0;
case GENERAL_ERROR = 1;
case NO_CONNECTION = 64;
case INVALID_PATH = 65;
case NOT_ALLOWED_PATH = 66;
case NO_FILES_FOUND = 67;
case CANNOT_READ_CONFIG = 68;
case CANNOT_PARSE_CONFIG = 69;
case IMPORTABLE_FILE_NOT_FOUND = 70;
case CANNOT_READ_IMPORTABLE_FILE = 71;
case TOO_MANY_ERRORS_PROCESSING = 72;
case NOTHING_WAS_IMPORTED = 73;
}
Loading…
Cancel
Save