From 7c1c2a7233ecd626076df3a25002ecf1aafe9862 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Fri, 13 Nov 2020 09:14:31 +0100 Subject: [PATCH] update importation command: add option --file --- README.md | 13 ++++++++++--- src/Command/MailImportCommand.php | 26 ++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9023fe7..660340d 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ In case of you want to delete the mailing: $ php bin/console mailing:delete xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ``` -Finally, create a script run in a cron: +Finally, create a script run in a cron: ``` #!/bin/sh @@ -84,12 +84,19 @@ Finally, create a script run in a cron: cd "/path/to/app" find /var/lib/mailrss -name "foobar-*" | while read MAIL_FILE; do - cat "MAIL_FILE" | php bin/console mail:import xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + MAILING_ID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" - rm "MAIL_FILE" + php bin/console mail:import "$MAILING_ID" -f "$MAIL_FILE" && rm "$MAIL_FILE" done ``` +You also can import a mail using stdin: + +``` +$ php bin/console mail:import "$MAILING_ID" < "$MAIL_FILE" +$ command | php bin/console mail:import "$MAILING_ID" +``` + If you wan create another feed then: * Create an aliase diff --git a/src/Command/MailImportCommand.php b/src/Command/MailImportCommand.php index f7f6369..3eb26c6 100644 --- a/src/Command/MailImportCommand.php +++ b/src/Command/MailImportCommand.php @@ -39,6 +39,7 @@ class MailImportCommand extends Command { $this ->setDescription('Import a mail into a mailing') + ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'File') ->addArgument('mailing_id', InputArgument::OPTIONAL, 'ID of the mailing') ; } @@ -47,6 +48,7 @@ class MailImportCommand extends Command { $io = new SymfonyStyle($input, $output); $mailingId = $input->getArgument('mailing_id'); + $file = $input->getOption('file'); $mailing = $this->mailingRepo->find(['id' => $mailingId]); @@ -56,16 +58,32 @@ class MailImportCommand extends Command return Command::FAILURE; } - $stdIn = file_get_contents('php://stdin'); + if (empty($file)) { + $file = 'php://stdin'; - if (empty($stdIn)) { - $io->error('Standard input is empty'); + $content = trim(file_get_contents($file)); + + if (empty($content)) { + $io->error('Standard input is empty'); + + return Command::FAILURE; + } + } elseif (file_exists($file) && is_readable($file) && is_file($file)) { + $content = trim(file_get_contents($file)); + + if (empty($content)) { + $io->error('File is empty'); + + return Command::FAILURE; + } + } else { + $io->error('No such file or is not readable'); return Command::FAILURE; } $parser = new Parser(); - $parser->setText($stdIn); + $parser->setText($content); $subject = $parser->getHeader('subject'); $date = $parser->getHeader('date');