info('Autowiring the application...'); $printers = $manager->getList()->map(fn(Printer $printer) => [ 'name' => $printer->getName(), 'status' => $printer->getStatus(), 'attributes' => $printer->getAttributes(), 'uri' => $printer->getUri(), ]); try { if ($this->option('init') && !file_exists($this->getYamlFilename())) { $response = Http::acceptJson()->post('https://events.anikeen.com/api/printers/autoinit', [ 'hostname' => gethostname(), ]); if ($response->successful()) { $this->setConfig($response->json()); } else { $this->warn('Unable to auto initialize configuration! Please use `print-cli init` to create a configuration file.'); } } $contig = $this->getConfig(expectedVersion: 2); $response = Http::acceptJson()->patch(sprintf('%s/api/printers/autowire', $contig->getBaseUrl()), [ 'id' => $contig->getId(), 'hostname' => gethostname(), 'printers' => $printers->toArray(), ]); if ($response->successful()) { $newPrinters = $response->json('printers', []); if (empty($newPrinters)) { $this->info('No new printers found to autowire.'); } else { foreach ($newPrinters as $printer) { $this->info(sprintf('Printer: %s', $printer['uri'])); } } $this->setConfig([ ...$contig->toArray(), 'printers' => $newPrinters, ]); } else { throw new Exception('Failed to autowire printers: ' . $response->body()); } } catch (Throwable $exception) { $this->error('Failed to autowire printers: ' . $exception->getMessage()); return self::FAILURE; } $this->info('Autowiring completed successfully.'); return self::SUCCESS; } }