Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit 16b0ba8

Browse files
committed
merge
2 parents 3c4b439 + 0b2086d commit 16b0ba8

29 files changed

Lines changed: 1031 additions & 0 deletions

lib/commands/gateway/create.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const error_1 = require("../../error");
13+
const gateway_1 = require("../../gateway");
14+
const function_1 = require("../../function");
15+
const utils_1 = require("../../utils");
16+
function createGw(ctx, gatewayPath, commandOptions) {
17+
return __awaiter(this, void 0, void 0, function* () {
18+
const { envId } = ctx;
19+
if (!gatewayPath) {
20+
throw new error_1.CloudBaseError('请指定需要创建的网关路径!');
21+
}
22+
const { function: functionName } = commandOptions;
23+
if (functionName) {
24+
const loading = utils_1.loadingFactory();
25+
loading.start(`[${functionName}] 云函数网关创建中...`);
26+
try {
27+
const functionList = yield function_1.listFunction({
28+
envId,
29+
limit: 100
30+
});
31+
const isExisted = functionList.filter(item => item.FunctionName === functionName);
32+
if (isExisted.length <= 0) {
33+
throw new error_1.CloudBaseError(`[${functionName}] 云函数不存在!`);
34+
}
35+
const res = yield gateway_1.createFunctionGateway({
36+
envId,
37+
path: gatewayPath,
38+
functionName
39+
});
40+
loading.succeed(`[${functionName}:${res.APIId}] 云函数网关创建成功!`);
41+
}
42+
catch (e) {
43+
loading.stop();
44+
throw e;
45+
}
46+
return;
47+
}
48+
throw new error_1.CloudBaseError('请指定需要创建的网关类型!');
49+
});
50+
}
51+
exports.createGw = createGw;

lib/commands/gateway/delete.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const error_1 = require("../../error");
13+
const gateway_1 = require("../../gateway");
14+
const utils_1 = require("../../utils");
15+
function deleteGw(ctx, commandOptions) {
16+
return __awaiter(this, void 0, void 0, function* () {
17+
const { envId } = ctx;
18+
const { gatewayPath, gatewayId } = commandOptions;
19+
if (!gatewayPath && !gatewayId) {
20+
throw new error_1.CloudBaseError('请指定需要删除的网关路径或ID!');
21+
}
22+
const loading = utils_1.loadingFactory();
23+
loading.start(`[${gatewayPath || gatewayId}] 云函数网关删除中...`);
24+
try {
25+
yield gateway_1.deleteGateway({
26+
envId,
27+
path: gatewayPath,
28+
gatewayId
29+
});
30+
loading.succeed(`[${gatewayPath || gatewayId}] 云函数网关删除成功!`);
31+
}
32+
catch (e) {
33+
loading.stop();
34+
throw e;
35+
}
36+
});
37+
}
38+
exports.deleteGw = deleteGw;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const error_1 = require("../../error");
13+
const gateway_1 = require("../../gateway");
14+
const utils_1 = require("../../utils");
15+
function bindGwDomain(ctx, domain) {
16+
return __awaiter(this, void 0, void 0, function* () {
17+
const { envId } = ctx;
18+
if (!domain) {
19+
throw new error_1.CloudBaseError('请指定需要绑定的自定义网关域名!');
20+
}
21+
const loading = utils_1.loadingFactory();
22+
loading.start(`自定义网关域名 [${domain}] 绑定中...`);
23+
try {
24+
yield gateway_1.bindGatewayDomain({
25+
envId,
26+
domain
27+
});
28+
loading.succeed(`自定义网关域名[${domain}] 绑定成功!`);
29+
}
30+
catch (e) {
31+
loading.stop();
32+
throw e;
33+
}
34+
});
35+
}
36+
exports.bindGwDomain = bindGwDomain;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const error_1 = require("../../error");
13+
const gateway_1 = require("../../gateway");
14+
const utils_1 = require("../../utils");
15+
function queryGwDomain(ctx, commandOptions) {
16+
return __awaiter(this, void 0, void 0, function* () {
17+
const { envId } = ctx;
18+
const { domain: domainName } = commandOptions;
19+
if (!envId && !domainName) {
20+
throw new error_1.CloudBaseError('请指定需要查询的环境ID或网关域名!');
21+
}
22+
const loading = utils_1.loadingFactory();
23+
loading.start(`查询自定义网关域名中...`);
24+
try {
25+
const res = yield gateway_1.queryGatewayDomain({
26+
envId,
27+
domain: domainName
28+
});
29+
loading.succeed(`查询自定义网关域名成功!`);
30+
const head = ['CustomDomain', 'CreateTime'];
31+
const tableData = res.ServiceSet.map(item => [
32+
item.Domain,
33+
item.OpenTime
34+
]);
35+
utils_1.printHorizontalTable(head, tableData);
36+
}
37+
catch (e) {
38+
loading.stop();
39+
throw e;
40+
}
41+
});
42+
}
43+
exports.queryGwDomain = queryGwDomain;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const error_1 = require("../../error");
13+
const gateway_1 = require("../../gateway");
14+
const utils_1 = require("../../utils");
15+
function unbindGwDomain(ctx, domain) {
16+
return __awaiter(this, void 0, void 0, function* () {
17+
const { envId } = ctx;
18+
if (!domain) {
19+
throw new error_1.CloudBaseError('请指定需要解绑的自定义网关域名!');
20+
}
21+
const loading = utils_1.loadingFactory();
22+
loading.start(`自定义网关域名 [${domain}] 解绑中...`);
23+
try {
24+
yield gateway_1.unbindGatewayDomain({
25+
envId,
26+
domain
27+
});
28+
loading.succeed(`自定义网关域名 [${domain}] 解绑成功!`);
29+
}
30+
catch (e) {
31+
loading.stop();
32+
throw e;
33+
}
34+
});
35+
}
36+
exports.unbindGwDomain = unbindGwDomain;

lib/commands/gateway/index.js

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
const commander_1 = __importDefault(require("commander"));
16+
const utils_1 = require("../../utils");
17+
const error_1 = require("../../error");
18+
const create_1 = require("./create");
19+
const delete_1 = require("./delete");
20+
const query_1 = require("./query");
21+
const domain_bind_1 = require("./domain-bind");
22+
const domain_unbind_1 = require("./domain-unbind");
23+
const domain_query_1 = require("./domain-query");
24+
function getGatewayContext(envId, configPath) {
25+
return __awaiter(this, void 0, void 0, function* () {
26+
const cloudBaseConfig = yield utils_1.resolveCloudBaseConfig(configPath);
27+
const assignEnvId = envId || cloudBaseConfig.envId;
28+
if (!assignEnvId) {
29+
throw new error_1.CloudBaseError('未识别到有效的环境 Id 变量,请在项目根目录进行操作或通过 envId 参数指定环境 Id');
30+
}
31+
const ctx = {
32+
envId: assignEnvId,
33+
config: cloudBaseConfig
34+
};
35+
return ctx;
36+
});
37+
}
38+
const commands = [
39+
{
40+
cmd: 'gateway:create <gatewayPath> [envId]',
41+
options: [
42+
{
43+
flags: '-f, --function <function>',
44+
desc: '创建云函数网关.'
45+
}
46+
],
47+
desc: '创建云函数网关.',
48+
handler: (gatewayPath, envId, options) => __awaiter(void 0, void 0, void 0, function* () {
49+
const { configFile } = options.parent;
50+
const ctx = yield getGatewayContext(envId, configFile);
51+
yield create_1.createGw(ctx, gatewayPath, options);
52+
})
53+
},
54+
{
55+
cmd: 'gateway:delete [envId]',
56+
options: [
57+
{
58+
flags: '-p, --gateway-path <gatewayPath>',
59+
desc: 'API Path'
60+
},
61+
{
62+
flags: '-i, --gateway-id <gatewayId>',
63+
desc: 'API id'
64+
}
65+
],
66+
desc: '删除云函数网关.',
67+
handler: (envId, options) => __awaiter(void 0, void 0, void 0, function* () {
68+
const { configFile } = options.parent;
69+
const ctx = yield getGatewayContext(envId, configFile);
70+
yield delete_1.deleteGw(ctx, options);
71+
})
72+
},
73+
{
74+
cmd: 'gateway:list [envId]',
75+
options: [
76+
{
77+
flags: '-d, --domain <domain>',
78+
desc: '域名'
79+
},
80+
{
81+
flags: '-p, --gateway-path <gatewayPath>',
82+
desc: 'API Path'
83+
},
84+
{
85+
flags: '-i, --gateway-id <gatewayId>',
86+
desc: 'API id'
87+
}
88+
],
89+
desc: '查询云函数网关.',
90+
handler: (envId, options) => __awaiter(void 0, void 0, void 0, function* () {
91+
const { configFile } = options.parent;
92+
const ctx = yield getGatewayContext(envId, configFile);
93+
yield query_1.queryGw(ctx, options);
94+
})
95+
},
96+
{
97+
cmd: 'gateway:domain:bind <customDomain> [envId]',
98+
options: [],
99+
desc: '绑定自定义网关域名.',
100+
handler: (customDomain, envId, options) => __awaiter(void 0, void 0, void 0, function* () {
101+
const { configFile } = options.parent;
102+
const ctx = yield getGatewayContext(envId, configFile);
103+
yield domain_bind_1.bindGwDomain(ctx, customDomain);
104+
})
105+
},
106+
{
107+
cmd: 'gateway:domain:unbind <customDomain> [envId]',
108+
options: [],
109+
desc: '解绑自定义网关名.',
110+
handler: (customDomain, envId, options) => __awaiter(void 0, void 0, void 0, function* () {
111+
const { configFile } = options.parent;
112+
const ctx = yield getGatewayContext(envId, configFile);
113+
yield domain_unbind_1.unbindGwDomain(ctx, customDomain);
114+
})
115+
},
116+
{
117+
cmd: 'gateway:domain:list [envId]',
118+
options: [
119+
{
120+
flags: '-d, --domain <domain>',
121+
desc: '域名'
122+
}
123+
],
124+
desc: '查询自定义网关域名.',
125+
handler: (envId, options) => __awaiter(void 0, void 0, void 0, function* () {
126+
const { configFile } = options.parent;
127+
const ctx = yield getGatewayContext(envId, configFile);
128+
yield domain_query_1.queryGwDomain(ctx, options);
129+
})
130+
}
131+
];
132+
commands.forEach(item => {
133+
let instance = commander_1.default.command(item.cmd);
134+
item.options.forEach(option => {
135+
instance = instance.option(option.flags, option.desc);
136+
});
137+
instance.description(item.desc);
138+
instance.action(item.handler);
139+
});

lib/commands/gateway/query.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const error_1 = require("../../error");
13+
const gateway_1 = require("../../gateway");
14+
const utils_1 = require("../../utils");
15+
function queryGw(ctx, commandOptions) {
16+
return __awaiter(this, void 0, void 0, function* () {
17+
const { envId } = ctx;
18+
const { domain: domainName, gatewayPath, gatewayId } = commandOptions;
19+
if (!envId && !domainName) {
20+
throw new error_1.CloudBaseError('请指定需要查询的环境ID或网关域名!');
21+
}
22+
const loading = utils_1.loadingFactory();
23+
loading.start(`查询云函数网关中...`);
24+
try {
25+
const res = yield gateway_1.queryGateway({
26+
envId,
27+
domain: domainName,
28+
path: gatewayPath,
29+
gatewayId
30+
});
31+
loading.succeed(`查询云函数网关成功!`);
32+
const head = ['GatewayID', 'Path', 'FunctionName', 'CreateTime'];
33+
const tableData = res.APISet.map(item => [
34+
item.APIId,
35+
item.Path,
36+
item.Name,
37+
item.CreateTime
38+
]);
39+
utils_1.printHorizontalTable(head, tableData);
40+
}
41+
catch (e) {
42+
loading.stop();
43+
throw e;
44+
}
45+
});
46+
}
47+
exports.queryGw = queryGw;

0 commit comments

Comments
 (0)