|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LinkORB\Component\Etcd\Command; |
| 4 | + |
| 5 | +use LinkORB\Component\Etcd\Client as EtcdClient; |
| 6 | +use LinkORB\Component\Etcd\DirectoryExporter; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | +use Symfony\Component\Filesystem\Filesystem; |
| 13 | +use Symfony\Component\Yaml\Yaml; |
| 14 | + |
| 15 | +class EtcdExportCommand extends Command |
| 16 | +{ |
| 17 | + const PATH_SEPARATOR = '/'; |
| 18 | + |
| 19 | + const FORMAT_JSON = 'json'; |
| 20 | + const FORMAT_YAML = 'yaml'; |
| 21 | + const FORMAT_DOTENV = 'dotenv'; |
| 22 | + |
| 23 | + protected function configure() |
| 24 | + { |
| 25 | + $this |
| 26 | + ->setName('etcd:export') |
| 27 | + ->setDescription( |
| 28 | + 'Export a directory' |
| 29 | + ) |
| 30 | + ->addArgument( |
| 31 | + 'key', |
| 32 | + InputArgument::REQUIRED, |
| 33 | + 'Dir to export' |
| 34 | + )->addOption( |
| 35 | + 'output', |
| 36 | + 'o', |
| 37 | + InputOption::VALUE_REQUIRED, |
| 38 | + 'json file to save dump' |
| 39 | + )->addOption( |
| 40 | + 'format', |
| 41 | + 'f', |
| 42 | + InputOption::VALUE_REQUIRED, |
| 43 | + 'json file to save dump', |
| 44 | + self::FORMAT_JSON |
| 45 | + )->addOption( |
| 46 | + 'server', |
| 47 | + 's', |
| 48 | + InputOption::VALUE_REQUIRED, |
| 49 | + 'Base url of etcd server', |
| 50 | + 'http://127.0.0.1:2379' |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + public function execute(InputInterface $input, OutputInterface $output) |
| 55 | + { |
| 56 | + $key = $input->getArgument('key'); |
| 57 | + $server = $input->getOption('server'); |
| 58 | + |
| 59 | + $client = new EtcdClient($server); |
| 60 | + $dirExporter = new DirectoryExporter($client); |
| 61 | + |
| 62 | + switch ($input->getOption('format')) { |
| 63 | + case self::FORMAT_JSON: |
| 64 | + $result = $this->createJson($dirExporter, $key); |
| 65 | + break; |
| 66 | + case self::FORMAT_YAML: |
| 67 | + $result = $this->createYaml($dirExporter, $key); |
| 68 | + break; |
| 69 | + case self::FORMAT_DOTENV: |
| 70 | + $result = $this->createDotEnv($dirExporter, $key); |
| 71 | + break; |
| 72 | + default: |
| 73 | + throw new \RuntimeException('Unknown format: ' . $input->getOption('format')); |
| 74 | + } |
| 75 | + |
| 76 | + $file = $input->getOption('output'); |
| 77 | + if (!is_null($file)) { |
| 78 | + $fs = new Filesystem(); |
| 79 | + $fs->dumpFile($file, $result . PHP_EOL); |
| 80 | + } else { |
| 81 | + $output->writeln($result); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + private function createJson(DirectoryExporter $directoryExporter, $key) |
| 87 | + { |
| 88 | + $data = $directoryExporter->exportArray($key); |
| 89 | + return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + private function createYaml(DirectoryExporter $directoryExporter, $key) |
| 94 | + { |
| 95 | + $data = $directoryExporter->exportArray($key); |
| 96 | + return Yaml::dump($data); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + private function createDotEnv(DirectoryExporter $directoryExporter, $key) |
| 101 | + { |
| 102 | + $data = $directoryExporter->exportKeyValuePairs($key, true); |
| 103 | + $result = []; |
| 104 | + foreach ($data as $k => $v) { |
| 105 | + $result[] = strtoupper(str_replace("/", "_", $k)) . '=' . $v; |
| 106 | + } |
| 107 | + return implode(PHP_EOL, $result); |
| 108 | + } |
| 109 | +} |
0 commit comments