Skip to content

Commit 3ab704a

Browse files
Merge pull request #555 from appwrite/feat-cli-func-secrets
Feat: CLI function variables
2 parents 6e824e0 + 133b2fd commit 3ab704a

File tree

3 files changed

+67
-18
lines changed

3 files changed

+67
-18
lines changed

templates/cli/lib/commands/deploy.js.twig

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const inquirer = require("inquirer");
22
const JSONbig = require("json-bigint")({ storeAsString: false });
33
const { Command } = require("commander");
44
const { localConfig } = require("../config");
5-
const { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections } = require("../questions");
5+
const { questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
66
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
7-
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment } = require('./functions');
7+
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
88
const {
99
databasesGet,
1010
databasesCreate,
@@ -39,7 +39,7 @@ const awaitPools = {
3939
const { attributes: remoteAttributes } = await databasesListAttributes({
4040
databaseId,
4141
collectionId,
42-
limit: 100,
42+
queries: [ 'limit(100)' ],
4343
parseOutput: false
4444
});
4545

@@ -59,7 +59,7 @@ const awaitPools = {
5959
const { indexes: remoteIndexes } = await databasesListIndexes({
6060
databaseId,
6161
collectionId,
62-
limit: 100,
62+
queries: [ 'limit(100)' ],
6363
parseOutput: false
6464
});
6565

@@ -79,7 +79,7 @@ const awaitPools = {
7979
const { attributes: remoteAttributes } = await databasesListAttributes({
8080
databaseId,
8181
collectionId,
82-
limit: 100,
82+
queries: [ 'limit(100)' ],
8383
parseOutput: false
8484
});
8585

@@ -111,7 +111,7 @@ const awaitPools = {
111111
const { indexes: remoteIndexes } = await databasesListIndexes({
112112
databaseId,
113113
collectionId,
114-
limit: 100,
114+
queries: [ 'limit(100)' ],
115115
parseOutput: false
116116
});
117117

@@ -142,7 +142,7 @@ const deploy = new Command("deploy")
142142
command.help()
143143
}));
144144

145-
const deployFunction = async ({ functionId, all } = {}) => {
145+
const deployFunction = async ({ functionId, all, yes } = {}) => {
146146
let response = {};
147147

148148
const functionIds = [];
@@ -160,7 +160,7 @@ const deployFunction = async ({ functionId, all } = {}) => {
160160
}
161161

162162
if(functionIds.length <= 0) {
163-
const answers = await inquirer.prompt(questionsDeployFunctions);
163+
const answers = await inquirer.prompt(questionsDeployFunctions[0]);
164164
functionIds.push(...answers.functions);
165165
}
166166

@@ -188,6 +188,46 @@ const deployFunction = async ({ functionId, all } = {}) => {
188188
throw new Error(`Runtime missmatch! (local=${func.runtime},remote=${response.runtime}) Please delete remote function or update your appwrite.json`);
189189
}
190190

191+
if(func.variables) {
192+
// Delete existing variables
193+
194+
// TODO: Pagination?
195+
const { variables: remoteVariables } = await functionsListVariables({
196+
functionId: func['$id'],
197+
queries: [ 'limit(100)' ],
198+
parseOutput: false
199+
});
200+
201+
if(remoteVariables.length > 0) {
202+
if(!yes) {
203+
const variableAnswers = await inquirer.prompt(questionsDeployFunctions[1])
204+
205+
if (variableAnswers.override !== "YES") {
206+
log(`Received "${variableAnswers.override}". Skipping ${func.name} ( ${func['$id']} )`);
207+
continue;
208+
}
209+
}
210+
211+
await Promise.all(remoteVariables.map(async remoteVariable => {
212+
await functionsDeleteVariable({
213+
functionId: func['$id'],
214+
variableId: remoteVariable['$id'],
215+
parseOutput: false
216+
});
217+
}));
218+
}
219+
220+
// Deploy local variables
221+
await Promise.all(Object.keys(func.variables).map(async localVariableKey => {
222+
await functionsCreateVariable({
223+
functionId: func['$id'],
224+
key: localVariableKey,
225+
value: func.variables[localVariableKey],
226+
parseOutput: false
227+
});
228+
}));
229+
}
230+
191231
response = await functionsUpdate({
192232
functionId: func['$id'],
193233
name: func.name,
@@ -359,7 +399,7 @@ const createAttribute = async (databaseId, collectionId, attribute) => {
359399
}
360400
}
361401

362-
const deployCollection = async ({ all } = {}) => {
402+
const deployCollection = async ({ all, yes } = {}) => {
363403
let response = {};
364404

365405
let collectionIds = [];
@@ -413,10 +453,12 @@ const deployCollection = async ({ all } = {}) => {
413453
})
414454
log(`Collection ${collection.name} ( ${collection['$id']} ) already exists.`);
415455

416-
answers = await inquirer.prompt(questionsDeployCollections[1])
417-
if (answers.override !== "YES") {
418-
log(`Received "${answers.override}". Skipping ${collection.name} ( ${collection['$id']} )`);
419-
continue;
456+
if(!yes) {
457+
answers = await inquirer.prompt(questionsDeployCollections[1])
458+
if (answers.override !== "YES") {
459+
log(`Received "${answers.override}". Skipping ${collection.name} ( ${collection['$id']} )`);
460+
continue;
461+
}
420462
}
421463

422464
log(`Updating attributes ... `);
@@ -425,7 +467,7 @@ const deployCollection = async ({ all } = {}) => {
425467
const { indexes: remoteIndexes } = await databasesListIndexes({
426468
databaseId,
427469
collectionId: collection['$id'],
428-
limit: 100,
470+
queries: [ 'limit(100)' ],
429471
parseOutput: false
430472
});
431473

@@ -447,7 +489,7 @@ const deployCollection = async ({ all } = {}) => {
447489
const { attributes: remoteAttributes } = await databasesListAttributes({
448490
databaseId,
449491
collectionId: collection['$id'],
450-
limit: 100,
492+
queries: [ 'limit(100)' ],
451493
parseOutput: false
452494
});
453495

@@ -556,12 +598,14 @@ deploy
556598
.description("Deploy functions in the current directory.")
557599
.option(`--functionId <functionId>`, `Function ID`)
558600
.option(`--all`, `Flag to deploy all functions`)
601+
.option(`--yes`, `Flag to confirm all warnings`)
559602
.action(actionRunner(deployFunction));
560603

561604
deploy
562605
.command("collection")
563606
.description("Deploy collections in the current project.")
564607
.option(`--all`, `Flag to deploy all functions`)
608+
.option(`--yes`, `Flag to confirm all warnings`)
565609
.action(actionRunner(deployCollection));
566610

567611
module.exports = {

templates/cli/lib/commands/init.js.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ const initCollection = async ({ all, databaseId } = {}) => {
170170
// TODO: Pagination?
171171
let response = await databasesListCollections({
172172
databaseId,
173-
limit: 100,
173+
queries: [ 'limit(100)' ],
174174
parseOutput: false
175175
})
176176

templates/cli/lib/questions.js.twig

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ const questionsInitProject = [
113113
type: "input",
114114
name: "id",
115115
message: "What ID would you like to have for your project?",
116-
default: "myAwesomeProject",
116+
default: "unique()",
117117
when(answers) {
118118
return answers.start == "new";
119119
},
@@ -251,7 +251,12 @@ const questionsDeployFunctions = [
251251
})
252252
return choices;
253253
}
254-
}
254+
},
255+
{
256+
type: "input",
257+
name: "override",
258+
message: 'Are you sure you want to override this function's variables? This can lead to loss of secrets! Type "YES" to confirm.'
259+
},
255260
]
256261

257262
const questionsDeployCollections = [

0 commit comments

Comments
 (0)