-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.php
140 lines (131 loc) · 2.95 KB
/
cli.php
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
<?php
/*
* @Name: php-cli-apps (draft in PHP 7.4)
* @Author: Max Base
* @Repository: https://github.com/BaseMax/php-cli-apps
* @Date: August 31, 2020
*/
$flags=[];
if($argc === 1) {
help();
}
else {
if(startsWith($argv[1], "-")) {// && ($argv[1] === "-h" || $argv[1] === "--help")
help();
}
else {
$command=$argv[1];
callCommand($command);
}
}
function startsWith($haystack, $needle ) {
$length = strlen($needle);
return substr($haystack, 0, $length) === $needle;
}
function endsWith($haystack, $needle) {
$length = strlen( $needle );
if(!$length ) {
return true;
}
return substr($haystack, -$length) === $needle;
}
function help($command="") {
// show special help for commands: if($command == "install") {}
print "Usage:
donya [command]
Available Commands:
help Help about any command
install Installing package(s) in DonyaOS
remove Removing package(s) in DonyaOS
list Listing package(s) in DonyaOS
search Search package(s) in the repository of DonyaOS
Flags:
-h, --help help for donya
Use \"donya [command] --help\" for more information about a command.
";
}
function callCommand($command) {
global $argv;
$args=$argv;
unset($args[0]); // software name. e.g: donya
unset($args[1]); // command name
$args=array_values($args); // start index of items from 0 in Array
switch ($command) {
case "i":
case "install":
commandInstall($args);
break;
case "r":
case "remove":
commandRemove($args);
break;
case "u":
case "update":
commandUpdate($args);
break;
case "l":
case "list":
commandList($args);
break;
case "s":
case "search":
commandSearch($args);
break;
case "h":
case "help":
default:
help($command);
break;
}
}
function commandInstall($args) {
$flags = flags("install", $args);
$args = $flags[1];
$flags = $flags[0];
print_r($args);
print_r($flags);
foreach($args as $arg) {
// Read list of package from `cache` file and detect category/group name of packages!
$group="core"; // WE need to set this from `cache` file
$data = file_get_contents("https://donyaos.github.io/Packages/$group/$arg/package.donya");
if(($data !== null || $data !== "") and strlen($data) > 0) {
print $data;
}
}
}
function commandRemove($args) {
$flags = flags("remove", $args);
$args = $flags[1];
$flags = $flags[0];
print_r($args);
print_r($flags);
}
function commandList($args) {
$flags = flags("list", $args);
$args = $flags[1];
$flags = $flags[0];
print_r($args);
print_r($flags);
}
function commandSearch($args) {
$flags = flags("search", $args);
$args = $flags[1];
$flags = $flags[0];
print_r($args);
print_r($flags);
}
function flags($command, $args) {
$flags=[];
foreach($args as $i=>$arg) {
if(startsWith($arg, "-")) {
if($arg === "-h" || $arg === "--help") {
unset($args[$i]);
help();
}
else if($command == "install" && ($arg == "-r" || $arg == '--rewrite')) {
$flags[]="rewrite";
}
}
}
return [$flags, $args];
}