public function _openConnection($environment) { $config = new Garp_Deploy_Config(); $params = $config->getParams($environment); if (!$params) { Garp_Cli::errorOut('No settings found for environment ' . $environment); } if (empty($params['server'])) { Garp_Cli::errorOut("'server' is a required setting."); return false; } // To provide a bit of backward-compatibility, convert to array if (!is_array($params['server'])) { $params['server'] = array(array('server' => $params['server'], 'user' => $params['user'])); } if (count($params['server']) === 1) { $this->_executeSshCommand($params['server'][0]['server'], $params['server'][0]['user']); return true; } $choice = Garp_Cli::prompt("Choose a server to use: \n" . array_reduce($params['server'], function ($output, $server) { $number = count(explode("\n", $output)); $output .= "{$number}) {$server['server']}\n"; return $output; }, '')); if (!array_key_exists($choice - 1, $params['server'])) { Garp_Cli::errorOut('Invalid choice: ' . $choice); return false; } $this->_executeSshCommand($params['server'][$choice - 1]['server'], $params['server'][$choice - 1]['user']); }
/** * Walks over every text column of every record of every table * and replaces references to $subject with $replacement. * Especially useful since all images in Rich Text Editors are * referenced with absolute paths including the domain. This method * can be used to replace "old domain" with "new domain" in one go. * * @param array $args * @return bool */ public function replace(array $args = array()) { $subject = !empty($args[0]) ? $args[0] : Garp_Cli::prompt('What is the string you wish to replace?'); $replacement = !empty($args[1]) ? $args[1] : Garp_Cli::prompt('What is the new string you wish to insert?'); $subject = trim($subject); $replacement = trim($replacement); $models = Garp_Content_Api::getAllModels(); foreach ($models as $model) { if (is_subclass_of($model->class, 'Garp_Model_Db')) { $this->_replaceString($model->class, $subject, $replacement); } } return true; }
public function make($args = array()) { $mem = new Garp_Util_Memory(); $mem->useHighMemory(); // @todo Superduperbonusmode: would be cool if you could go back in time and generate a // gumball for a given semver (using Git to grab the correct tag). // There would be no way to include that moment's data though. $version = new Garp_Semver(); Garp_Cli::lineOut('Creating gumball ' . $version, Garp_Cli::PURPLE); $fromEnv = null; if ($useDb = Garp_Cli::confirm(self::PROMPT_INCLUDE_DATABASE)) { $fromEnv = Garp_Cli::prompt(self::PROMPT_SOURCE_DATABASE_ENVIRONMENT) ?: self::DEFAULT_SOURCE_DATABASE_ENVIRONMENT; } $gumball = new Garp_Gumball($version, array('useDatabase' => $useDb, 'databaseSourceEnvironment' => $fromEnv)); if ($gumball->exists() && !Garp_Cli::confirm(sprintf(self::PROMPT_OVERWRITE, $version))) { Garp_Cli::lineOut(self::ABORT_NO_OVERWRITE, Garp_Cli::YELLOW); return false; } $gumball->exists() && $gumball->remove(); if (!$this->_createGumballDirectory()) { Garp_Cli::errorOut(self::ABORT_CANT_MKDIR_GUMBALLS); return false; } try { $gumball->make(); } catch (Garp_Gumball_Exception_CannotWriteTargetDirectory $e) { Garp_Cli::errorOut(self::ABORT_CANT_MKDIR_TARGET_DIRECTORY); return false; } catch (Garp_Gumball_Exception_CannotCopySourceFiles $e) { Garp_Cli::errorOut(self::ABORT_CANT_COPY_SOURCEFILES); return false; } catch (Garp_Gumball_Exception_CannotCreateZip $e) { Garp_Cli::errorOut(self::ABORT_CANT_WRITE_ZIP); return false; } catch (Garp_Gumball_Exception_DatadumpFailed $e) { Garp_Cli::errorOut(self::ABORT_DATADUMP_FAILED); return false; } return true; }
/** * Create snippet interactively * * @return bool */ protected function _createInteractive() { Garp_Cli::lineOut('Please provide the following values'); $data = array('identifier' => Garp_Cli::prompt('Identifier')); if ($snippet = $this->_fetchExisting($data['identifier'])) { Garp_Cli::lineOut('Snippet already exists. Id: #' . $snippet->id, Garp_Cli::GREEN); return true; } $data['uri'] = Garp_Cli::prompt('Url'); $checks = array(array('has_name', 'Does this snippet have a name?', 'y'), array('has_html', 'Does this snippet contain HTML?', 'y'), array('has_text', 'Does this snippet contain text?', 'n'), array('has_image', 'Does this snippet have an image?', 'n')); foreach ($checks as $check) { $key = $check[0]; $question = $check[1]; $default = $check[2]; if ($default == 'y') { $question .= ' Yn'; } elseif ($default == 'n') { $question .= ' yN'; } $userResponse = trim(Garp_Cli::prompt($question)); if (!$userResponse) { $userResponse = $default; } if (strtolower($userResponse) == 'y' || $userResponse == 1) { $data[$key] = 1; } elseif (strtolower($userResponse) == 'n' || $userResponse == 0) { $data[$key] = 0; } } $snippet = $this->_create($data); Garp_Cli::lineOut('New snippet inserted. Id: #' . $snippet->id . ', Identifier: "' . $snippet->identifier . '"'); return true; }
protected function _getInsertionMode() { $query = 'Want to create a record (i)nteractively or shall I just ' . 'insert a bunch of (g)ibberish?'; $response = Garp_Cli::prompt($query); if (in_array($response, array('i', 'g'))) { return $response; } Garp_Cli::lineOut('Please answer "i" or "g"'); return $this->_getInsertionMode(); }
/** * Retrieve input from the user. * Prefer readline because it supports history. * * @return String */ protected function _getInput() { if (function_exists('readline') && function_exists('readline_add_history')) { $line = readline($this->_getPrompt()); readline_add_history($line); return $line; } echo $this->_getPrompt(); $line = Garp_Cli::prompt(); return $line; }
/** * Make an existing user admin * * @param array $args * @return void */ public function make(array $args = array()) { $userModel = new Model_User(); if (!empty($args)) { $id = $args[0]; } else { $id = Garp_Cli::prompt('What is the id or email address of the user?'); } $select = $userModel->select(); if (is_numeric($id)) { $filterColumn = 'id'; } else { $filterColumn = 'email'; } $select->where($filterColumn . ' = ?', $id); $user = $userModel->fetchRow($select); if (!$user) { Garp_Cli::errorOut('Error: could not find user with ' . $filterColumn . ' ' . $id); } else { $user->role = 'admin'; if ($user->save()) { // For completeness sake, check if the user has an AuthLocal // record. We disregard the fact wether the user already has any // of the other Auth- records. $authLocalModel = new Model_AuthLocal(); $authLocalRecord = $authLocalModel->fetchRow($authLocalModel->select()->where('user_id = ?', $user->id)); if (!$authLocalRecord) { $newAuthLocalData = array('password' => trim(Garp_Cli::prompt('Choose a password:'******'user_id' => $user->id); $authLocalModel->insert($newAuthLocalData); } Garp_Cli::lineOut('User with ' . $filterColumn . ' ' . $id . ' is now administrator'); } else { Garp_Cli::errorOut('Error: could not make user with ' . $filterColumn . ' ' . $id . ' administrator'); } } }