| 28 | | final class RunCommand extends StewartCommand |
| 29 | | { |
| 30 | | public const string NAME = 'run'; |
| 31 | | |
| 32 | | |
| 33 | | public const int RESTART_EXIT_CODE = 75; |
| 34 | | |
| 35 | | public function __construct( |
| 36 | | private readonly ConfigLoader $config, |
| 37 | | private readonly AppDiscovery $discovery, |
| 38 | | private readonly AppInstanceResolver $instances, |
| 39 | | private readonly AppCatalogResolver $catalogs, |
| 40 | | private readonly BrokerKernel $brokers, |
| 41 | | private readonly LoggerFactory $loggers, |
| 42 | | private readonly CatalogWarnings $catalogWarnings, |
| 43 | | ) { |
| 44 | | parent::__construct(); |
| 45 | | } |
| 46 | | |
| 47 | | protected function configure(): void |
| 48 | | { |
| 49 | | parent::configure(); |
| 50 | | $this |
| 51 | | ->addOption('log-level', 'l', InputOption::VALUE_REQUIRED, 'debug|info|notice|warning|error') |
| 52 | | ->addOption('json-logs', null, InputOption::VALUE_NONE, 'Emit logs as JSON') |
| 53 | | ->addOption('only', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Run only this automation, even if disabled; repeat for more') |
| 54 | | ->addOption('trace', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Log TRACE lines for this automation at any log level; repeat for more') |
| 55 | | ->addOption('watch', null, InputOption::VALUE_NONE, 'Reload after every saved change to the apps, services.php or stewart.yaml') |
| 56 | | ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Log actions, event fires and registry edits instead of sending them (ha_calls.dry_run)'); |
| 57 | | } |
| 58 | | |
| 59 | | protected function execute(InputInterface $input, OutputInterface $output): int |
| 60 | | { |
| 61 | | try { |
| 62 | | $daemon = $this->prepareDaemon($input); |
| 63 | | } catch (StewartException $e) { |
| 64 | | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 65 | | |
| 66 | | return Command::FAILURE; |
| 67 | | } |
| 68 | | |
| 69 | | try { |
| 70 | | $outcome = $daemon->broker->run(); |
| 71 | | } catch (Throwable $e) { |
| 72 | | $daemon->logger->error('Stewart stopped', ['exception' => $e]); |
| 73 | | |
| 74 | | return Command::FAILURE; |
| 75 | | } |
| 76 | | |
| 77 | | return match ($outcome) { |
| 78 | | BrokerStopOutcome::Stopped => Command::SUCCESS, |
| 79 | | BrokerStopOutcome::RestartRequested => self::RESTART_EXIT_CODE, |
| 80 | | }; |
| 81 | | } |
| 82 | | |
| 83 | | |
| 84 | | private function prepareDaemon(InputInterface $input): PreparedDaemon |
| 85 | | { |
| 86 | | $launch = new DaemonLaunchOptions( |
| 87 | | $this->findConfigOption($input), |
| 88 | | $this->readAppSelectionOption($input), |
| 89 | | $this->nonEmptyStringOption($input, 'log-level'), |
| 90 | | $input->getOption('watch') === true, |
| 91 | | $input->getOption('dry-run') === true, |
| 92 | | ); |
| 93 | | $config = $this->config->loadConfig($launch->configFile, $launch->listConfigOverrides()); |
| 94 | | |
| 95 | | $config->requireHomeAssistant(); |
| 96 | | $discovered = $this->instances->resolveInstances($this->discovery->discover(), $config->apps); |
| 97 | | $selection = $launch->selection; |
| 98 | | $apps = $this->catalogs->resolveCatalog($discovered, $config->apps, $config->workers, $selection); |
| 99 | | $verbosity = new LogVerbosityPolicy($config->logLevel, $apps->enabled->listVerbositiesByAppId()); |
| 100 | | $logger = $this->loggers->createDaemonLogger($verbosity, $this->logFormatFromOptions($input, $config->logFormat)); |
| 101 | | |
| 102 | | $this->noteMissingConfigFile($this->config->findMissingDefaultFile($launch->configFile), $logger); |
| 103 | | $this->catalogWarnings->logCatalogWarnings($discovered, $apps, $this->config->locateConfigFile($launch->configFile), $logger); |
| 104 | | |
| 105 | | if ($config->haCalls->dryRun) { |
| 106 | | $logger->warning('ha_calls.dry_run is on: actions, event fires and registry edits are logged, not sent to Home Assistant.'); |
| 107 | | } |
| 108 | | |
| 109 | | if ($selection->onlyIds !== null) { |
| 110 | | $logger->notice('Running only the automations named by --only.', ['apps' => $selection->onlyIds->toStrings()]); |
| 111 | | } |
| 112 | | |
| 113 | | if ($selection->tracedIds !== null) { |
| 114 | | $logger->notice('Tracing the automations named by --trace.', ['apps' => $selection->tracedIds->toStrings()]); |
| 115 | | } |
| 116 | | |
| 117 | | return new PreparedDaemon($this->brokers->createBroker($config, $apps, $logger, $verbosity, $launch), $logger); |
| 118 | | } |
| 119 | | |
| 120 | | private function noteMissingConfigFile(?string $missingConfigFile, LoggerInterface $logger): void |
| 121 | | { |
| 122 | | if ($missingConfigFile !== null) { |
| 123 | | $logger->info('No config file found; using the environment only.', ['config' => $missingConfigFile]); |
| 124 | | } |
| 125 | | } |
| 126 | | |
| 127 | | private function logFormatFromOptions(InputInterface $input, LogFormat $configured): LogFormat |
| 128 | | { |
| 129 | | return $input->getOption('json-logs') === true ? LogFormat::Json : $configured; |
| 130 | | } |
| 131 | | } |