Skip to content

Commit 0b404e4

Browse files
authored
Merge pull request #112 from christyjacob4/fix-cli-issues
Fix cli issues
2 parents 4a07482 + 1609977 commit 0b404e4

File tree

13 files changed

+513
-107
lines changed

13 files changed

+513
-107
lines changed

example.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,17 @@ function getSSLPage($url) {
337337
$sdk->generate(__DIR__ . '/examples/dotnet');
338338

339339
// CLI
340-
$sdk = new SDK(new CLI(), new Swagger2($spec));
340+
$cli = new CLI();
341+
$cli->setExecutableName('appwrite');
342+
$cli->setLogo("
343+
_ _ _ ___ __ _____
344+
/_\ _ __ _ ____ ___ __(_) |_ ___ / __\ / / \_ \
345+
//_\\| '_ \| '_ \ \ /\ / / '__| | __/ _ \ / / / / / /\/
346+
/ _ \ |_) | |_) \ V V /| | | | || __/ / /___/ /___/\/ /_
347+
\_/ \_/ .__/| .__/ \_/\_/ |_| |_|\__\___| \____/\____/\____/
348+
|_| |_|
349+
");
350+
$sdk = new SDK($cli, new Swagger2($spec));
341351
$sdk
342352
->setName('NAME')
343353
->setDescription('Repo description goes here')

src/SDK/Language/CLI.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,39 @@
44

55
class CLI extends PHP {
66

7+
/**
8+
* @var array
9+
*/
10+
protected $params = [
11+
'composerVendor' => 'vendor-name',
12+
'composerPackage' => 'package-name',
13+
'executableName' => 'executable',
14+
'logo' => ''
15+
];
16+
17+
/**
18+
* @param string $name
19+
* @return $this
20+
*/
21+
public function setExecutableName($name)
22+
{
23+
$this->setParam('executableName', $name);
24+
25+
return $this;
26+
}
27+
28+
/**
29+
* @param string $logo
30+
* @return $this
31+
*/
32+
public function setLogo($logo)
33+
{
34+
$this->setParam('logo', $logo);
35+
36+
return $this;
37+
}
38+
39+
740
/**
841
* @return string
942
*/
@@ -139,6 +172,18 @@ public function getFiles()
139172
'template' => '/cli/bin/client.twig',
140173
'minify' => false,
141174
],
175+
[
176+
'scope' => 'default',
177+
'destination' => '/install.sh',
178+
'template' => '/cli/install.sh.twig',
179+
'minify' => false,
180+
],
181+
[
182+
'scope' => 'default',
183+
'destination' => '/install.ps1',
184+
'template' => '/cli/install.ps1.twig',
185+
'minify' => false,
186+
],
142187
[
143188
'scope' => 'default',
144189
'destination' => '/bin/help',

templates/cli/README.md.twig

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,73 @@ You can install the CLI by getting our installation script.
2121

2222
### Linux / MacOS
2323
```bash
24-
$ wget -q https://appwrite.io/cli/install.sh -O - | /bin/bash
24+
$ wget -q https://{{ spec.title | lower }}.io/cli/install.sh -O - | /bin/bash
2525
```
2626

2727
### Windows
2828
```powershell
29-
$ iwr -useb https://appwrite.io/cli/install.ps1 | iex
29+
$ iwr -useb https://{{ spec.title | lower }}.io/cli/install.ps1 | iex
3030
```
3131

3232
## Initialisation
3333
The CLI needs to be initialised with your project settings. This can be done using
3434
```sh
35-
$ appwrite init
35+
$ {{ language.params.executableName }} init
3636
```
3737

38-
The CLI requires an endpoint, project ID, API key and a locale to be able to communicate with the Appwrite backend server. There are two ways to achieve this
38+
The CLI requires an endpoint, project ID, API key and a locale to be able to communicate with the {{ spec.title | caseUcfirst }} backend server. There are two ways to achieve this
3939

4040
### 1. Environment file
4141
```sh
42-
$ appwrite --env-file path/to/your/environment/file init
42+
$ {{ language.params.executableName }} --env-file path/to/your/environment/file init
4343
```
4444
The .env file must have the following keys
4545
```sh
4646
endpoint=http://192.168.1.6/v1
47-
X-Appwrite-Project=5ff450422d42f
48-
X-Appwrite-Key=0773c7...a2672a
49-
X-Appwrite-Locale=en-US
47+
X-{{ spec.title | caseUcfirst }}-Project=5ff450422d42f
48+
X-{{ spec.title | caseUcfirst }}-Key=0773c7...a2672a
49+
X-{{ spec.title | caseUcfirst }}-Locale=en-US
5050
```
5151

5252
### 2. Using the prompt
5353

54-
Invoking `appwrite init` without an env file triggers an interactive prompt where you can manually enter the details. These values are saved locally so that you dont have to re-enter them everytime.
54+
Invoking `{{ language.params.executableName }} init` without an env file triggers an interactive prompt where you can manually enter the details. These values are saved locally so that you dont have to re-enter them everytime.
5555

5656
## Usage
5757

58-
The Appwrite CLI follows the following general syntax.
58+
The {{ spec.title | caseUcfirst }} CLI follows the following general syntax.
5959
```sh
60-
$ appwrite [SERVICE] [COMMAND] --[OPTIONS]
60+
$ {{ language.params.executableName }} [SERVICE] [COMMAND] --[OPTIONS]
6161
```
6262

6363
A few sample commands to get you started
6464

6565
```sh
66-
$ appwrite users create --email="hello@appwrite.io" --password="very_strong_password"
67-
$ appwrite users list
66+
$ {{ language.params.executableName }} users create --email="hello@{{ spec.title | lower }}.io" --password="very_strong_password"
67+
$ {{ language.params.executableName }} users list
6868
```
6969

7070
To get information about the different services available, you can use
7171
```sh
72-
$ appwrite help
72+
$ {{ language.params.executableName }} help
7373
```
7474

7575
To get information about a particular service and the commands available in a service you can use
7676
```sh
77-
$ appwrite users help
78-
$ appwrite accounts help
77+
$ {{ language.params.executableName }} users help
78+
$ {{ language.params.executableName }} accounts help
7979
```
8080

8181
To get information about a particular command and the parameters it accepts, you can use
8282

8383
```sh
84-
$ appwrite users list --help
85-
$ appwrite account get --help
84+
$ {{ language.params.executableName }} users list --help
85+
$ {{ language.params.executableName }} account get --help
8686
```
8787

8888
## Contribution
8989

90-
This library is auto-generated by Appwrite custom [SDK Generator](https://github.com/appwrite/sdk-generator). To learn more about how you can help us improve this SDK, please check the [contribution guide](https://github.com/appwrite/sdk-generator/blob/master/CONTRIBUTING.md) before sending a pull-request.
90+
This library is auto-generated by {{ spec.title | caseUcfirst }} custom [SDK Generator](https://github.com/{{ spec.title | lower }}/sdk-generator). To learn more about how you can help us improve this SDK, please check the [contribution guide](https://github.com/{{ spec.title | lower }}/sdk-generator/blob/master/CONTRIBUTING.md) before sending a pull-request.
9191

9292
## License
9393

templates/cli/app/services/client.php.twig

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,43 @@ use {{ spec.title | caseUcfirst }}\Client;
88
use Utopia\CLI\CLI;
99
use Utopia\Validator\Mock;
1010
use Utopia\CLI\Console;
11+
use {{ spec.title | caseUcfirst }}\Parser;
1112
1213
$client = new Client();
1314
$cli = new CLI();
15+
$parser = new Parser();
16+
17+
$cli->
18+
init(function() use ($cli, $parser) {
19+
20+
if (array_key_exists('help', $cli->getArgs())) {
21+
$taskName = $cli->match()->getName();
22+
$task = $cli->getTasks()[$taskName];
23+
$description = $task->getLabel('description', '');
24+
$params = $task->getParams();
25+
26+
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
27+
Console::log("\nUsage : {{ language.params.executableName }} client {$taskName} --[OPTIONS] \n");
28+
Console::log($description);
29+
Console::log("Options:");
30+
array_walk($params, function(&$key) {
31+
$key = $key['description'];
32+
});
33+
$parser->formatArray($params);
34+
Console::exit(0);
35+
}
36+
});
1437
1538
$cli
1639
->task('setEndpoint')
17-
->param('endpoint', '', new Mock(), 'Your Appwrite endpoint', false)
40+
->param('endpoint', '', new Mock(), 'Your {{ spec.title | title }} endpoint', false)
1841
->action(function($endpoint) use ($client) {
1942
$client->setPreference('endpoint', $endpoint);
2043
$result = $client->savePreferences();
2144
if ($result === false) {
22-
Console::error('Could not save preferences.');
45+
Console::error('Could not save preferences.');
2346
} else {
24-
Console::success('Preferences saved successfully');
47+
Console::success('Preferences saved successfully');
2548
}
2649
});
2750
@@ -34,9 +57,9 @@ $cli
3457
$client->setPreference('{{ header.name }}', ${{ header.key | lower }});
3558
$result = $client->savePreferences();
3659
if ($result === false) {
37-
Console::error('Could not save preferences.');
60+
Console::error('Could not save preferences.');
3861
} else {
39-
Console::success('Preferences saved successfully');
62+
Console::success('Preferences saved successfully');
4063
}
4164
});
4265
@@ -49,4 +72,22 @@ $cli
4972
Console::log('Server Version : {{ spec.version | url_encode }}');
5073
});
5174
75+
76+
$cli
77+
->task('help')
78+
->action(function() use ($parser) {
79+
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
80+
Console::log("\nUsage : {{ language.params.executableName }} client [COMMAND]\n");
81+
Console::log("Commands :");
82+
$commands = [
83+
"setEndpoint" => "Set your server endpoint.",
84+
"setProject" => "Set the project you want to connect to.",
85+
"setKey" => "Set the API key for the project.",
86+
"setLocale" => "Set your preferred locale (eg: en-US)."
87+
];
88+
$parser->formatArray($commands);
89+
Console::log("\nRun '{{ language.params.executableName }} client COMMAND --help' for more information on a command.");
90+
});
91+
92+
5293
$cli->run();

templates/cli/app/services/help.php.twig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22
33
namespace {{ spec.title | caseUcfirst }}\Services;
44
5-
echo "\e[0;31;m
6-
_ _ _
7-
/_\ _ __ _ ____ ___ __(_) |_ ___
8-
//_\\| '_ \| '_ \ \ /\ / / '__| | __/ _ \
9-
/ _ \ |_) | |_) \ V V /| | | | || __/
10-
\_/ \_/ .__/| .__/ \_/\_/ |_| |_|\__\___|
11-
|_| |_|
12-
13-
\e[0m" ;
5+
require_once './vendor/autoload.php';
146
15-
printf("\nUsage : appwrite [SERVICE] [COMMAND] --[OPTION]\n\n");
16-
printf("Services :\n");
17-
$mask = "\t%-20.20s %-125.125s\n";
7+
use Utopia\CLI\Console;
8+
use {{ spec.title | caseUcfirst }}\Parser;
9+
10+
$parser = new Parser();
11+
12+
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
13+
Console::log("\nUsage : {{ language.params.executableName }} [SERVICE] [COMMAND] --[OPTION]\n");
14+
Console::log("Services :");
15+
$commands = [
1816
{% for service in spec.services %}
19-
printf($mask, "{{ service.name }}", "{{ service.description | replace({'"':'\''}) | raw }}");
17+
"{{ service.name }}" => "{{ service.description | replace({'"':'\''}) | raw }}",
2018
{% endfor %}
21-
printf("\nRun 'appwrite [SERVICE] help' for more information on a service.\n");
19+
];
20+
$parser->formatArray($commands);
21+
Console::log("\nRun '{{ language.params.executableName }} [SERVICE] help' for more information on a service.");

templates/cli/app/services/init.php.twig

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<?php
22
3-
namespace Appwrite\Services;
3+
namespace {{ spec.title | caseUcfirst }}\Services;
44
55
require_once './vendor/autoload.php';
66
77
use Exception;
88
use Utopia\CLI\CLI;
99
use Utopia\Validator\Mock;
1010
use Utopia\CLI\Console;
11+
use {{ spec.title | caseUcfirst }}\Parser;
1112
1213
const USER_PREFERENCES_FILE = __DIR__."/../../.preferences/.prefs.json";
1314
const PREFERENCE_ENDPOINT = "endpoint";
@@ -110,7 +111,7 @@ function promptUser()
110111
}
111112
112113
if(empty(getPreference('X-Appwrite-Project'))) {
113-
$project = Console::confirm('🟢 Enter your project from the Appwrite console: ');
114+
$project = Console::confirm('🟢 Enter your project from the {{ spec.title | caseUcfirst }} console: ');
114115
if (empty($project)) {
115116
Console::error("❌ You cannot continue without a project. Exiting...");
116117
exit();
@@ -119,7 +120,7 @@ function promptUser()
119120
}
120121
121122
if(empty(getPreference('X-Appwrite-Key'))) {
122-
$key = Console::confirm('🟢 Enter your key from the Appwrite console: ');
123+
$key = Console::confirm('🟢 Enter your key from the {{ spec.title | caseUcfirst }} console: ');
123124
if (empty($key)) {
124125
Console::error("❌ You cannot continue without a key. Exiting...");
125126
exit();
@@ -141,10 +142,33 @@ function promptUser()
141142
}
142143
143144
$cli = new CLI();
145+
$parser = new Parser();
146+
147+
$cli->
148+
init(function() use ($cli, $parser) {
149+
150+
if (array_key_exists('help', $cli->getArgs())) {
151+
$taskName = $cli->match()->getName();
152+
$task = $cli->getTasks()[$taskName];
153+
$description = $task->getLabel('description', '');
154+
$params = $task->getParams();
155+
156+
Console::log("\e[0;31;m {{ language.params.logo | raw }} \e[0m") ;
157+
Console::log("\nUsage : {{ language.params.executableName }} {$taskName} --[OPTIONS] \n");
158+
Console::log($description);
159+
Console::log("Options:");
160+
array_walk($params, function(&$key) {
161+
$key = $key['description'];
162+
});
163+
$parser->formatArray($params);
164+
Console::exit(0);
165+
}
166+
});
144167
145168
$cli
146169
->task('init')
147-
->param('endpoint', '', new Mock(), 'Your Appwrite endpoint', true)
170+
->label('description', "The init command is used to initialise your CLI\n")
171+
->param('endpoint', '', new Mock(), 'Your {{ spec.title | caseUcfirst }} endpoint', true)
148172
{% for header in spec.global.headers %}
149173
->param('{{ header.key | lower }}', '', new Mock(), '{{ header.description }}', true)
150174
{% endfor %}

0 commit comments

Comments
 (0)