Skip to content

Commit 508ecaa

Browse files
authored
added "all" and "showonly" flags to occ app:update (#256)
They do almost the same like the original flags from Server repo with one difference: `--showonly` flag can be specified only with `--all` flag. We can not easy make `--showonly` work for specified appid, cause we support updating ExApps with specifyng `json` or `xml` and not only by `appid`. Signed-off-by: Alexander Piskun <[email protected]>
1 parent c4e56dc commit 508ecaa

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8-
## [2.3.2 - 2024-03-26]
8+
## [2.3.2 - 2024-03-28]
9+
10+
### Added
11+
12+
- `--all` and `--showonly` flags to `occ app_api:app:update` command. #256
913

1014
### Fixed
1115

lib/Command/ExApp/Register.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use OCA\AppAPI\Service\ExAppApiScopeService;
1414
use OCA\AppAPI\Service\ExAppScopesService;
1515
use OCA\AppAPI\Service\ExAppService;
16-
use OCA\AppAPI\Service\ExAppUsersService;
1716

1817
use OCP\IConfig;
1918
use OCP\Security\ISecureRandom;
@@ -33,7 +32,6 @@ public function __construct(
3332
private readonly DaemonConfigService $daemonConfigService,
3433
private readonly ExAppScopesService $exAppScopesService,
3534
private readonly ExAppApiScopeService $exAppApiScopeService,
36-
private readonly ExAppUsersService $exAppUsersService,
3735
private readonly DockerActions $dockerActions,
3836
private readonly ManualActions $manualActions,
3937
private readonly IConfig $config,
@@ -134,7 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134132
$appInfo['port'] = $appInfo['port'] ?? $this->exAppService->getExAppFreePort();
135133
$appInfo['secret'] = $appInfo['secret'] ?? $this->random->generate(128);
136134
$appInfo['daemon_config_name'] = $appInfo['daemon_config_name'] ?? $daemonConfigName;
137-
$appInfo['api_scopes'] = $this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']);
135+
$appInfo['api_scopes'] = array_values($this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']));
138136
$exApp = $this->exAppService->registerExApp($appInfo);
139137
if (!$exApp) {
140138
$this->logger->error(sprintf('Error during registering ExApp %s.', $appId));
@@ -144,8 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
144142
return 3;
145143
}
146144
if (count($appInfo['external-app']['scopes']) > 0) {
147-
if (!$this->exAppScopesService->registerExAppScopes($exApp, $appInfo['api_scopes'])
148-
) {
145+
if (!$this->exAppScopesService->registerExAppScopes($exApp, $this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']))) {
149146
$this->logger->error(sprintf('Error while registering API scopes for %s.', $appId));
150147
if ($outputConsole) {
151148
$output->writeln(sprintf('Error while registering API scopes for %s.', $appId));

lib/Command/ExApp/Update.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use OCA\AppAPI\DeployActions\DockerActions;
99
use OCA\AppAPI\DeployActions\ManualActions;
1010
use OCA\AppAPI\Fetcher\ExAppArchiveFetcher;
11+
use OCA\AppAPI\Fetcher\ExAppFetcher;
1112
use OCA\AppAPI\Service\AppAPIService;
1213
use OCA\AppAPI\Service\DaemonConfigService;
1314
use OCA\AppAPI\Service\ExAppApiScopeService;
@@ -35,6 +36,7 @@ public function __construct(
3536
private readonly ManualActions $manualActions,
3637
private readonly LoggerInterface $logger,
3738
private readonly ExAppArchiveFetcher $exAppArchiveFetcher,
39+
private readonly ExAppFetcher $exAppFetcher,
3840
) {
3941
parent::__construct();
4042
}
@@ -43,19 +45,52 @@ protected function configure(): void {
4345
$this->setName('app_api:app:update');
4446
$this->setDescription('Update ExApp');
4547

46-
$this->addArgument('appid', InputArgument::REQUIRED);
48+
$this->addArgument('appid', InputArgument::OPTIONAL, 'Update the specified app');
4749

4850
$this->addOption('info-xml', null, InputOption::VALUE_REQUIRED, 'Path to ExApp info.xml file (url or local absolute path)');
4951
$this->addOption('json-info', null, InputOption::VALUE_REQUIRED, 'ExApp info.xml in JSON format');
5052
$this->addOption('force-scopes', null, InputOption::VALUE_NONE, 'Force new ExApp scopes approval');
5153
$this->addOption('wait-finish', null, InputOption::VALUE_NONE, 'Wait until finish');
5254
$this->addOption('silent', null, InputOption::VALUE_NONE, 'Do not print to console');
55+
$this->addOption('all', null, InputOption::VALUE_NONE, 'Update all updatable apps');
56+
$this->addOption('showonly', null, InputOption::VALUE_NONE, 'Additional flag for "--all" to only show all updatable apps');
5357
}
5458

5559
protected function execute(InputInterface $input, OutputInterface $output): int {
56-
$outputConsole = !$input->getOption('silent');
5760
$appId = $input->getArgument('appid');
61+
if (empty($appId) && !$input->getOption('all')) {
62+
$output->writeln("<error>Please specify an app to update or \"--all\" to update all updatable apps</error>");
63+
return 1;
64+
} elseif (!empty($appId) && $input->getOption('all')) {
65+
$output->writeln("<error>The \"--all\" flag is mutually exclusive with specifying app</error>");
66+
return 1;
67+
} elseif ($input->getOption('all')) {
68+
$apps = $this->exAppFetcher->get();
69+
$appsWithUpdates = array_filter($apps, function (array $app) {
70+
$exApp = $this->exAppService->getExApp($app['id']);
71+
$newestVersion = $app['releases'][0]['version'];
72+
return $exApp !== null && isset($app['releases'][0]['version']) && version_compare($newestVersion, $exApp->getVersion(), '>');
73+
});
74+
if ($input->getOption('showonly')) {
75+
foreach ($appsWithUpdates as $appWithUpdate) {
76+
$output->writeln($appWithUpdate['id'] . ' new version available: ' . $appWithUpdate['releases'][0]['version']);
77+
}
78+
return 0;
79+
}
80+
$return = 0;
81+
foreach ($appsWithUpdates as $appWithUpdate) {
82+
$result = $this->updateExApp($input, $output, $appWithUpdate['id']);
83+
if ($result > 0) {
84+
$return = $result;
85+
}
86+
}
87+
return $return;
88+
}
89+
return $this->updateExApp($input, $output, $appId);
90+
}
5891

92+
private function updateExApp(InputInterface $input, OutputInterface $output, string $appId): int {
93+
$outputConsole = !$input->getOption('silent');
5994
$appInfo = $this->exAppService->getAppInfo(
6095
$appId, $input->getOption('info-xml'), $input->getOption('json-info')
6196
);
@@ -136,7 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
136171
}
137172
}
138173

139-
$appInfo['api_scopes'] = $this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']);
174+
$appInfo['api_scopes'] = array_values($this->exAppApiScopeService->mapScopeNamesToNumbers($appInfo['external-app']['scopes']));
140175
if (!$this->exAppService->updateExAppInfo($exApp, $appInfo)) {
141176
$this->logger->error(sprintf('Failed to update ExApp %s info', $appId));
142177
if ($outputConsole) {

0 commit comments

Comments
 (0)