Skip to content

Commit dfcaafb

Browse files
Merge pull request #425 from appwrite/feat-endpoint-check-cli
Feat: Appwrite CLI validate endpoint on login and client --endpoint commands
2 parents 0e8a69e + 35f89f4 commit dfcaafb

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const inquirer = require("inquirer");
22
const { Command } = require("commander");
3+
const Client = require("../client");
34
const { sdkForConsole } = require("../sdks");
45
const { globalConfig, localConfig } = require("../config");
56
const { actionRunner, success, parseBool, commandDescriptions, log, parse } = require("../parser");
@@ -67,13 +68,19 @@ const client = new Command("client")
6768
if (endpoint !== undefined) {
6869
try {
6970
let url = new URL(endpoint);
70-
if (url.protocol === "http:" || url.protocol === "https:") {
71-
globalConfig.setEndpoint(endpoint);
72-
} else {
71+
if (url.protocol !== "http:" && url.protocol !== "https:") {
7372
throw new Error();
7473
}
74+
75+
let client = new Client().setEndpoint(endpoint);
76+
let response = await client.call('GET', '/health/version');
77+
if(!response.version) {
78+
throw new Error();
79+
}
80+
81+
globalConfig.setEndpoint(endpoint);
7582
} catch (_) {
76-
throw new Error("Invalid endpoint");
83+
throw new Error("Invalid endpoint or your Appwrite server is not running as expected.");
7784
}
7885
}
7986

templates/cli/lib/sdks.js.twig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ const questionGetEndpoint = [
77
type: "input",
88
name: "endpoint",
99
message: "Enter the endpoint of your {{ spec.title|caseUcfirst }} server",
10-
default: "http://localhost/v1"
10+
default: "http://localhost/v1",
11+
async validate(value) {
12+
if (!value) {
13+
return "Please enter a valid endpoint.";
14+
}
15+
let client = new Client().setEndpoint(value);
16+
try {
17+
let response = await client.call('get', '/health/version');
18+
if (response.version) {
19+
return true;
20+
} else {
21+
throw new Error();
22+
}
23+
} catch (error) {
24+
return "Invalid endpoint or your Appwrite server is not running as expected.";
25+
}
26+
}
1127
}
1228
]
1329

0 commit comments

Comments
 (0)