Skip to content

Commit

Permalink
Merge pull request #156 from appwrite/feat-cli-array-params
Browse files Browse the repository at this point in the history
feat: added new array params
  • Loading branch information
eldadfux authored Apr 21, 2021
2 parents feb6d75 + 1793377 commit 3987782
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
10 changes: 10 additions & 0 deletions templates/cli/README.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ $ {{ language.params.executableName }} users create --email="hello@{{ spec.title
$ {{ language.params.executableName }} users list
```

To create a Document you can use the following command
```sh
$ {{ language.params.executableName }} database createDocument --collectionId="YOUR COLLECTION ID" --data='A VALID JSON STRING' --read=role:member --read="*" --write=role:guest
```

### Some Gotchas
- `data` expects the JSON string to be escaped.
- 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.
- 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.

To get information about the different services available, you can use
```sh
$ {{ language.params.executableName }} help
Expand Down
2 changes: 2 additions & 0 deletions templates/cli/app/services/help.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ $commands = [
{% for service in spec.services %}
"{{ service.name }}" => "{{ service.description | replace({'"':'\''}) | raw }}",
{% endfor %}
"client" => "The Client service allows you to set preferences of your Appwrite CLI",
"init" => "Init allows you to reset your Appwrite CLI."
];
$parser->formatArray($commands);
Console::log("\nRun '{{ language.params.executableName }} [SERVICE] help' for more information on a service.");
3 changes: 2 additions & 1 deletion templates/cli/app/services/init.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ $cli
->param('{{ header.key | lower }}', '', new Wildcard(), '{{ header.description }}', true)
{% endfor %}
->action(function( $endpoint, {% for header in spec.global.headers %} ${{ header.key | lower }}{% if not loop.last %},{% endif %}{% endfor %} ) {
/* Check if enviroment variables exist
/*
* Check if enviroment variables exist
* Else prompt the user
*/
Expand Down
20 changes: 16 additions & 4 deletions templates/cli/app/services/service.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ $cli
{% for parameter in method.parameters.all %}
/** @var {{ parameter.type }} ${{ parameter.name }} */
{% endfor %}
$client = new Client();
$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 }}');
$params = [];
{% if method.parameters.query|length > 0 %}
/** Query Params */
{% endif %}
{% for parameter in method.parameters.query %}
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel }};
$params['{{ parameter.name }}'] = {% if parameter.type == 'array' %}!is_array(${{ parameter.name | caseCamel }}) ? array(${{ parameter.name | caseCamel }}) : ${{ parameter.name | caseCamel }};{% else %}${{ parameter.name | caseCamel }};{% endif %}
{% endfor %}
{% if method.parameters.body|length > 0 %}
/** Body Params */
{% endif %}
{% for parameter in method.parameters.body %}
{% if parameter.type == 'file' and method.packaging %}
$cloudFunctionPath = realpath(__DIR__.'/../../../files/'.${{ parameter.name | caseCamel }});
Expand All @@ -74,19 +82,23 @@ $cli
$cFile = new \CURLFile(${{ parameter.name | caseCamel }}, {% if method.packaging %} 'application/x-gzip' {% else %} 'image/png' {% endif %}, basename(${{ parameter.name | caseCamel }}));
$params['{{ parameter.name }}'] = $cFile;
{% else %}
$params['{{ parameter.name }}'] = {% if parameter.type == 'integer' %}(int){% endif %} ${{ parameter.name | caseCamel }};
$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 %}
{% endif %}
{% endfor %}
{% if method.parameters.formData|length > 0 %}
/** Form Data Params */
{% endif %}
{% for parameter in method.parameters.formData %}
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel }};
$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 %}
{% endfor %}
{% if method.type == 'location' %}
$params['project'] = $client->getPreference('X-Appwrite-Project');
$params['key'] = $client->getPreference('X-Appwrite-Key');
$path = $client->getPreference(Client::PREFERENCE_ENDPOINT).$path . "?" . http_build_query($params);
Console::success($path);
{% else %}
$response = $client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
{% for parameter in method.parameters.header %}
'{{ parameter.name }}' => ${{ parameter.name | caseCamel }},
Expand Down
2 changes: 1 addition & 1 deletion templates/cli/composer.json.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"require": {
"php": ">=7.4.0",
"utopia-php/cli": "0.10.0",
"utopia-php/cli": "0.11.0",
"jc21/clitable": "^1.2"
},
"minimum-stability": "dev"
Expand Down
2 changes: 1 addition & 1 deletion templates/cli/docs/example.md.twig
Original file line number Diff line number Diff line change
@@ -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 %}
{{ language.params.executableName }} {{ service.name }} {{ method.name }} {% for parameter in method.parameters.all %}--{{ parameter.name }}="{{ parameter.example }}" {% endfor %}
22 changes: 11 additions & 11 deletions tests/languages/cli/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,54 @@
echo "\nTest Started\n";

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

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

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

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

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


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

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

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

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

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

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

0 comments on commit 3987782

Please sign in to comment.