Skip to content

Commit 25d7032

Browse files
committed
refactor(cli): Refactoring sudo and make it globally available
1 parent c91aa21 commit 25d7032

File tree

4 files changed

+41
-38
lines changed

4 files changed

+41
-38
lines changed

templates/cli/index.js.twig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@ program
2828
.version(version, "-v, --version")
2929
.option("--verbose", "Show complete error log")
3030
.option("--json", "Output in JSON format")
31+
.option("-f,--force", "Flag to confirm all warnings")
32+
.option("-a,--all", "Flag to push all resources")
33+
.option("--ids [id...]", "Flag to pass list of ids for a giving action")
3134
.on("option:json", () => {
3235
cliConfig.json = true;
3336
})
3437
.on("option:verbose", () => {
3538
cliConfig.verbose = true;
3639
})
40+
.on("option:force", () => {
41+
cliConfig.force = true;
42+
})
43+
.on("option:all", () => {
44+
cliConfig.all = true;
45+
})
46+
.on("option:ids", function() {
47+
cliConfig.ids = this.opts().ids;
48+
})
3749
.showSuggestionAfterError()
3850
{% if sdk.test != "true" %}
3951
.addCommand(login)

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { localConfig } = require("../config");
1414
const ID = require("../id");
1515
const { paginate } = require("../paginate");
1616
const { questionsPullProject, questionsPullFunction, questionsPullCollection } = require("../questions");
17-
const { success, log, actionRunner, commandDescriptions } = require("../parser");
17+
const { cliConfig, success, log, actionRunner, commandDescriptions } = require("../parser");
1818

1919
const pull = new Command("pull")
2020
.description(commandDescriptions['pull'])
@@ -164,12 +164,12 @@ const pullFunction = async () => {
164164
success();
165165
}
166166

167-
const pullCollection = async ({ all, databaseId } = {}) => {
167+
const pullCollection = async ({ databaseId } = {}) => {
168168
const databaseIds = [];
169169

170170
if (databaseId) {
171171
databaseIds.push(databaseId);
172-
} else if (all) {
172+
} else if (cliConfig.all) {
173173
let allDatabases = await databasesList({
174174
parseOutput: false
175175
})
@@ -265,7 +265,6 @@ pull
265265
.command("collection")
266266
.description("Pulling your {{ spec.title|caseUcfirst }} collections")
267267
.option(`--databaseId <databaseId>`, `Database ID`)
268-
.option(`--all`, `Flag to pullialize all databases`)
269268
.action(actionRunner(pullCollection))
270269

271270
pull

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

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { localConfig, globalConfig } = require("../config");
55
const { Spinner, SPINNER_ARC, SPINNER_DOTS } = require('../spinner');
66
const { paginate } = require('../paginate');
77
const { questionsPushBuckets, questionsPushTeams, questionsPushFunctions, questionsGetEntrypoint, questionsPushCollections, questionsConfirmPushCollections, questionsPushMessagingTopics } = require("../questions");
8-
const { actionRunner, success, log, error, commandDescriptions } = require("../parser");
8+
const { cliConfig, actionRunner, success, log, error, commandDescriptions } = require("../parser");
99
const { functionsGet, functionsCreate, functionsUpdate, functionsCreateDeployment, functionsUpdateDeployment, functionsGetDeployment, functionsListVariables, functionsDeleteVariable, functionsCreateVariable } = require('./functions');
1010
const {
1111
databasesGet,
@@ -232,7 +232,7 @@ const awaitPools = {
232232
},
233233
}
234234

235-
const pushResources = async ({ all, yes } = {}) => {
235+
const pushResources = async () => {
236236
const actions = {
237237
functions: pushFunction,
238238
collections: pushCollection,
@@ -241,27 +241,28 @@ const pushResources = async ({ all, yes } = {}) => {
241241
messages: pushMessagingTopic
242242
}
243243

244-
if (all) {
245-
Object.values(actions).forEach(action => action({ all: true, yes }));
244+
if (cliConfig.all) {
245+
Object.values(actions).forEach(action => action());
246246
} else {
247247
const answers = await inquirer.prompt(questionsPushResources[0]);
248+
248249
answers.resources.forEach((resource) => {
249250
const action = actions[resource];
250251
if (action !== undefined) {
251-
action({ all: true, yes });
252+
action();
252253
}
253254
})
254255
}
255256
};
256257

257-
const pushFunction = async ({ functionId, all, yes, async } = {}) => {
258+
const pushFunction = async ({ functionId, async } = {}) => {
258259
let response = {};
259260

260261
const functionIds = [];
261262

262263
if (functionId) {
263264
functionIds.push(functionId);
264-
} else if (all) {
265+
} else if (cliConfig.all) {
265266
const functions = localConfig.getFunctions();
266267
if (functions.length === 0) {
267268
throw new Error("No functions found in the current directory.");
@@ -299,7 +300,7 @@ const pushFunction = async ({ functionId, all, yes, async } = {}) => {
299300
}
300301

301302
if (func.variables) {
302-
func.pushVariables = yes;
303+
func.pushVariables = cliConfig.force;
303304

304305
try {
305306
const { total } = await functionsListVariables({
@@ -645,12 +646,12 @@ const createAttribute = async (databaseId, collectionId, attribute) => {
645646
}
646647
}
647648

648-
const pushCollection = async ({ all, yes } = {}) => {
649+
const pushCollection = async () => {
649650
let response = {};
650651

651652
const collections = [];
652653

653-
if (all) {
654+
if (cliConfig.all) {
654655
if (localConfig.getCollections().length === 0) {
655656
throw new Error("No collections found in the current directory. Run `{{ language.params.executableName }} pull collection` to fetch all your collections.");
656657
}
@@ -712,7 +713,7 @@ const pushCollection = async ({ all, yes } = {}) => {
712713

713714
log(`Collection ${collection.name} ( ${collection['$id']} ) already exists.`);
714715

715-
if (!yes) {
716+
if (!cliConfig.force) {
716717
const answers = await inquirer.prompt(questionsPushCollections[1])
717718
if (answers.override.toLowerCase() !== "yes") {
718719
log(`Received "${answers.override}". Skipping ${collection.name} ( ${collection['$id']} )`);
@@ -866,13 +867,13 @@ const pushCollection = async ({ all, yes } = {}) => {
866867
}
867868
}
868869

869-
const pushBucket = async ({ all, yes } = {}) => {
870+
const pushBucket = async () => {
870871
let response = {};
871872

872873
let bucketIds = [];
873874
const configBuckets = localConfig.getBuckets();
874875

875-
if (all) {
876+
if (cliConfig.all) {
876877
if (configBuckets.length === 0) {
877878
throw new Error("No buckets found in the current directory. Run `appwrite pull bucket` to fetch all your buckets.");
878879
}
@@ -901,7 +902,7 @@ const pushBucket = async ({ all, yes } = {}) => {
901902
})
902903
log(`Bucket ${bucket.name} ( ${bucket['$id']} ) already exists.`);
903904

904-
if (!yes) {
905+
if (!cliConfig.force) {
905906
const answers = await inquirer.prompt(questionsPushBuckets[1])
906907
if (answers.override.toLowerCase() !== "yes") {
907908
log(`Received "${answers.override}". Skipping ${bucket.name} ( ${bucket['$id']} )`);
@@ -953,13 +954,13 @@ const pushBucket = async ({ all, yes } = {}) => {
953954
}
954955
}
955956

956-
const pushTeam = async ({ all, yes } = {}) => {
957+
const pushTeam = async () => {
957958
let response = {};
958959

959960
let teamIds = [];
960961
const configTeams = localConfig.getTeams();
961962

962-
if (all) {
963+
if (cliConfig.all) {
963964
if (configTeams.length === 0) {
964965
throw new Error("No teams found in the current directory. Run `appwrite pull team` to fetch all your teams.");
965966
}
@@ -988,7 +989,7 @@ const pushTeam = async ({ all, yes } = {}) => {
988989
})
989990
log(`Team ${team.name} ( ${team['$id']} ) already exists.`);
990991

991-
if (!yes) {
992+
if (!cliConfig.force) {
992993
const answers = await inquirer.prompt(questionsPushTeams[1])
993994
if (answers.override.toLowerCase() !== "yes") {
994995
log(`Received "${answers.override}". Skipping ${team.name} ( ${team['$id']} )`);
@@ -1023,14 +1024,14 @@ const pushTeam = async ({ all, yes } = {}) => {
10231024
}
10241025
}
10251026

1026-
const pushMessagingTopic = async ({ all, yes } = {}) => {
1027+
const pushMessagingTopic = async () => {
10271028
let response = {};
10281029

10291030
let topicsIds = [];
10301031
const configTopics = localConfig.getMessagingTopics();
1031-
let overrideExisting = yes;
1032+
let overrideExisting = cliConfig.force;
10321033

1033-
if (all) {
1034+
if (cliConfig.all) {
10341035
if (configTopics.length === 0) {
10351036
throw new Error("No topics found in the current directory. Run `appwrite pull topics` to pull all your messaging topics.");
10361037
}
@@ -1049,7 +1050,7 @@ const pushMessagingTopic = async ({ all, yes } = {}) => {
10491050
topics.push(...idTopic);
10501051
}
10511052

1052-
if (!yes) {
1053+
if (!cliConfig.force) {
10531054
const answers = await inquirer.prompt(questionsPushMessagingTopics[1])
10541055
if (answers.override.toLowerCase() === "yes") {
10551056
overrideExisting = true;
@@ -1102,45 +1103,33 @@ const pushMessagingTopic = async ({ all, yes } = {}) => {
11021103

11031104
const push = new Command("push")
11041105
.description(commandDescriptions['push'])
1105-
.option(`--all`, `Flag to push all resources`)
1106-
.option(`--yes`, `Flag to confirm all warnings`)
11071106
.action(actionRunner(pushResources));
11081107

11091108
push
11101109
.command("function")
11111110
.description("Push functions in the current directory.")
11121111
.option(`--functionId <functionId>`, `Function ID`)
1113-
.option(`--all`, `Flag to push all functions`)
1114-
.option(`--yes`, `Flag to confirm all warnings`)
11151112
.option(`--async`, `Don't wait for functions deployments status`)
11161113
.action(actionRunner(pushFunction));
11171114

11181115
push
11191116
.command("collection")
11201117
.description("Push collections in the current project.")
1121-
.option(`--all`, `Flag to push all collections`)
1122-
.option(`--yes`, `Flag to confirm all warnings`)
11231118
.action(actionRunner(pushCollection));
11241119

11251120
push
11261121
.command("bucket")
11271122
.description("Push buckets in the current project.")
1128-
.option(`--all`, `Flag to push all buckets`)
1129-
.option(`--yes`, `Flag to confirm all warnings`)
11301123
.action(actionRunner(pushBucket));
11311124

11321125
push
11331126
.command("team")
11341127
.description("Push teams in the current project.")
1135-
.option(`--all`, `Flag to push all teams`)
1136-
.option(`--yes`, `Flag to confirm all warnings`)
11371128
.action(actionRunner(pushTeam));
11381129

11391130
push
11401131
.command("topic")
11411132
.description("Push messaging topics in the current project.")
1142-
.option(`--all`, `Flag to deploy all topics`)
1143-
.option(`--yes`, `Flag to confirm all warnings`)
11441133
.action(actionRunner(pushMessagingTopic));
11451134

11461135
module.exports = {

templates/cli/lib/parser.js.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ const { description } = require('../package.json');
55

66
const cliConfig = {
77
verbose: false,
8-
json: false
8+
json: false,
9+
force: false,
10+
all: false,
11+
ids: []
912
};
1013

1114
const parse = (data) => {

0 commit comments

Comments
 (0)