This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathProjectInfoCommand.php
More file actions
181 lines (150 loc) · 6.06 KB
/
Copy pathProjectInfoCommand.php
File metadata and controls
181 lines (150 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
namespace Platformsh\Cli\Command\Project;
use Platformsh\Cli\Selector\Selector;
use Platformsh\Cli\Service\ActivityMonitor;
use Platformsh\Cli\Service\Api;
use Platformsh\Cli\Command\CommandBase;
use Platformsh\Cli\Console\AdaptiveTableCell;
use Platformsh\Cli\Service\PropertyFormatter;
use Platformsh\Cli\Service\Table;
use Platformsh\Client\Model\Project;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'project:info', description: 'Read or set properties for a project')]
class ProjectInfoCommand extends CommandBase
{
public function __construct(private readonly ActivityMonitor $activityMonitor, private readonly Api $api, private readonly PropertyFormatter $propertyFormatter, private readonly Selector $selector, private readonly Table $table)
{
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('property', InputArgument::OPTIONAL, 'The name of the property')
->addArgument('value', InputArgument::OPTIONAL, 'Set a new value for the property')
->addOption('refresh', null, InputOption::VALUE_NONE, 'Whether to refresh the cache');
PropertyFormatter::configureInput($this->getDefinition());
Table::configureInput($this->getDefinition());
$this->selector->addProjectOption($this->getDefinition());
$this->addCompleter($this->selector);
$this->activityMonitor->addWaitOptions($this->getDefinition());
$this->addExample('Read all project properties')
->addExample("Show the project's Git URL", 'git')
->addExample("Change the project's title", 'title "My project"');
$this->setHiddenAliases(['project:metadata']);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$selection = $this->selector->getSelection($input);
$project = $selection->getProject();
if ($input->getOption('refresh')) {
$project->refresh();
}
$property = $input->getArgument('property');
// Setting the pseudo-properties 'git' and 'url', and un-setting the
// property 'entropy', are done twice in this command so that
// lazy-loading still works.
if (!$property) {
$properties = $project->getProperties();
$properties['git'] = $project->getGitUrl();
$properties['url'] = $project->getUri();
unset($properties['entropy']);
return $this->listProperties($properties);
}
$value = $input->getArgument('value');
if ($value !== null) {
return $this->setProperty($property, $value, $project, !$this->activityMonitor->shouldWait($input));
}
switch ($property) {
case 'git':
$value = $project->getGitUrl();
break;
case 'url':
$value = $project->getUri();
break;
case 'entropy':
throw new \InvalidArgumentException('Property not found: ' . $property);
default:
$value = $this->api->getNestedProperty($project, $property);
}
$output->writeln($this->propertyFormatter->format($value, $property));
return 0;
}
/**
* @param array<string, mixed> $properties
*
* @return int
*/
protected function listProperties(array $properties): int
{
$headings = [];
$values = [];
foreach ($properties as $key => $value) {
$headings[] = new AdaptiveTableCell($key, ['wrap' => false]);
$values[] = $this->propertyFormatter->format($value, $key);
}
$this->table->renderSimple($values, $headings);
return 0;
}
protected function setProperty(string $property, string $value, Project $project, bool $noWait): int
{
$isMap = str_contains($property, '.');
if ($isMap) {
[$mapName, $mapKey] = explode('.', $property, 2);
$type = $this->getType($mapName . '.*');
$currentMap = $project->getProperty($mapName) ?? [];
$currentValue = $currentMap[$mapKey] ?? null;
$update = [$mapName => [$mapKey => $value]];
} else {
$type = $this->getType($property);
$currentValue = $project->getProperty($property);
$update = [$property => $value];
}
if (!$type) {
$this->stdErr->writeln("Property not writable: <error>$property</error>");
return 1;
}
if ($type === 'boolean' && $value === 'false') {
$value = false;
}
settype($value, $type);
if ($currentValue === $value) {
$this->stdErr->writeln(
"Property <info>$property</info> already set as: " . $this->propertyFormatter->format($value, $property),
);
return 0;
}
$project->ensureFull();
$result = $project->update($update);
$this->stdErr->writeln(sprintf(
'Property <info>%s</info> set to: %s',
$property,
$this->propertyFormatter->format($value, $property),
));
$this->api->clearProjectsCache();
$success = true;
if (!$noWait) {
$activityMonitor = $this->activityMonitor;
$success = $activityMonitor->waitMultiple($result->getActivities(), $project);
}
return $success ? 0 : 1;
}
/**
* Gets the type of a writable property.
*/
protected function getType(string $property): string|false
{
$writableProperties = [
'title' => 'string',
'description' => 'string',
'default_domain' => 'string',
'default_branch' => 'string',
'attributes.*' => 'string',
];
return $writableProperties[$property] ?? false;
}
}