Skip to content

Commit 260cd19

Browse files
author
Johannes Leuker
committed
Add commands to manage tags via OCC
list, add, delete, edit Signed-off-by: Johannes Leuker <j.leuker@hosting.de>
1 parent f031dd6 commit 260cd19

File tree

11 files changed

+964
-0
lines changed

11 files changed

+964
-0
lines changed

core/Command/SystemTag/Add.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\TagAlreadyExistsException;
29+
use Symfony\Component\Console\Input\InputArgument;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class Add extends Base {
35+
36+
/** @var ISystemTagManager */
37+
protected $systemTagManager;
38+
39+
/**
40+
* @param ISystemTagManager $systemTagManager
41+
*/
42+
public function __construct(ISystemTagManager $systemTagManager) {
43+
$this->systemTagManager = $systemTagManager;
44+
parent::__construct();
45+
}
46+
47+
protected function configure() {
48+
$this
49+
->setName('tag:add')
50+
->setDescription('Add new tag')
51+
->addArgument(
52+
'name',
53+
InputArgument::REQUIRED,
54+
'sets the \'name\' parameter',
55+
)
56+
->addArgument(
57+
'access',
58+
InputArgument::REQUIRED,
59+
'sets the `visible` and `assignable` parameters (public, restricted, invisible)',
60+
)
61+
->addOption(
62+
'output',
63+
null,
64+
InputOption::VALUE_OPTIONAL,
65+
'Output format (plain, json or json_pretty, default is plain)',
66+
$this->defaultOutputFormat
67+
);
68+
}
69+
70+
protected function execute(InputInterface $input, OutputInterface $output): int {
71+
$name = $input->getArgument('name');
72+
73+
if ($input->getArgument('access')) {
74+
switch ($input->getArgument('access')) {
75+
case 'public':
76+
$userVisible = true;
77+
$userAssignable = true;
78+
break;
79+
case 'restricted':
80+
$userVisible = true;
81+
$userAssignable = false;
82+
break;
83+
case 'invisible':
84+
$userVisible = false;
85+
$userAssignable = false;
86+
break;
87+
default:
88+
$output->writeln('<error>`access` property is invalid</error>');
89+
return 1;
90+
}
91+
}
92+
93+
try {
94+
$tag = $this->systemTagManager->createTag($name, $userVisible, $userAssignable);
95+
$this->writeArrayInOutputFormat($input, $output,
96+
[
97+
$tag->getId() => [
98+
'name' => $tag->getName(),
99+
'access' => $tag->getHumanReadableAccess(),
100+
]
101+
]);
102+
return 0;
103+
} catch (TagAlreadyExistsException $e) {
104+
$output->writeln('<error>'.$e->getMessage().'</error>');
105+
return 2;
106+
}
107+
}
108+
}

core/Command/SystemTag/Delete.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\TagNotFoundException;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class Delete extends Base {
34+
35+
/** @var ISystemTagManager */
36+
protected $systemTagManager;
37+
38+
/**
39+
* @param ISystemTagManager $systemTagManager
40+
*/
41+
public function __construct(ISystemTagManager $systemTagManager) {
42+
$this->systemTagManager = $systemTagManager;
43+
parent::__construct();
44+
}
45+
46+
protected function configure() {
47+
$this
48+
->setName('tag:delete')
49+
->setDescription('delete a tag')
50+
->addArgument(
51+
'id',
52+
InputOption::VALUE_REQUIRED,
53+
'The ID of the tag that should be deleted',
54+
);
55+
}
56+
57+
protected function execute(InputInterface $input, OutputInterface $output): int {
58+
try {
59+
$this->systemTagManager->deleteTags($input->getArgument('id'));
60+
$output->writeln('<info>The specified tag was deleted</info>');
61+
return 0;
62+
} catch (TagNotFoundException $e) {
63+
$output->writeln('<error>Tag not found</error>');
64+
return 1;
65+
}
66+
}
67+
}

core/Command/SystemTag/Edit.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\TagAlreadyExistsException;
29+
use OCP\SystemTag\TagNotFoundException;
30+
use Symfony\Component\Console\Input\InputInterface;
31+
use Symfony\Component\Console\Input\InputOption;
32+
use Symfony\Component\Console\Output\OutputInterface;
33+
34+
class Edit extends Base {
35+
36+
/** @var ISystemTagManager */
37+
protected $systemTagManager;
38+
39+
/**
40+
* @param ISystemTagManager $systemTagManager
41+
*/
42+
public function __construct(ISystemTagManager $systemTagManager) {
43+
$this->systemTagManager = $systemTagManager;
44+
parent::__construct();
45+
}
46+
47+
protected function configure() {
48+
$this
49+
->setName('tag:edit')
50+
->setDescription('edit tag attributes')
51+
->addArgument(
52+
'id',
53+
InputOption::VALUE_REQUIRED,
54+
'The ID of the tag that should be deleted',
55+
)
56+
->addOption(
57+
'name',
58+
null,
59+
InputOption::VALUE_OPTIONAL,
60+
'sets the \'name\' parameter',
61+
)
62+
->addOption(
63+
'access',
64+
null,
65+
InputOption::VALUE_OPTIONAL,
66+
'sets the access control level (public, restricted, invisible)',
67+
);
68+
}
69+
70+
protected function execute(InputInterface $input, OutputInterface $output): int {
71+
$tag_arr = $this->systemTagManager->getTagsByIds($input->getArgument('id'));
72+
// returns an array, but we always expect 0 or 1 results
73+
74+
if ($tag_arr) {
75+
$tag = array_values($tag_arr)[0];
76+
$name = $tag->getName();
77+
if ($input->getOption('name')) {
78+
$name = $input->getOption('name');
79+
}
80+
81+
$userVisible = $tag->isUserVisible();
82+
$userAssignable = $tag->isUserAssignable();
83+
if ($input->getOption('access')) {
84+
switch ($input->getOption('access')) {
85+
case 'public':
86+
$userVisible = true;
87+
$userAssignable = true;
88+
break;
89+
case 'restricted':
90+
$userVisible = true;
91+
$userAssignable = false;
92+
break;
93+
case 'invisible':
94+
$userVisible = false;
95+
$userAssignable = false;
96+
break;
97+
default:
98+
$output->writeln('<error>`access` property is invalid</error>');
99+
return 1;
100+
}
101+
}
102+
103+
try {
104+
$this->systemTagManager->updateTag($input->getArgument('id'), $name, $userVisible, $userAssignable);
105+
$output->writeln('<info>Tag updated ("' . $name . '", '. $userVisible . ', ' . $userAssignable . ')</info>');
106+
return 0;
107+
} catch (TagNotFoundException $e) {
108+
$output->writeln('<error>Tag not found</error>');
109+
return 1;
110+
} catch (TagAlreadyExistsException $e) {
111+
$output->writeln('<error>'.$e->getMessage().'</error>');
112+
return 2;
113+
}
114+
} else {
115+
$output->writeln('<error>Tag not found</error>');
116+
return 3;
117+
}
118+
}
119+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2021, hosting.de, Johannes Leuker <developers@hosting.de>
4+
*
5+
* @author Johannes Leuker <developers@hosting.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OC\Core\Command\SystemTag;
25+
26+
use OC\Core\Command\Base;
27+
use OCP\SystemTag\ISystemTagManager;
28+
use OCP\SystemTag\ISystemTag;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
class ListCommand extends Base {
34+
35+
/** @var ISystemTagManager */
36+
protected $systemTagManager;
37+
38+
/**
39+
* @param ISystemTagManager $systemTagManager
40+
*/
41+
public function __construct(ISystemTagManager $systemTagManager) {
42+
$this->systemTagManager = $systemTagManager;
43+
parent::__construct();
44+
}
45+
46+
protected function configure() {
47+
$this
48+
->setName('tag:list')
49+
->setDescription('list tags')
50+
->addOption(
51+
'visibilityFilter',
52+
null,
53+
InputOption::VALUE_OPTIONAL,
54+
'filter by visibility (1,0)'
55+
)
56+
->addOption(
57+
'nameSearchPattern',
58+
null,
59+
InputOption::VALUE_OPTIONAL,
60+
'optional search pattern for the tag name (infix)'
61+
)
62+
->addOption(
63+
'output',
64+
null,
65+
InputOption::VALUE_OPTIONAL,
66+
'Output format (plain, json or json_pretty, default is plain)',
67+
$this->defaultOutputFormat
68+
);
69+
}
70+
71+
protected function execute(InputInterface $input, OutputInterface $output): int {
72+
$tags = $this->systemTagManager->getAllTags(
73+
$input->getOption('visibilityFilter'),
74+
$input->getOption('nameSearchPattern')
75+
);
76+
77+
$this->writeArrayInOutputFormat($input, $output, $this->formatTags($tags));
78+
return 0;
79+
}
80+
81+
/**
82+
* @param ISystemtag[] $tags
83+
* @param bool [$detailed=false]
84+
* @return array
85+
*/
86+
private function formatTags(array $tags) {
87+
$result = [];
88+
foreach ($tags as $tag) {
89+
$result[$tag->getId()] = [
90+
'name' => $tag->getName(),
91+
'access' => $tag->getHumanReadableAccess(),
92+
];
93+
}
94+
return $result;
95+
}
96+
}

0 commit comments

Comments
 (0)