Skip to content

Commit 6ce7b0a

Browse files
committed
Merge remote-tracking branch 'origin/feat-cli-sudo' into feat-cli-sudo
# Conflicts: # templates/cli/lib/commands/push.js.twig # templates/cli/lib/questions.js.twig
2 parents 5a0111e + 69c5d66 commit 6ce7b0a

File tree

4 files changed

+70
-38
lines changed

4 files changed

+70
-38
lines changed

templates/cli/index.js.twig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ program
3030
.option("--json", "Output in JSON format")
3131
.option("-f,--force", "Flag to confirm all warnings")
3232
.option("-a,--all", "Flag to push all resources")
33-
.option("--ids [id...]", "Flag to pass list of ids for a giving action")
33+
.option("--id [id...]", "Flag to pass list of ids for a giving action")
34+
.option("--report", "Enable reporting when cli is crashing")
3435
.on("option:json", () => {
3536
cliConfig.json = true;
3637
})
3738
.on("option:verbose", () => {
3839
cliConfig.verbose = true;
3940
})
41+
.on("option:report", () => {
42+
cliConfig.report = true;
43+
cliConfig.reportData = { data: this };
44+
})
4045
.on("option:force", () => {
4146
cliConfig.force = true;
4247
})
4348
.on("option:all", () => {
4449
cliConfig.all = true;
4550
})
46-
.on("option:ids", function() {
47-
cliConfig.ids = this.opts().ids;
51+
.on("option:id", function() {
52+
cliConfig.ids = this.opts().id;
4853
})
4954
.showSuggestionAfterError()
5055
{% if sdk.test != "true" %}

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

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const { databasesGet, databasesListCollections, databasesList } = require("./dat
1111
const { storageListBuckets } = require("./storage");
1212
const { sdkForConsole } = require("../sdks");
1313
const { localConfig } = require("../config");
14-
const ID = require("../id");
14+
const ID = require("../id");
1515
const { paginate } = require("../paginate");
1616
const { questionsPullProject, questionsPullFunction, questionsPullCollection } = require("../questions");
1717
const { cliConfig, success, log, actionRunner, commandDescriptions } = require("../parser");
@@ -164,26 +164,12 @@ const pullFunction = async () => {
164164
success();
165165
}
166166

167-
const pullCollection = async ({ databaseId } = {}) => {
168-
const databaseIds = [];
167+
const pullCollection = async () => {
168+
const databasesIds = (await inquirer.prompt(questionsPullCollection)).databases;
169169

170-
if (databaseId) {
171-
databaseIds.push(databaseId);
172-
} else if (cliConfig.all) {
173-
let allDatabases = await databasesList({
174-
parseOutput: false
175-
})
176-
177-
databaseIds.push(...allDatabases.databases.map((d) => d.$id));
178-
}
170+
for (let id of databasesIds) {
171+
const databaseId = id.value ? id.value : id;
179172

180-
if (databaseIds.length <= 0) {
181-
let answers = await inquirer.prompt(questionsPullCollection)
182-
if (!answers.databases) process.exit(1)
183-
databaseIds.push(...answers.databases);
184-
}
185-
186-
for (const databaseId of databaseIds) {
187173
const database = await databasesGet({
188174
databaseId,
189175
parseOutput: false
@@ -198,14 +184,15 @@ const pullCollection = async ({ databaseId } = {}) => {
198184

199185
log(`Found ${total} collections`);
200186

201-
collections.forEach(async collection => {
187+
collections.map(async collection => {
202188
log(`Fetching ${collection.name} ...`);
203189
localConfig.addCollection({
204190
...collection,
205191
'$createdAt': undefined,
206-
'$updatedAt': undefined,
192+
'$updatedAt': undefined
207193
});
208194
});
195+
209196
}
210197

211198
success();
@@ -264,7 +251,6 @@ pull
264251
pull
265252
.command("collection")
266253
.description("Pulling your {{ spec.title|caseUcfirst }} collections")
267-
.option(`--databaseId <databaseId>`, `Database ID`)
268254
.action(actionRunner(pullCollection))
269255

270256
pull

templates/cli/lib/paginate.js.twig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@ const paginate = async (action, args = {}, limit = 100, wrapper = '') => {
55

66
while (true) {
77
const offset = pageNumber * limit;
8-
8+
const additionalQueries = [];
9+
if (args.queries) {
10+
additionalQueries.push(...args.queries);
11+
}
912
// Merge the limit and offset into the args
1013
const response = await action({
1114
...args,
1215
queries: [
16+
...additionalQueries,
1317
JSON.stringify({ method: 'limit', values: [limit] }),
1418
JSON.stringify({ method: 'offset', values: [offset] })
1519
]
@@ -48,4 +52,4 @@ const paginate = async (action, args = {}, limit = 100, wrapper = '') => {
4852

4953
module.exports = {
5054
paginate
51-
};
55+
};

templates/cli/lib/parser.js.twig

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ const chalk = require('chalk');
22
const commander = require('commander');
33
const Table = require('cli-table3');
44
const { description } = require('../package.json');
5+
const { globalConfig } = require("./config.js");
6+
const os = require('os');
57

68
const cliConfig = {
7-
verbose: false,
8-
json: false,
9-
force: false,
10-
all: false,
11-
ids: []
9+
verbose: false,
10+
json: false,
11+
force: false,
12+
all: false,
13+
ids: [],
14+
report: false,
15+
reportData: {}
1216
};
1317

1418
const parse = (data) => {
@@ -113,17 +117,50 @@ const drawJSON = (data) => {
113117
}
114118

115119
const parseError = (err) => {
116-
if(cliConfig.verbose) {
117-
console.error(err);
118-
}
120+
if (cliConfig.report) {
121+
(async () => {
122+
let appwriteVersion = 'unknown';
123+
const isCloud = globalConfig.getEndpoint().includes('cloud.appwrite.io') ? 'Yes' : 'No';
124+
125+
try {
126+
const res = await fetch(`${globalConfig.getEndpoint()}/health/version`);
127+
const json = await res.json();
128+
appwriteVersion = json.version;
129+
} catch {
130+
}
131+
132+
const version = '0.16.0';
133+
const stepsToReproduce = encodeURIComponent(`Running \`appwrite ${cliConfig.reportData.data.args}\``);
134+
const yourEnvironment = encodeURI(`CLI version: ${version}\nOperation System: ${os.type()}\nAppwrite version: ${appwriteVersion}\nIs Cloud: ${isCloud}`)
119135

120-
error(err.message);
136+
const stack = encodeURIComponent('```\n' + err.stack + '\n```').replaceAll('(', '').replaceAll(')', '');
137+
138+
const githubIssueUrl = `https://github.com/appwrite/appwrite/issues/new?labels=bug&template=bug.yaml&title=%F0%9F%90%9B+Bug+Report%3A+CLI+error:+${encodeURI(err.message)}&actual-behavior=CLI%20Error:%0A${stack}&steps-to-reproduce=${stepsToReproduce}&environment=${yourEnvironment}`;
139+
140+
log(`To report this error you can:\n - Create a support ticket in our Discord server https://appwrite.io/discord \n - Create an issue in our Github\n ${githubIssueUrl}\n`);
141+
142+
process.exit(1);
143+
})()
144+
}
145+
else {
146+
if (cliConfig.verbose) {
147+
console.error(err);
148+
} else {
149+
error(err.message);
150+
}
151+
process.exit(1);
152+
}
121153

122-
process.exit(1)
123154
}
124155

125156
const actionRunner = (fn) => {
126-
return (...args) => fn(...args).catch(parseError);
157+
return (...args) => {
158+
if (cliConfig.all && (Array.isArray(cliConfig.ids) && cliConfig.ids.length !== 0)) {
159+
error(`The '--all' and '--id' flags cannot be used together.`);
160+
process.exit(1);
161+
}
162+
return fn(...args).catch(parseError)
163+
};
127164
}
128165

129166
const parseInteger = (value) => {

0 commit comments

Comments
 (0)