Skip to content

Commit 7df24c2

Browse files
committed
CLI implement buckets logic
1 parent ed8d3da commit 7df24c2

File tree

4 files changed

+173
-1
lines changed

4 files changed

+173
-1
lines changed

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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, questionsConfirmDeployCollections } = require("../questions");
5+
const { questionsDeployBuckets, questionsDeployFunctions, questionsGetEntrypoint, questionsDeployCollections, questionsConfirmDeployCollections } = require("../questions");
66
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
77
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
88
const {
@@ -25,6 +25,9 @@ const {
2525
databasesListIndexes,
2626
databasesDeleteIndex
2727
} = require("./databases");
28+
const {
29+
storageGetBucket, storageUpdateBucket, storageCreateBucket
30+
} = require("./storage");
2831

2932
const POOL_DEBOUNCE = 2000; // in milliseconds
3033
const POOL_MAX_DEBOUNCES = 30;
@@ -593,6 +596,93 @@ const deployCollection = async ({ all, yes } = {}) => {
593596
}
594597
}
595598

599+
const deployBucket = async ({ all, yes } = {}) => {
600+
let response = {};
601+
602+
let bucketIds = [];
603+
const configBuckets = localConfig.getBuckets();
604+
605+
if(all) {
606+
if (configBuckets.length === 0) {
607+
throw new Error("No buckets found in the current directory. Run `appwrite init bucket` to fetch all your buckets.");
608+
}
609+
bucketIds.push(...configBuckets.map((c) => c.$id));
610+
}
611+
612+
if(bucketIds.length <= 0) {
613+
let answers = await inquirer.prompt(questionsDeployBuckets[0])
614+
bucketIds.push(...answers.buckets);
615+
}
616+
617+
let buckets = [];
618+
619+
for(const bucketId of bucketIds) {
620+
const idBuckets = configBuckets.filter((b) => b.$id === bucketId);
621+
buckets.push(...idBuckets);
622+
}
623+
624+
for (let bucket of buckets) {
625+
log(`Deploying bucket ${bucket.name} ( ${bucket['$id']} )`)
626+
627+
try {
628+
response = await storageGetBucket({
629+
bucketId: bucket['$id'],
630+
parseOutput: false,
631+
})
632+
log(`Bucket ${bucket.name} ( ${bucket['$id']} ) already exists.`);
633+
634+
if(!yes) {
635+
answers = await inquirer.prompt(questionsDeployBuckets[1])
636+
if (answers.override !== "YES") {
637+
log(`Received "${answers.override}". Skipping ${bucket.name} ( ${bucket['$id']} )`);
638+
continue;
639+
}
640+
}
641+
642+
log(`Updating bucket ...`)
643+
644+
await storageUpdateBucket({
645+
bucketId: bucket['$id'],
646+
name: bucket.name,
647+
permissions: bucket['$permissions'],
648+
fileSecurity: bucket.fileSecurity,
649+
enabled: bucket.enabled,
650+
maximumFileSize: bucket.maximumFileSize,
651+
allowedFileExtensions: bucket.allowedFileExtensions,
652+
compression: bucket.compression,
653+
encryption: bucket.encryption,
654+
antivirus: bucket.antivirus,
655+
compression: bucket.compression,
656+
parseOutput: false
657+
});
658+
659+
success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
660+
} catch (e) {
661+
if (e.code == 404) {
662+
log(`Bucket ${bucket.name} does not exist in the project. Creating ... `);
663+
664+
response = await storageCreateBucket({
665+
bucketId: bucket['$id'],
666+
name: bucket.name,
667+
permissions: bucket['$permissions'],
668+
fileSecurity: bucket.fileSecurity,
669+
enabled: bucket.enabled,
670+
maximumFileSize: bucket.maximumFileSize,
671+
allowedFileExtensions: bucket.allowedFileExtensions,
672+
compression: bucket.compression,
673+
encryption: bucket.encryption,
674+
antivirus: bucket.antivirus,
675+
parseOutput: false
676+
})
677+
678+
success(`Deployed ${bucket.name} ( ${bucket['$id']} )`);
679+
} else {
680+
throw e;
681+
}
682+
}
683+
}
684+
}
685+
596686
deploy
597687
.command("function")
598688
.description("Deploy functions in the current directory.")
@@ -608,6 +698,13 @@ deploy
608698
.option(`--yes`, `Flag to confirm all warnings`)
609699
.action(actionRunner(deployCollection));
610700

701+
deploy
702+
.command("bucket")
703+
.description("Deploy buckets in the current project.")
704+
.option(`--all`, `Flag to deploy all buckets`)
705+
.option(`--yes`, `Flag to confirm all warnings`)
706+
.action(actionRunner(deployBucket));
707+
611708
module.exports = {
612709
deploy
613710
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { teamsCreate } = require("./teams");
77
const { projectsCreate } = require("./projects");
88
const { functionsCreate } = require("./functions");
99
const { databasesListCollections, databasesList } = require("./databases");
10+
const { storageListBuckets } = require("./storage");
1011
const { sdkForConsole } = require("../sdks");
1112
const { localConfig } = require("../config");
1213
const { questionsInitProject, questionsInitFunction, questionsInitCollection } = require("../questions");
@@ -186,6 +187,24 @@ const initCollection = async ({ all, databaseId } = {}) => {
186187
success();
187188
}
188189

190+
const initBucket = async () => {
191+
// TODO: Pagination?
192+
let response = await storageListBuckets({
193+
queries: [ 'limit(100)' ],
194+
parseOutput: false
195+
})
196+
197+
let buckets = response.buckets;
198+
log(`Found ${buckets.length} buckets`);
199+
200+
buckets.forEach(async bucket => {
201+
log(`Fetching ${bucket.name} ...`);
202+
localConfig.addBucket(bucket);
203+
});
204+
205+
success();
206+
}
207+
189208
init
190209
.command("project")
191210
.description("Initialise your {{ spec.title|caseUcfirst }} project")
@@ -203,6 +222,11 @@ init
203222
.option(`--all`, `Flag to initialize all databases`)
204223
.action(actionRunner(initCollection))
205224

225+
init
226+
.command("bucket")
227+
.description("Initialise your Appwrite buckets")
228+
.action(actionRunner(initBucket))
229+
206230
module.exports = {
207231
init,
208232
};

templates/cli/lib/config.js.twig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ class Local extends Config {
134134
return this.get("collections");
135135
}
136136

137+
getBuckets() {
138+
if (!this.has("buckets")) {
139+
return [];
140+
}
141+
return this.get("buckets");
142+
}
143+
137144
getCollection($id) {
138145
if (!this.has("collection")) {
139146
return {};
@@ -166,6 +173,23 @@ class Local extends Config {
166173
this.set("collections", collections);
167174
}
168175

176+
addBucket(props) {
177+
if (!this.has("buckets")) {
178+
this.set("buckets", []);
179+
}
180+
181+
let buckets = this.get("buckets");
182+
for (let i = 0; i < buckets.length; i++) {
183+
if (buckets[i]['$id'] == props['$id']) {
184+
buckets[i] = props;
185+
this.set("buckets", buckets);
186+
return;
187+
}
188+
}
189+
buckets.push(props);
190+
this.set("buckets", buckets);
191+
}
192+
169193
getProject() {
170194
if (!this.has("projectId") || !this.has("projectName")) {
171195
return {};

templates/cli/lib/questions.js.twig

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,32 @@ const questionsDeployCollections = [
285285
},
286286
]
287287

288+
const questionsDeployBuckets = [
289+
{
290+
type: "checkbox",
291+
name: "buckets",
292+
message: "Which buckets would you like to deploy?",
293+
choices: () => {
294+
let buckets = localConfig.getBuckets();
295+
if (buckets.length === 0) {
296+
throw new Error("No buckets found in the current directory. Run `appwrite init bucket` to fetch all your buckets.");
297+
}
298+
let choices = buckets.map((bucket, idx) => {
299+
return {
300+
name: `${bucket.name} (${bucket['$id']})`,
301+
value: bucket.$id
302+
}
303+
})
304+
return choices;
305+
}
306+
},
307+
{
308+
type: "input",
309+
name: "override",
310+
message: 'Are you sure you want to override this bucket? This can lead to loss of data! Type "YES" to confirm.'
311+
},
312+
]
313+
288314
const questionsGetEntrypoint = [
289315
{
290316
type: "input",
@@ -307,5 +333,6 @@ module.exports = {
307333
questionsInitCollection,
308334
questionsDeployFunctions,
309335
questionsDeployCollections,
336+
questionsDeployBuckets,
310337
questionsGetEntrypoint
311338
};

0 commit comments

Comments
 (0)