PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/src/Config.php

http://github.com/BrunoDeBarros/git-deploy-php
PHP | 129 lines | 105 code | 23 blank | 1 comment | 20 complexity | f8f476b5ba8501aace0ae4c4b78bd532 MD5 | raw file
  1. <?php
  2. namespace Brunodebarros\Gitdeploy;
  3. use Brunodebarros\Gitdeploy\Helpers;
  4. class Config {
  5. public static function getArgs() {
  6. $argv = $_SERVER['argv'];
  7. $deploy = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'deploy.ini';
  8. $commands = array('-l', '-r', '-c', '-d', '--revert', '--log', '--repo');
  9. $deploy_file = isset($argv[1]) ? end($argv) : "deploy.ini";
  10. if (!in_array($deploy_file, $commands)) {
  11. $deploy = $deploy_file . (substr($deploy_file, -4) === '.ini' ? '' : '.ini');
  12. }
  13. $opts = getopt("lr:d:c:", array("revert", "log::", "repo:"));
  14. if (isset($opts['log'])) {
  15. define('WRITE_TO_LOG', $opts['revert'] ? $opts['revert'] : 'git_deploy_php_log.txt');
  16. }
  17. if (isset($opts['d'])) {
  18. $deploy = $opts['d'];
  19. }
  20. if (isset($opts['c'])) {
  21. $opts['r'] = $opts['c'];
  22. }
  23. if (isset($opts['repo'])) {
  24. $repo_path = $opts['repo'];
  25. } else {
  26. $repo_path = getcwd() . DIRECTORY_SEPARATOR;
  27. }
  28. return array(
  29. 'config_file' => $deploy,
  30. 'target_commit' => isset($opts['r']) ? $opts['r'] : 'HEAD',
  31. 'list_only' => isset($opts['l']),
  32. 'revert' => isset($opts['revert']),
  33. 'repo_path' => $repo_path,
  34. );
  35. }
  36. public static function getServers($config_file) {
  37. $servers = @parse_ini_file($config_file, true);
  38. $return = array();
  39. if (!$servers) {
  40. Helpers::error("File '$config_file' is not a valid .ini file.");
  41. } else {
  42. foreach ($servers as $uri => $options) {
  43. if (stristr($uri, "://") !== false) {
  44. $options = array_merge($options, parse_url($uri));
  45. }
  46. # Throw in some default values, in case they're not set.
  47. $options = array_merge(array(
  48. 'skip' => false,
  49. 'scheme' => 'ftp',
  50. 'host' => '',
  51. 'user' => '',
  52. 'branch' => null,
  53. 'port' => 21,
  54. 'path' => '/',
  55. 'passive' => true,
  56. 'clean_directories' => array(),
  57. 'ignore_files' => array(),
  58. 'ignore_directories' => array(),
  59. 'upload_untracked' => array(),
  60. 'check_sync_with_remote' => false,
  61. 'remote_branch' => null
  62. ), $options);
  63. if ($options['check_sync_with_remote'])
  64. {
  65. if (empty($options['remote_branch']))
  66. {
  67. $options['remote_branch'] = (!empty($options['branch'])) ? "origin/".$options['branch'] : null;
  68. }
  69. }
  70. if (!isset($options['pass']) && !isset($options['sftp_key'])) {
  71. $options['pass'] = self::promptPassword();
  72. }
  73. if (isset($options['sftp_key'])) {
  74. if (substr($options['sftp_key'], 0, 2) == "~/") {
  75. $options['sftp_key'] = $_SERVER['HOME'] . substr($options['sftp_key'], 1);
  76. }
  77. }
  78. if ($options['skip']) {
  79. continue;
  80. } else {
  81. unset($options['skip']);
  82. $type = "Brunodebarros\\Gitdeploy\\" . ucfirst(strtolower($options['scheme']));
  83. $return[$uri] = new $type($options, $config_file);
  84. }
  85. }
  86. }
  87. return $return;
  88. }
  89. public static function promptPassword() {
  90. $prompt = 'Enter ftp password: ';
  91. $command = "/usr/bin/env bash -c 'echo OK'";
  92. if (rtrim(shell_exec($command)) !== 'OK') {
  93. trigger_error("Can't invoke bash");
  94. return;
  95. }
  96. $command = "/usr/bin/env bash -c 'read -s -p \""
  97. . addslashes($prompt)
  98. . "\" mypassword && echo \$mypassword'";
  99. $password = rtrim(shell_exec($command));
  100. echo "\n";
  101. return $password;
  102. }
  103. }