Skip to content

Commit 3987782

Browse files
authored
Merge pull request #156 from appwrite/feat-cli-array-params
feat: added new array params
2 parents feb6d75 + 1793377 commit 3987782

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

templates/cli/README.md.twig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ $ {{ language.params.executableName }} users create --email="hello@{{ spec.title
7575
$ {{ language.params.executableName }} users list
7676
```
7777

78+
To create a Document you can use the following command
79+
```sh
80+
$ {{ language.params.executableName }} database createDocument --collectionId="YOUR COLLECTION ID" --data='A VALID JSON STRING' --read=role:member --read="*" --write=role:guest
81+
```
82+
83+
### Some Gotchas
84+
- `data` expects the JSON string to be escaped.
85+
- If using the wildcard (`*`) read or write permissions , make sure that it is properly escaped using a `\` or by enclosing it in `"*"` since bash interprets them differently.
86+
- Some arguments like `read` and `write` permissions are expected to be arrays. In the {{ spec.title }} CLI, arrays are passed by simply repeating the argument as seen in the `createDocument` example above.
87+
7888
To get information about the different services available, you can use
7989
```sh
8090
$ {{ language.params.executableName }} help

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ $commands = [
1616
{% for service in spec.services %}
1717
"{{ service.name }}" => "{{ service.description | replace({'"':'\''}) | raw }}",
1818
{% endfor %}
19+
"client" => "The Client service allows you to set preferences of your Appwrite CLI",
20+
"init" => "Init allows you to reset your Appwrite CLI."
1921
];
2022
$parser->formatArray($commands);
2123
Console::log("\nRun '{{ language.params.executableName }} [SERVICE] help' for more information on a service.");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ $cli
173173
->param('{{ header.key | lower }}', '', new Wildcard(), '{{ header.description }}', true)
174174
{% endfor %}
175175
->action(function( $endpoint, {% for header in spec.global.headers %} ${{ header.key | lower }}{% if not loop.last %},{% endif %}{% endfor %} ) {
176-
/* Check if enviroment variables exist
176+
/*
177+
* Check if enviroment variables exist
177178
* Else prompt the user
178179
*/
179180

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,20 @@ $cli
4646
{% for parameter in method.parameters.all %}
4747
/** @var {{ parameter.type }} ${{ parameter.name }} */
4848
{% endfor %}
49+
4950
$client = new Client();
5051
$path = str_replace([{% for parameter in method.parameters.path %}'{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}'{% if not loop.last %}, {% endif %}{% endfor %}], [{% for parameter in method.parameters.path %}${{ parameter.name | caseCamel }}{% if not loop.last %}, {% endif %}{% endfor %}], '{{ method.path }}');
5152
$params = [];
53+
{% if method.parameters.query|length > 0 %}
54+
/** Query Params */
55+
{% endif %}
5256
{% for parameter in method.parameters.query %}
53-
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel }};
57+
$params['{{ parameter.name }}'] = {% if parameter.type == 'array' %}!is_array(${{ parameter.name | caseCamel }}) ? array(${{ parameter.name | caseCamel }}) : ${{ parameter.name | caseCamel }};{% else %}${{ parameter.name | caseCamel }};{% endif %}
58+
5459
{% endfor %}
60+
{% if method.parameters.body|length > 0 %}
61+
/** Body Params */
62+
{% endif %}
5563
{% for parameter in method.parameters.body %}
5664
{% if parameter.type == 'file' and method.packaging %}
5765
$cloudFunctionPath = realpath(__DIR__.'/../../../files/'.${{ parameter.name | caseCamel }});
@@ -74,19 +82,23 @@ $cli
7482
$cFile = new \CURLFile(${{ parameter.name | caseCamel }}, {% if method.packaging %} 'application/x-gzip' {% else %} 'image/png' {% endif %}, basename(${{ parameter.name | caseCamel }}));
7583
$params['{{ parameter.name }}'] = $cFile;
7684
{% else %}
77-
$params['{{ parameter.name }}'] = {% if parameter.type == 'integer' %}(int){% endif %} ${{ parameter.name | caseCamel }};
85+
$params['{{ parameter.name }}'] = {% if parameter.type == 'array' %}!is_array(${{ parameter.name | caseCamel }}) ? array(${{ parameter.name | caseCamel }}) : ${{ parameter.name | caseCamel }};{% elseif parameter.type == 'integer' %}(int)${{ parameter.name | caseCamel }};{% else %}${{ parameter.name | caseCamel }};{% endif %}
86+
7887
{% endif %}
7988
{% endfor %}
89+
{% if method.parameters.formData|length > 0 %}
90+
/** Form Data Params */
91+
{% endif %}
8092
{% for parameter in method.parameters.formData %}
81-
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel }};
93+
$params['{{ parameter.name }}'] = {% if parameter.type == 'array' %}!is_array(${{ parameter.name | caseCamel }}) ? array(${{ parameter.name | caseCamel }}) : ${{ parameter.name | caseCamel }};{% elseif parameter.type == 'integer' %}(int)${{ parameter.name | caseCamel }};{% else %}${{ parameter.name | caseCamel }};{% endif %}
94+
8295
{% endfor %}
8396
{% if method.type == 'location' %}
8497
$params['project'] = $client->getPreference('X-Appwrite-Project');
8598
$params['key'] = $client->getPreference('X-Appwrite-Key');
8699
$path = $client->getPreference(Client::PREFERENCE_ENDPOINT).$path . "?" . http_build_query($params);
87100
Console::success($path);
88101
{% else %}
89-
90102
$response = $client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
91103
{% for parameter in method.parameters.header %}
92104
'{{ parameter.name }}' => ${{ parameter.name | caseCamel }},

templates/cli/composer.json.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"require": {
1616
"php": ">=7.4.0",
17-
"utopia-php/cli": "0.10.0",
17+
"utopia-php/cli": "0.11.0",
1818
"jc21/clitable": "^1.2"
1919
},
2020
"minimum-stability": "dev"

templates/cli/docs/example.md.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{ language.params.executableName }} {{ service.name }} {{ method.name }} {% for parameter in method.parameters.all %}{% if parameter.type == 'array' %}"--{{ parameter.name }}[]={{ parameter.example }}"{% else %}--{{ parameter.name }}="{{ parameter.example }}"{% endif %} {% endfor %}
1+
{{ language.params.executableName }} {{ service.name }} {{ method.name }} {% for parameter in method.parameters.all %}--{{ parameter.name }}="{{ parameter.example }}" {% endfor %}

tests/languages/cli/test.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,54 +11,54 @@
1111
echo "\nTest Started\n";
1212

1313
// Foo Service
14-
$command = "${baseCommand} cli foo get --x='string' --y='123' --z[]='string in array'";
14+
$command = "${baseCommand} cli foo get --x='string' --y='123' --z='string in array'";
1515
$output = [];
1616
exec($command, $output);
1717
echo substr($output[0],9)."\n";
1818

19-
$command = "${baseCommand} cli foo post --x='string' --y='123' --z[]='string in array'";
19+
$command = "${baseCommand} cli foo post --x='string' --y='123' --z='string in array'";
2020
$output = [];
2121
exec($command, $output);
2222
echo substr($output[0],9)."\n";
2323

24-
$command = "${baseCommand} cli foo put --x='string' --y='123' --z[]='string in array'";
24+
$command = "${baseCommand} cli foo put --x='string' --y='123' --z='string in array'";
2525
$output = [];
2626
exec($command, $output);
2727
echo substr($output[0],9)."\n";
2828

29-
$command = "${baseCommand} cli foo patch --x='string' --y='123' --z[]='string in array'";
29+
$command = "${baseCommand} cli foo patch --x='string' --y='123' --z='string in array'";
3030
$output = [];
3131
exec($command, $output);
3232
echo substr($output[0],9)."\n";
3333

34-
$command = "${baseCommand} cli foo delete --x='string' --y='123' --z[]='string in array'";
34+
$command = "${baseCommand} cli foo delete --x='string' --y='123' --z='string in array'";
3535
$output = [];
3636
exec($command, $output);
3737
echo substr($output[0],9)."\n";
3838

3939

4040
// Bar Service
41-
$command = "${baseCommand} cli bar get --x='string' --y='123' --z[]='string in array'";
41+
$command = "${baseCommand} cli bar get --x='string' --y='123' --z='string in array'";
4242
$output = [];
4343
exec($command, $output);
4444
echo substr($output[0],9)."\n";
4545

46-
$command = "${baseCommand} cli bar post --x='string' --y='123' --z[]='string in array'";
46+
$command = "${baseCommand} cli bar post --x='string' --y='123' --z='string in array'";
4747
$output = [];
4848
exec($command, $output);
4949
echo substr($output[0],9)."\n";
5050

51-
$command = "${baseCommand} cli bar put --x='string' --y='123' --z[]='string in array'";
51+
$command = "${baseCommand} cli bar put --x='string' --y='123' --z='string in array'";
5252
$output = [];
5353
exec($command, $output);
5454
echo substr($output[0],9)."\n";
5555

56-
$command = "${baseCommand} cli bar patch --x='string' --y='123' --z[]='string in array'";
56+
$command = "${baseCommand} cli bar patch --x='string' --y='123' --z='string in array'";
5757
$output = [];
5858
exec($command, $output);
5959
echo substr($output[0],9)."\n";
6060

61-
$command = "${baseCommand} cli bar delete --x='string' --y='123' --z[]='string in array'";
61+
$command = "${baseCommand} cli bar delete --x='string' --y='123' --z='string in array'";
6262
$output = [];
6363
exec($command, $output);
6464
echo substr($output[0],9)."\n";
@@ -69,7 +69,7 @@
6969
exec($command, $output);
7070
echo substr($output[0],9)."\n";
7171

72-
$command = "${baseCommand} cli general upload --x='string' --y='123' --z[]='string in array' --file='file.png'";
72+
$command = "${baseCommand} cli general upload --x='string' --y='123' --z='string in array' --file='file.png'";
7373
$output = [];
7474
exec($command, $output);
7575
echo substr($output[0],9)."\n";

0 commit comments

Comments
 (0)