/** * Show all sites user has access to * Note: because of the size of this call, it is cached * and also is the basis for loading individual sites by name * * [--team] * : filter sites you are a team member of * * [--org=<id>] * : filter sites you can access via the organization * * @subcommand list * @alias show */ public function index($args, $assoc_args) { // Always fetch a fresh list of sites $this->sitesCache->rebuild(); $cached_sites = $this->sitesCache->all(); $rows = array_map(function ($cached_site) { return array('name' => $cached_site['name'], 'id' => $cached_site['id'], 'service_level' => $cached_site['service_level'], 'framework' => $cached_site['framework'], 'created' => date('Y-m-d H:i:s', $cached_site['created']), 'memberships' => array_map(function ($membership) { return $membership['name']; }, array_values($cached_site['memberships']))); }, array_values($cached_sites)); if (isset($assoc_args['team'])) { $rows = array_filter($rows, function ($site) { return in_array('Team', $site['memberships']); }); } if (isset($assoc_args['org'])) { $org_id = $assoc_args['org']; $rows = array_filter($rows, function ($site) use($org_id) { $org_ids = array_keys($site['memberships']); return in_array($org_id, $org_ids); }); } if (count($rows) == 0) { Terminus::log("You have no sites."); exit(0); } $this->handleDisplay($rows); }
/** * Show all sites user has access to * Note: because of the size of this call, it is cached * and also is the basis for loading individual sites by name * * [--team] * : filter sites you are a team member of * * [--org=<id>] * : filter sites you can access via the organization * * @subcommand list * @alias show */ public function index($args, $assoc_args) { // Always fetch a fresh list of sites $this->sites->rebuildCache(); $sites = $this->sites->all(); $rows = array(); foreach ($sites as $site) { $memberships = array(); foreach ($site->get('memberships') as $membership) { $memberships[$membership['id']] = $membership['name']; } $rows[$site->get('id')] = array('name' => $site->get('name'), 'id' => $site->get('id'), 'service_level' => $site->get('service_level'), 'framework' => $site->get('framework'), 'created' => date('Y-m-d H:i:s', $site->get('created')), 'memberships' => $memberships); } usort($rows, function ($row_1, $row_2) { return strcasecmp($row_1['name'], $row_2['name']); }); if (isset($assoc_args['team'])) { $rows = array_filter($rows, function ($site) { return in_array('Team', $site['memberships']); }); } if (isset($assoc_args['org'])) { $org_id = $assoc_args['org']; $rows = array_filter($rows, function ($site) use($org_id) { return isset($org_ids[$org_id]) || in_array($org_id, $site['memberships']); }); } if (count($rows) == 0) { Terminus::log('You have no sites.'); } $labels = ['name' => 'Name', 'id', 'ID', 'service_level', 'Service Level', 'framework' => 'Framework', 'created' => 'Created', 'memberships' => 'Memberships']; $this->outputter->outputRecordList($rows, $labels); }
/** * Make a request to the Pantheon API * * @param [string] $realm Permissions realm for data request (e.g. user, * site organization, etc. Can also be "public" to simply pull read-only * data that is not privileged. * @param [string] $uuid The UUID of the item in the realm to access * @param [string] $path API path (URL) * @param [string] $method HTTP method to use * @param [mixed] $options A native PHP data structure (e.g. int, string, * array, or stdClass) to be sent along with the request * @return [array] $data */ public static function request($realm, $uuid, $path = false, $method = 'GET', $options = null) { if (!in_array($realm, array('login', 'user', 'public'))) { Auth::loggedIn(); } try { $cache = Terminus::get_cache(); if (!in_array($realm, array('login', 'user'))) { $options['cookies'] = array('X-Pantheon-Session' => Session::getValue('session')); $options['verify'] = false; } $url = Endpoint::get(array('realm' => $realm, 'uuid' => $uuid, 'path' => $path)); if (Terminus::get_config('debug')) { Terminus::log('debug', 'Request URL: ' . $url); } $resp = Request::send($url, $method, $options); $json = $resp->getBody(true); $data = array('info' => $resp->getInfo(), 'headers' => $resp->getRawHeaders(), 'json' => $json, 'data' => json_decode($json), 'status_code' => $resp->getStatusCode()); return $data; } catch (Guzzle\Http\Exception\BadResponseException $e) { $response = $e->getResponse(); throw new TerminusException($response->getBody(true)); } catch (Guzzle\Http\Exception\HttpException $e) { $request = $e->getRequest(); $sanitized_request = TerminusCommand::stripSensitiveData((string) $request, TerminusCommand::$blacklist); throw new TerminusException('API Request Error. {msg} - Request: {req}', array('req' => $sanitized_request, 'msg' => $e->getMessage())); } catch (Exception $e) { throw new TerminusException('API Request Error: {msg}', array('msg' => $e->getMessage())); } }
/** * Actually go out and get the sites. */ protected function _fetch_sites() { Terminus::log('Fetching site list from Pantheon'); $request = self::request('user', Session::getValue('user_uuid'), 'sites', 'GET', array('hydrated' => true)); # TODO: handle errors well. $sites = $request['data']; $this->cache->put_data('sites', $sites); $this->sites = $sites; return $sites; }
static function get_runner() { // the entire process needs an exception wrapper try { static $runner; if (!isset($runner) || !$runner) { $runner = new Runner(); } return $runner; } catch (\Exception $e) { Terminus::log('error', $e->getMessage()); exit(1); } }
/** * Show all sites user has access to * Note: because of the size of this call, it is cached * and also is the basis for loading individual sites by name * * @subcommand list * @alias show */ public function index($args, $assoc_args) { // Always fetch a fresh list of sites $this->sitesCache->rebuild(); $cached_sites = $this->sitesCache->all(); if (count($cached_sites) == 0) { Terminus::log("You have no sites."); exit(0); } $rows = array_map(function ($cached_site) { return array('name' => $cached_site['name'], 'id' => $cached_site['id'], 'service_level' => $cached_site['service_level'], 'framework' => $cached_site['framework'], 'memberships' => array_map(function ($membership) { return $membership['name']; }, $cached_site['memberships'])); }, $cached_sites); $this->handleDisplay($rows); return $rows; }
public static function send($url, $method, $data = array()) { // create a new Guzzle\Http\Client $browser = new Browser(); $browser->setUserAgent(self::userAgent()); $options = array('allow_redirects' => false, 'verify' => false, 'json' => false); if (isset($data['allow_redirects'])) { $options['allow_redirects'] = $data['allow_redirects']; } if (isset($data['json'])) { $options['json'] = $data['json']; } if (isset($data['body']) && $data['body']) { $options['body'] = $data['body']; if (\Terminus::get_config('debug')) { \Terminus::log('debug', $data['body']); } } $request = $browser->createRequest($method, $url, null, null, $options); if (!empty($data['postdata'])) { foreach ($data['postdata'] as $k => $v) { $request->setPostField($k, $v); } } if (!empty($data['cookies'])) { foreach ($data['cookies'] as $k => $v) { $request->addCookie($k, $v); } } if (!empty($data['headers'])) { foreach ($data['headers'] as $k => $v) { $request->setHeader($k, $v); } } if (\Terminus::get_config("debug")) { $debug = "#### REQUEST ####" . PHP_EOL; $debug .= $request->getRawHeaders(); \Terminus::log('debug', $debug); if (isset($data['body'])) { \Terminus::log('debug', $data['body']); } } $response = $request->send(); return $response; }
public function invalid_positionals($args) { $positionals = $this->query_spec(array('type' => 'positional')); for ($i = 0; $i < count($args); $i++) { if (!isset($positionals[$i]['token'])) { continue; } $token = preg_replace('#\\[?\\<([a-zA-Z].*)\\>\\]?.*#s', '$1', $positionals[$i]['token']); if ("commands" == trim($token) || "email" == trim($token)) { // we exit here because the wp and drush commands need to not have validation running since their commands are dependent on their respective code bases. return false; } $regex = "#^({$token})\$#s"; \Terminus::log('debug', "Positional match {$regex}"); if (!preg_match($regex, $args[$i])) { return $args[$i]; } } return false; }
/** * @return array list of invalid $assoc_args keys to unset */ private function validate_args($args, $assoc_args, $extra_args) { $synopsis = $this->get_synopsis(); if (!$synopsis) { return array(); } $validator = new \Terminus\SynopsisValidator($synopsis); $cmd_path = implode(' ', get_path($this)); foreach ($validator->get_unknown() as $token) { \Terminus::log('warning', "The `{cmd}` command has an invalid synopsis part: {token}", array('cmd' => $cmd_path, 'token' => $token)); } if (!$validator->enough_positionals($args)) { $this->show_usage(); exit(1); } if ($this->name != 'help') { $invalid = $validator->invalid_positionals($args); if ($invalid) { throw new TerminusException("Invalid positional value: {invalid}", array('invalid' => $invalid)); } } $unknown_positionals = $validator->unknown_positionals($args); if (!empty($unknown_positionals)) { throw new TerminusException('Too many positional arguments: {args}', array('args' => implode(' ', $unknown_positionals))); } list($errors, $to_unset) = $validator->validate_assoc(array_merge(\Terminus::get_config(), $extra_args, $assoc_args)); foreach ($validator->unknown_assoc($assoc_args) as $key) { $errors['fatal'][] = "unknown --{$key} parameter"; } if (!empty($errors['fatal'])) { $out = 'Parameter errors:'; foreach ($errors['fatal'] as $error) { $out .= "\n " . $error; } throw new TerminusException($out); } foreach ($errors['warning'] as $warning) { \Terminus::log('warning', $warning); } return $to_unset; }