Example #1
0
 /**
  * @inheritdoc
  */
 public function call($args, $flags)
 {
     // Check version and operations on remote server
     if (in_array('-c', $flags)) {
         $client = self::loadClient($args);
         \cli\line('%_Base URI:%n ' . $client->getBaseURI());
         \cli\line('%_Version:%n ' . $client->serverVersion());
         \cli\line('%_Operations:%n');
         $table = new \cli\Table();
         $table->setHeaders(array('Class', 'Methods', 'Path'));
         foreach ($client->request('GET', 'operations')->body as $data) {
             $table->addRow(array_values((array) $data));
         }
         $table->display();
     } elseif (isset($args['-r'])) {
         $servers = $this->loadStoredServerInfo();
         if (isset($servers[$args['-r']])) {
             unset($servers[$args['-r']]);
             file_put_contents($this->infoFile, serialize($servers));
             \cli\line('Server removed (add flag -l to list servers)');
         }
     } elseif (in_array('-l', $flags)) {
         $servers = $this->loadStoredServerInfo();
         $this->listServers($servers);
     } elseif (isset($args['-d'])) {
         $servers = $this->loadStoredServerInfo();
         if (empty($servers[$args['-d']])) {
             \cli\line('Server does not exist....');
             $this->listServers($servers);
         } else {
             $servers['__default'] = $args['-d'];
             file_put_contents($this->infoFile, serialize($servers));
             \cli\line('%gServer "' . $args['-d'] . '" set as default %n');
         }
     } else {
         \cli\line('Add remote server');
         \cli\line('--------------------');
         $name = \cli\prompt('Server name (any name of your choice)');
         $address = rtrim(\cli\prompt('Server address'), '/') . '/';
         $user = \cli\prompt('Admin e-mail');
         $pass = Utils::promptPassword('Admin password: '******'Secret (leave empty if not used)'));
         try {
             // Try to request server
             $auth = $user . ':' . $pass;
             if ($secret) {
                 $auth = 'RC4 ' . base64_encode(RC4Cipher::encrypt($secret, $auth));
             } else {
                 $auth = 'Basic ' . base64_encode($auth);
             }
             $client = new Client($address);
             $client->setAuthString($auth);
             $user = $client->me();
             // just to check that auth is correct
             if (!$user) {
                 throw new \Exception('Could not authenticate');
             }
             $version = $client->serverVersion();
             $this->addServer($name, $address, $auth);
             \cli\line('%gSuccessfully added server "' . $name . '" (v' . $version . ')%n');
             \cli\line('... add flag -l to list all added servers');
         } catch (\Exception $e) {
             \cli\err('Failed adding server with message "' . $e->getMessage() . '"');
         }
     }
 }