Add --dry-run option for getting curl commands
[shpub.git] / src / shpub / Config.php
1 <?php
2 namespace shpub;
3
4 class Config
5 {
6     public $hosts = [];
7
8     /**
9      * Currently selected host.
10      *
11      * @var Host
12      */
13     public $host;
14
15     public $debug = false;
16
17     public $dryRun = false;
18
19     protected function getConfigFilePath()
20     {
21         if (!isset($_SERVER['HOME'])) {
22             Log::err('Cannot determine home directory');
23             return false;
24         }
25
26         return $_SERVER['HOME'] . '/.config/shpub.ini';
27     }
28
29     public function load()
30     {
31         $cfgFile = $this->getConfigFilePath();
32         if ($cfgFile == false) {
33             return false;
34         }
35
36         if (!file_exists($cfgFile) || !is_readable($cfgFile)) {
37             return false;
38         }
39
40         $data = parse_ini_file($cfgFile, true);
41         foreach ($data as $key => $val) {
42             if (!is_array($val)) {
43                 continue;
44             }
45             $host = new Config_Host();
46             foreach ($val as $hostProp => $hostVal) {
47                 if (!property_exists($host, $hostProp)) {
48                     Log::err('Invalid config key "' . $hostProp . '"');
49                     exit(1);
50                 }
51                 $host->$hostProp = $hostVal;
52             }
53             $this->hosts[$key] = $host;
54         }
55     }
56
57     public function save()
58     {
59         $str = '';
60         foreach ($this->hosts as $hostName => $host) {
61             if ($str != '') {
62                 $str .= "\n";
63             }
64             $str .= '[' . $hostName . "]\n";
65             foreach ($host as $hostProp => $hostVal) {
66                 if ($hostProp == 'endpoints') {
67                     continue;
68                 }
69                 if ($hostVal == '') {
70                     continue;
71                 }
72                 $str .= $hostProp
73                     . '=' . static::quoteIniValue($hostVal) . "\n";
74             }
75         }
76         $cfgFilePath = $this->getConfigFilePath();
77         $cfgDir = dirname($cfgFilePath);
78         if (!is_dir($cfgDir)) {
79             mkdir($cfgDir, 0700);
80         }
81         file_put_contents($cfgFilePath, $str);
82         //contains sensitive data; nobody else may read that
83         chmod($cfgFilePath, 0600);
84     }
85
86     public function getDefaultHost()
87     {
88         if (!count($this->hosts)) {
89             return null;
90         }
91         foreach ($this->hosts as $key => $host) {
92             if ($host->default) {
93                 return $key;
94             }
95         }
96
97         reset($this->hosts);
98         return key($this->hosts);
99     }
100
101     public function getHostByName($keyOrServer)
102     {
103         if (!count($this->hosts)) {
104             return null;
105         }
106         foreach ($this->hosts as $key => $host) {
107             if ($key == $keyOrServer || $host->server == $keyOrServer) {
108                 return $key;
109             }
110         }
111         return null;
112     }
113
114     public static function quoteIniValue($val)
115     {
116         if (strpos($val, '=') === false) {
117             return $val;
118         }
119         return '"' . $val . '"';
120     }
121
122     public function setDebug($debug)
123     {
124         $this->debug = $debug;
125     }
126
127     public function setDryRun($dryRun)
128     {
129         $this->dryRun = $dryRun;
130     }
131 }
132 ?>