Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@
<order>5</order>
</navigation>
</navigations>

<commands>
<command>OCA\Calendar\Command\Export</command>
<command>OCA\Calendar\Command\Import</command>
</commands>
</info>
82 changes: 82 additions & 0 deletions lib/Command/Export.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/**
* @copyright 2022 Christopher Ng <[email protected]>
*
* @author Christopher Ng <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Calendar\Command;

use OC\Core\Command\Base;
use OCA\Calendar\UserMigration\CalendarMigrator;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Export extends Base {

/** @var IUserManager */
private $userManager;

/** @var CalendarMigrator */
private $calendarMigrator;

public function __construct(
IUserManager $userManager,
CalendarMigrator $calendarMigrator
) {
parent::__construct();
$this->userManager = $userManager;
$this->calendarMigrator = $calendarMigrator;
}

protected function configure() {
$this
->setName('calendar:export')
->setDescription('Export the calendars of a user')
->addArgument(
'user',
InputArgument::REQUIRED,
'User to export',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $this->userManager->get($input->getArgument('user'));

if (!$user instanceof IUser) {
$output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
return 1;
}

try {
$this->calendarMigrator->export($user, $output);
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return $e->getCode() !== 0 ? $e->getCode() : 1;
}

return 0;
}
}
93 changes: 93 additions & 0 deletions lib/Command/Import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/**
* @copyright 2022 Christopher Ng <[email protected]>
*
* @author Christopher Ng <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Calendar\Command;

use OC\Core\Command\Base;
use OCA\Calendar\UserMigration\CalendarMigrator;
use OCP\IUser;
use OCP\IUserManager;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Import extends Base {

/** @var IUserManager */
private $userManager;

/** @var CalendarMigrator */
private $calendarMigrator;

public function __construct(
IUserManager $userManager,
CalendarMigrator $calendarMigrator
) {
parent::__construct();
$this->userManager = $userManager;
$this->calendarMigrator = $calendarMigrator;
}

protected function configure() {
$this
->setName('calendar:import')
->setDescription('Import a calendar to a user\'s account')
->addArgument(
'user',
InputArgument::REQUIRED,
'User to import the calendar for',
)
->addArgument(
'path',
InputArgument::REQUIRED,
'Path to the *.ics file',
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$user = $this->userManager->get($input->getArgument('user'));

[
'basename' => $filename,
'dirname' => $srcDir,
] = pathinfo($input->getArgument('path'));


if (!$user instanceof IUser) {
$output->writeln('<error>User ' . $input->getArgument('user') . ' does not exist</error>');
return 1;
}

try {
$this->calendarMigrator->import($user, $srcDir, $filename, $output);
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage() . '</error>');
return $e->getCode() !== 0 ? $e->getCode() : 1;
}

return 0;
}
}
Loading