| 22 | | final class CheckCommand extends StewartCommand |
| 23 | | { |
| 24 | | public const string NAME = 'check'; |
| 25 | | |
| 26 | | public function __construct( |
| 27 | | private readonly ConfigLoader $config, |
| 28 | | private readonly ProjectCheck $projects, |
| 29 | | ) { |
| 30 | | parent::__construct(); |
| 31 | | } |
| 32 | | |
| 33 | | protected function execute(InputInterface $input, OutputInterface $output): int |
| 34 | | { |
| 35 | | $configFile = $this->findConfigOption($input); |
| 36 | | $result = $this->projects->checkProject($configFile); |
| 37 | | |
| 38 | | if (!$result->hasPassed()) { |
| 39 | | $errors = $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output; |
| 40 | | |
| 41 | | foreach ($result->errors as $error) { |
| 42 | | $errors->writeln('<error>' . $error . '</error>'); |
| 43 | | } |
| 44 | | |
| 45 | | return Command::FAILURE; |
| 46 | | } |
| 47 | | |
| 48 | | $discovered = $result->discovered; |
| 49 | | $apps = $result->catalog; |
| 50 | | |
| 51 | | $this->writeMissingConfigFileNotice($output, $this->config->findMissingDefaultFile($configFile)); |
| 52 | | $this->writeHttpListenerNotice($output, $result->httpListenerError); |
| 53 | | $this->writeUnmatchedOverridesNotice($output, $apps); |
| 54 | | $this->writeUnmarkedAppNotices($output, $discovered->unmarked); |
| 55 | | $this->writeUnusedTemplateNotices($output, $discovered->listUnusedTemplateClasses()); |
| 56 | | $this->writeAppTable($output, $discovered, $apps, $result->workerSlots); |
| 57 | | $output->writeln(\sprintf('Configuration and %d automations load; %d enabled.', $discovered->apps->count(), $apps->enabled->count())); |
| 58 | | |
| 59 | | return Command::SUCCESS; |
| 60 | | } |
| 61 | | |
| 62 | | private function writeHttpListenerNotice(OutputInterface $output, ?string $error): void |
| 63 | | { |
| 64 | | if ($error !== null) { |
| 65 | | $this->writeNotice($output, $error . ' The daemon refuses to start until it is fixed.'); |
| 66 | | } |
| 67 | | } |
| 68 | | |
| 69 | | private function writeUnmarkedAppNotices(OutputInterface $output, UnmarkedAppCollection $unmarked): void |
| 70 | | { |
| 71 | | foreach ($unmarked as $app) { |
| 72 | | $this->writeNotice($output, \sprintf('%s implements App but is not marked #[Automation] or #[AppTemplate], so it will not run.', $app->class)); |
| 73 | | } |
| 74 | | } |
| 75 | | |
| 76 | | |
| 77 | | private function writeUnusedTemplateNotices(OutputInterface $output, array $templates): void |
| 78 | | { |
| 79 | | foreach ($templates as $template) { |
| 80 | | $this->writeNotice($output, \sprintf('%s is an #[AppTemplate], but no apps.<id>.class names it, so it does not run.', $template)); |
| 81 | | } |
| 82 | | } |
| 83 | | |
| 84 | | private function writeUnmatchedOverridesNotice(OutputInterface $output, AppCatalog $apps): void |
| 85 | | { |
| 86 | | if (!$apps->unmatchedOverrideIds->isEmpty()) { |
| 87 | | $this->writeNotice($output, \sprintf( |
| 88 | | 'stewart.yaml or the environment configures automations that were not found, so their settings are ignored: %s. Known automations: %s.', |
| 89 | | implode(', ', $apps->unmatchedOverrideIds->toStrings()), |
| 90 | | $apps->knownIds->isEmpty() ? 'none' : implode(', ', $apps->knownIds->toStrings()), |
| 91 | | )); |
| 92 | | } |
| 93 | | } |
| 94 | | |
| 95 | | |
| 96 | | private function writeAppTable(OutputInterface $output, DiscoveryResult $discovered, AppCatalog $apps, WorkerSlotCollection $slots): void |
| 97 | | { |
| 98 | | $rows = $discovered->apps->mapToList(static function (DiscoveredApp $app) use ($apps, $slots): array { |
| 99 | | $definition = $apps->enabled->find($app->id); |
| 100 | | $state = $definition === null ? 'disabled' : ($definition->startsPaused ? 'paused' : 'enabled'); |
| 101 | | |
| 102 | | return [$app->id->value, $app->class, (string) ($slots->findWorkerOfApp($app->id) ?? '-'), $state]; |
| 103 | | }); |
| 104 | | |
| 105 | | foreach ($discovered->listUnusedTemplateClasses() as $template) { |
| 106 | | $rows[] = ['-', $template, '-', 'no entry']; |
| 107 | | } |
| 108 | | |
| 109 | | usort($rows, static fn(array $a, array $b): int => [$a[1], $a[0]] <=> [$b[1], $b[0]]); |
| 110 | | |
| 111 | | if ($rows !== []) { |
| 112 | | new Table($output)->setStyle('compact')->setHeaders(['app', 'class', 'worker', 'state'])->setRows($rows)->render(); |
| 113 | | } |
| 114 | | } |
| 115 | | } |