mirror of
https://github.com/verdigado/groupfolder_tags.git
synced 2024-12-03 18:22:52 +01:00
occ commands: output data in tables instead of json by default
This commit is contained in:
parent
8750f3d927
commit
61f1e253b5
10 changed files with 149 additions and 104 deletions
|
@ -18,8 +18,8 @@
|
|||
<nextcloud min-version="29" max-version="30"/>
|
||||
</dependencies>
|
||||
<commands>
|
||||
<command>OCA\GroupfolderTags\Command\Tag</command>
|
||||
<command>OCA\GroupfolderTags\Command\SetTag</command>
|
||||
<command>OCA\GroupfolderTags\Command\GetTag</command>
|
||||
<command>OCA\GroupfolderTags\Command\GetFolder</command>
|
||||
<command>OCA\GroupfolderTags\Command\FindGroupfolders</command>
|
||||
</commands>
|
||||
</info>
|
||||
|
|
|
@ -15,6 +15,7 @@ class GetTag extends TagCommand {
|
|||
->setDescription('Get single tag value by Groupfolder ID and key. Omit key to get all tags.')
|
||||
->addArgument('folder_id', InputArgument::REQUIRED, 'Groupfolder ID of the tag')
|
||||
->addArgument('key', InputArgument::OPTIONAL, 'Key of the tag');
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
|
@ -33,16 +34,8 @@ class GetTag extends TagCommand {
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (isset($tagKey)) {
|
||||
/** @var Tag $tag */
|
||||
$tag = current($tags);
|
||||
$output->writeln($tag->getTagValue());
|
||||
return 0;
|
||||
}
|
||||
|
||||
foreach ($tags as $tag) {
|
||||
$output->writeln(json_encode($tag));
|
||||
}
|
||||
$this->writeTableInOutputFormat($input, $output, $this->formatTagEntities($tags));
|
||||
|
||||
return 0;
|
||||
} catch (Exception $e) {
|
||||
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
|
||||
|
|
|
@ -2,18 +2,21 @@
|
|||
|
||||
namespace OCA\GroupfolderTags\Command;
|
||||
|
||||
use OCA\GroupfolderTags\Errors\GroupfolderNotFound;
|
||||
|
||||
use OCP\DB\Exception;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class GetFolder extends TagCommand {
|
||||
class FindGroupfolders extends TagCommand {
|
||||
protected function configure(): void {
|
||||
$this
|
||||
->setName('groupfolder-tags:get-folder')
|
||||
->setDescription('Get Groupfolders with tag key optionally filtered by tag value.')
|
||||
->setName('groupfolder-tags:find-groupfolders')
|
||||
->setDescription('Get all groupfolders with tag key optionally filtered by tag value.')
|
||||
->addArgument('key', InputArgument::REQUIRED, 'Key of the tag')
|
||||
->addArgument('value', InputArgument::OPTIONAL, 'Value of the tag');
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
|
@ -21,21 +24,25 @@ class GetFolder extends TagCommand {
|
|||
$tagValue = $input->getArgument('value');
|
||||
|
||||
try {
|
||||
$results = $this->service->findFolder($tagKey, $tagValue);
|
||||
$groupfolderIds = $this->service->findGroupfoldersWithTag($tagKey, $tagValue);
|
||||
|
||||
if (empty($results)) {
|
||||
$output->writeln("<error>Could not find Groupfolders for tag</error>");
|
||||
if (empty($groupfolderIds)) {
|
||||
$output->writeln("<error>No matching Groupfolders</error>");
|
||||
return 1;
|
||||
}
|
||||
|
||||
foreach ($results as $result) {
|
||||
$folder = $this->folderManager->getFolder($result['group_folder_id'], $this->rootFolder->getMountPoint()->getNumericStorageId());
|
||||
if ($folder === false) {
|
||||
$output->writeln("<error>Folder not found: {$result['group_folder_id']}</error>");
|
||||
continue;
|
||||
$results = [];
|
||||
|
||||
foreach ($groupfolderIds as $groupfolderId) {
|
||||
try {
|
||||
$results[] = $this->formatGroupfolder($groupfolderId);
|
||||
} catch (GroupfolderNotFound $e) {
|
||||
$output->writeln("<error>{$e->getMessage()}</error>");
|
||||
}
|
||||
$output->writeln(json_encode($folder));
|
||||
}
|
||||
|
||||
$this->writeTableInOutputFormat($input, $output, $results);
|
||||
|
||||
return 0;
|
||||
} catch (Exception $e) {
|
||||
$output->writeln("<error>Exception \"{$e->getMessage()}\" at {$e->getFile()} line {$e->getLine()}</error>");
|
51
lib/Command/SetTag.php
Normal file
51
lib/Command/SetTag.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\GroupfolderTags\Command;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
class SetTag extends TagCommand {
|
||||
protected function configure(): void {
|
||||
$this
|
||||
->setName('groupfolder-tags:set')
|
||||
->setDescription('Update or add a tag (and optional tag value) to a groupfolder by id')
|
||||
->addArgument('groupfolder_id', InputArgument::REQUIRED, 'groupfolder id to add or update tag of')
|
||||
->addArgument('key', InputArgument::REQUIRED, 'key of tag')
|
||||
->addArgument('value', InputArgument::OPTIONAL, 'value of tag');
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$errors = [];
|
||||
|
||||
$groupFolderId = $input->getArgument('groupfolder_id');
|
||||
$tagKey = $input->getArgument('key');
|
||||
$tagValue = $input->getArgument('value');
|
||||
|
||||
if(!is_numeric($groupFolderId)) {
|
||||
$errors[] = "no group folder id provided";
|
||||
}
|
||||
|
||||
if(is_null($tagKey) || $tagKey === "") {
|
||||
$errors[] = "no tag key provided";
|
||||
}
|
||||
|
||||
if(empty($errors)) {
|
||||
$tag = $this->service->update((int)$groupFolderId, $tagKey, $tagValue);
|
||||
$this->writeTableInOutputFormat($input, $output, [$this->formatTagEntity($tag)]);
|
||||
|
||||
return 0;
|
||||
} else {
|
||||
$output->writeln("Error: \n" . implode("\n", $errors));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,71 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OCA\GroupfolderTags\Command;
|
||||
|
||||
use OC\Core\Command\Base;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use OCA\GroupfolderTags\Service\TagService;
|
||||
|
||||
class Tag extends Base {
|
||||
public function __construct(
|
||||
private TagService $service,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void {
|
||||
$this
|
||||
->setName('groupfolder-tags:update')
|
||||
->setDescription('Update or add a tag (and optional tag value) to a groupfolder by id')
|
||||
->addOption(
|
||||
'groupfolder_id',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Add or update tag of the given groupfolder id'
|
||||
)
|
||||
->addOption(
|
||||
'key',
|
||||
null,
|
||||
InputOption::VALUE_REQUIRED,
|
||||
'Set tag key to given value'
|
||||
)
|
||||
->addOption(
|
||||
'value',
|
||||
null,
|
||||
InputOption::VALUE_OPTIONAL,
|
||||
'Set tag value to given value'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int {
|
||||
$errors = [];
|
||||
|
||||
$groupFolderId = $input->getOption('groupfolder_id');
|
||||
$tagKey = $input->getOption('key');
|
||||
$tagValue = $input->getOption('value');
|
||||
|
||||
if(!is_numeric($groupFolderId)) {
|
||||
$errors[] = "no group folder id provided";
|
||||
}
|
||||
|
||||
if(is_null($tagKey) || $tagKey === "") {
|
||||
$errors[] = "no tag key provided";
|
||||
}
|
||||
|
||||
if(empty($errors)) {
|
||||
$output->writeln($groupFolderId . " " . $tagKey . " " . $tagValue);
|
||||
$output->writeln(json_encode($this->service->update((int)$groupFolderId, $tagKey, $tagValue)));
|
||||
return 0;
|
||||
} else {
|
||||
$output->writeln("Error: \n" . implode("\n", $errors));
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -2,19 +2,67 @@
|
|||
|
||||
namespace OCA\GroupfolderTags\Command;
|
||||
|
||||
use OCA\GroupfolderTags\Db\Tag;
|
||||
use OCA\GroupfolderTags\Service\TagService;
|
||||
use OCA\GroupfolderTags\Errors\GroupfolderNotFound;
|
||||
|
||||
use OCA\GroupFolders\Command\FolderCommand;
|
||||
use OCA\GroupFolders\Folder\FolderManager;
|
||||
use OCA\GroupFolders\Mount\MountProvider;
|
||||
use OCA\GroupfolderTags\Service\TagService;
|
||||
|
||||
|
||||
use OCP\Files\IRootFolder;
|
||||
use OCP\IDateTimeFormatter;
|
||||
|
||||
abstract class TagCommand extends FolderCommand {
|
||||
private int $rootFolderNumericStorageId;
|
||||
|
||||
public function __construct(
|
||||
protected readonly TagService $service,
|
||||
FolderManager $folderManager,
|
||||
IRootFolder $rootFolder,
|
||||
MountProvider $mountProvider
|
||||
MountProvider $mountProvider,
|
||||
protected readonly TagService $service,
|
||||
private readonly IDateTimeFormatter $dateTimeFormatter,
|
||||
) {
|
||||
parent::__construct($folderManager, $rootFolder, $mountProvider);
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function getRootFolderNumericStorageId() {
|
||||
if(!isset($this->rootFolderNumericStorageId)) {
|
||||
$this->rootFolderNumericStorageId = $this->rootFolder->getMountPoint()->getNumericStorageId();
|
||||
}
|
||||
|
||||
return $this->rootFolderNumericStorageId;
|
||||
}
|
||||
|
||||
protected function formatTagEntity(Tag $tag) {
|
||||
return [
|
||||
"Groupfolder Id" => $tag->getGroupFolderId(),
|
||||
"Key" => $tag->getTagKey(),
|
||||
"Value" => $tag->getTagValue(),
|
||||
"Last Updated" => $this->dateTimeFormatter->formatDateTime($tag->getLastUpdatedTimestamp()),
|
||||
];
|
||||
}
|
||||
|
||||
protected function formatTagEntities($tags) {
|
||||
return array_map($this->formatTagEntity(...), $tags);
|
||||
}
|
||||
|
||||
protected function formatGroupfolder($groupfolderId) {
|
||||
$groupfolder = $this->folderManager->getFolder($groupfolderId, $this->getRootFolderNumericStorageId());
|
||||
|
||||
if ($groupfolder === false) {
|
||||
throw new GroupfolderNotFound($groupfolderId);
|
||||
}
|
||||
|
||||
return [
|
||||
"Groupfolder Id" => $groupfolder["id"],
|
||||
"Mount Point" => $groupfolder["mount_point"],
|
||||
"Quota" => $groupfolder["quota"],
|
||||
"Size" => $groupfolder["size"],
|
||||
"ACL" => $groupfolder["acl"],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,7 +95,7 @@ class TagMapper extends QBMapper {
|
|||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function findFolder(string $tagKey, ?string $tagValue): array {
|
||||
public function findGroupfoldersWithTag(string $tagKey, ?string $tagValue): array {
|
||||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->selectDistinct('group_folder_id')
|
||||
->from(self::TABLENAME)
|
||||
|
@ -105,6 +105,6 @@ class TagMapper extends QBMapper {
|
|||
$qb->andWhere($qb->expr()->eq('tag_value', $qb->createNamedParameter($tagValue)));
|
||||
}
|
||||
|
||||
return $qb->executeQuery()->fetchAll();
|
||||
return $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
|
||||
}
|
||||
}
|
||||
|
|
9
lib/Errors/GroupfolderNotFound.php
Normal file
9
lib/Errors/GroupfolderNotFound.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace OCA\GroupfolderTags\Errors;
|
||||
|
||||
class GroupfolderNotFound extends NotFoundException {
|
||||
public function __construct($id) {
|
||||
parent::__construct("groupfolder", ["id" => $id]);
|
||||
}
|
||||
}
|
|
@ -2,5 +2,13 @@
|
|||
|
||||
namespace OCA\GroupfolderTags\Errors;
|
||||
|
||||
abstract class NotFoundException extends \Exception {
|
||||
}
|
||||
abstract class NotFoundException extends \RuntimeException {
|
||||
public function __construct($entity, array|string $criteria) {
|
||||
$message = sprintf(
|
||||
"Could not find %s with criteria %s",
|
||||
class_exists($entity) ? array_pop(explode('\\', $entity)) : $entity,
|
||||
is_string($criteria) ? $criteria : json_encode($criteria),
|
||||
);
|
||||
parent::__construct($message);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -82,7 +82,7 @@ class TagService {
|
|||
/**
|
||||
* @throws \OCP\DB\Exception
|
||||
*/
|
||||
public function findFolder(string $key, ?string $value): array {
|
||||
return $this->mapper->findFolder($key, $value);
|
||||
public function findGroupfoldersWithTag(string $key, ?string $value): array {
|
||||
return $this->mapper->findGroupfoldersWithTag($key, $value);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue