-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpswitch.php
More file actions
executable file
·356 lines (316 loc) · 9.69 KB
/
Copy pathpswitch.php
File metadata and controls
executable file
·356 lines (316 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#!/usr/bin/env php
<?php
process($argv);
/**
* process argv and start
*
* @param $argv
*/
function process($argv)
{
$config = init();
in_array('-a', $argv) && addSoftware($config);
in_array('-s', $argv) && switchSoftware($argv, $config);
in_array('-l', $argv) && listSoftware($argv, $config);
in_array('-d', $argv) && deleteSoftware($argv, $config);
in_array('-v', $argv) && version();
displayHelp();
}
/**
* init
*
* @return array
*/
function init()
{
$path = __DIR__ . DIRECTORY_SEPARATOR . 'switch.json';
$emptyConfig = [];
if (file_exists($path)) {
//decode config json to array
$config = json_decode(file_get_contents($path), true);
} else {
//make new config file
if (is_writeable(dirname($path))) {
touch($path);
} else {
error("can't create config file [$path]. check the permission to write");
}
}
return empty($config) ? $emptyConfig : $config;
}
/**
* display help information
*/
function displayHelp()
{
echo <<<EOF
PSWITCH Help
------------------
Options
--help this help
-a add software version
-s switch software version.
e.g: pswitch -s [software name] [software version]
-l show the software list
e.g: pswitch -l / pswitch -l [software name]
-d delete software version
e.g: pswitch -d [software name] [software version]
-v show PSWITCH version
EOF;
}
/**
* show PSWITCH version
*/
function version()
{
echo <<<EOF
PSWITCH version 0.0.1
EOF;
exit(0);
}
/**
* add software version info to config
*
* @param $config
*/
function addSoftware($config)
{
//get the software info
fwrite(STDOUT, 'Please input software name:');
$name = trim(fgets(STDIN));
empty($name) && error('software name is required');
fwrite(STDOUT, 'Please input software absolute path (bin directory or file):');
$path = trim(fgets(STDIN));
empty($path) && error('software path is required');
$path = rtrim($path, '/');
fwrite(STDOUT, 'Please input software version (default: 1.0.0):');
$version = trim(fgets(STDIN));
empty($version) && ($version = '1.0.0');
fwrite(STDOUT, 'Please input symbolic link directory (default: /usr/local/bin):');
$linkDir = trim(fgets(STDIN));
empty($linkDir) && ($linkDir = '/usr/local/bin');
$linkDir = rtrim($linkDir, '/');
//make sure symbolic link directory exist
if (!is_dir($linkDir)) {
error("can't add software, because symbolic link directory doesn't exist");
}
//get all file path from software path
$files = [];
if (is_dir($path)) {
$dir = dir($path);
while ($filename = $dir->read()) {
$file = $path . '/' . $filename;
if (!is_dir($file) && ($filename != '.') && ($filename != '..')) {
$files[] = $file;
}
}
$dir->close();
} else {
if (!file_exists($path)) {
error("the file [$path] doesn't exist!");
}
$files[] = $path;
$path = dirname($path);
}
//set all software info
$info = [];
$info['path'] = $path;
$info['files'] = $files;
$info['linkDir'] = $linkDir;
$info['active'] = 0;
//make sure the software version does not exist or replace it
if (isset($config[$name][$version])) {
fwrite(STDOUT, 'this software version is exist, replace it? (y/n, default:n):');
$temp = trim(fgets(STDIN));
if ($temp != 'y') {
exit(0);
}
}
//save info
$config[$name][$version] = $info;
saveConfig($config);
exit(0);
}
/**
* switch software version
*
* @param $argv
* @param $config
*/
function switchSoftware($argv, $config)
{
//make sure $argv & $config are right
if ((count($argv) != 4) || ($argv[1] != '-s')) {
error('wrong input! e.g: pswitch -s [software name] [software version]');
}
$name = $argv[2];
$version = $argv[3];
$info = [];
if (empty($config)) {
error('the config is empty, please add software!');
}
//find the version of this software
foreach ($config as $softwareName => $software) {
if ($softwareName == $name) {
foreach ($config[$softwareName] as $softwareVersion => $value) {
if ($softwareVersion == $version) {
$config[$softwareName][$softwareVersion]['active'] = 1;
$info = $value;
break;
} elseif ($config[$softwareName][$softwareVersion]['active']) {
$oldVersion = $softwareVersion;
}
}
break;
}
}
if (empty($info)) {
error("can't find this software or version");
}
//make sure symlink path are writable
if (!is_writeable($info['linkDir'])) {
error("the link path [{$info['linkDir']}] can't write, please check the permission");
}
//link all files of this software
foreach ($info['files'] as $file) {
if (file_exists($file)) {
linkSoftware($name, $file, $info['linkDir'], $config);
} else {
echo "\e[0;31m Warning: file [ $file ] not found, please check it." . PHP_EOL;
}
}
//set the old version inactive
if (isset($softwareName) && isset($oldVersion)) {
$config[$softwareName][$oldVersion]['active'] = 0;
}
saveConfig($config);
exit(0);
}
/**
* show the software list
*
* @param $argv
* @param $config
*/
function listSoftware($argv, $config)
{
//if user set the software name, only output that info
if (isset($argv[2]) && isset($config[$argv[2]])) {
$temp = $config[$argv[2]];
$config = [];
$config[$argv[2]] = $temp;
}
//output all software info or user selected software info
foreach ($config as $name => $software) {
echo "\e[0m" . $name . PHP_EOL;
//get the max string length to format & min length 20
$padLen = array_reduce($software, function ($previousMax, $info) {
$max = max([strlen($info['path']), strlen($info['linkDir'])]);
$max = ($max > $previousMax) ? $max : $previousMax;
return $max;
}, 20);
//output the header
echo "\e[0m\t" . str_pad('version', $padLen) . ' ' .
str_pad('path', $padLen) . ' ' .
str_pad('link directory', $padLen) . ' ' .
str_pad('active', $padLen) . PHP_EOL;
//output the info
foreach ($software as $version => $info) {
//format the info
array_walk($info, function ($value, $key) use (&$info, $padLen) {
if (is_string($value)) {
$info[$key] = str_pad($value, $padLen);
}
});
$version = str_pad($version, $padLen);
if ($info['active']) {
echo "\e[0;32m\t $version {$info['path']} {$info['linkDir']} {$info['active']}" . PHP_EOL;
} else {
echo "\033[0m\t $version {$info['path']} {$info['linkDir']} {$info['active']}" . PHP_EOL;
}
}
}
exit(0);
}
function deleteSoftware($argv, $config)
{
//make sure $argv & $config are right
if ((count($argv) != 4) || ($argv[1] != '-d')) {
error('wrong input! e.g: pswitch -d [software name] [software version]');
}
$name = $argv[2];
$version = $argv[3];
if (!isset($config[$name][$version])) {
error('this software version is not in config!');
}
//delete link
$info = $config[$name][$version];
if ($info['active']) {
//make sure symlink path are writable
if (!is_writeable($info['linkDir'])) {
error("the link path [{$info['linkDir']}] can't write, please check the permission");
}
foreach ($info['files'] as $file) {
$temp = explode('/', $file);
$link = $info['linkDir'] . '/' . end($temp);
if (!checkSymlink($name, $link, $config)) {
echo "\e[0;31m Warning: can't delete link [$link]" . PHP_EOL;
}
}
}
//delete the version info & if this software is empty, delete it
unset($config[$name][$version]);
if (empty($config[$name])) {
unset($config[$name]);
}
saveConfig($config);
exit(0);
}
function linkSoftware($softwareName, $target, $linkDir, $config)
{
//set the symlink path
$temp = explode('/', $target);
$link = $linkDir . '/' . end($temp);
//set symlink
if (checkSymlink($softwareName, $link, $config)) {
symlink($target, $link);
} else {
echo "\e[0;31m Warning: can't create symlink because $link exist" . PHP_EOL;
}
}
function checkSymlink($softwareName, $link, $config)
{
//file_exists() will return FALSE for broken links
if (file_exists($link) || is_link($link)) {
if (!is_link($link)) {
return false;
}
$realPath = readlink($link);
//if this link in config, delete it and return true
$inConfig = false;
foreach ($config[$softwareName] as $version) {
if (!$inConfig) {
foreach ($version['files'] as $file) {
if ($realPath == $file) {
unlink($link);
$inConfig = true;
break;
}
}
}
}
return $inConfig;
}
return true;
}
function saveConfig($config)
{
$path = __DIR__ . DIRECTORY_SEPARATOR . 'switch.json';
$config = json_encode($config, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
file_put_contents($path, $config);
}
function error($info)
{
echo "\e[0;31m Error: $info" . PHP_EOL;
exit(1);
}