Skip to content

Commit fcdb6d2

Browse files
authored
Merge pull request #104 from christyjacob4/feat-na-appwrite-cli
feat: Enable save preference in the client class
2 parents 81f4419 + 0ac5298 commit fcdb6d2

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require_once './vendor/autoload.php';
66
77
use Exception;
88
use Utopia\CLI\CLI;
9+
use Utopia\Validator\Mock;
910
use Utopia\CLI\Console;
1011
1112
const USER_PREFERENCES_FILE = __DIR__."/../../.preferences/.prefs.json";
@@ -143,10 +144,20 @@ $cli = new CLI();
143144
144145
$cli
145146
->task('init')
146-
->action(function() {
147-
/* Load user defaults from a json file if available
148-
Else Prompt the user to enter the details
147+
->param('endpoint', '', new Mock(), 'Your Appwrite endpoint', true)
148+
{% for header in spec.global.headers %}
149+
->param('{{ header.key | lower }}', '', new Mock(), '{{ header.description }}', true)
150+
{% endfor %}
151+
->action(function( $endpoint, {% for header in spec.global.headers %} ${{ header.key | lower }}{% if not loop.last %},{% endif %}{% endfor %} ) {
152+
/* Check if enviroment variables exist
153+
* Else prompt the user
149154
*/
155+
156+
putenv("endpoint=$endpoint");
157+
{% for header in spec.global.headers %}
158+
putenv("{{ header.name }}=${{ header.key | lower }}");
159+
{% endfor %}
160+
150161
if (!loadEnvVariables()) {
151162
promptUser();
152163
}

templates/cli/bin/init.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/bin/sh
22

3-
php /usr/local/code/app/{{ spec.title | caseUcfirst}}/Services/init.php init
3+
php /usr/local/code/app/{{ spec.title | caseUcfirst}}/Services/init.php init $@

templates/cli/src/Client.php.twig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,20 @@ class Client
121121
return true;
122122
}
123123
124+
/**
125+
* Function to write user preferences to
126+
* the JSON file
127+
*
128+
* @param string $filename
129+
* @return int
130+
*/
131+
function savePreferences(string $filename = self::USER_PREFERENCES_FILE): int
132+
{
133+
$jsondata = json_encode($this->preferences, JSON_PRETTY_PRINT);
134+
$result = file_put_contents($filename, $jsondata);
135+
return $result;
136+
}
137+
124138
/**
125139
* Check if all the preferences have been successfully loaded.
126140
*

0 commit comments

Comments
 (0)