@tolinax/ayoune-cli 2024.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/data/defaultActions.js +9 -0
- package/data/operations.js +5 -0
- package/index.js +34 -0
- package/lib/api/apiCallHandler.js +42 -0
- package/lib/api/apiClient.js +16 -0
- package/lib/api/auditCallHandler.js +42 -0
- package/lib/api/handleAPIError.js +0 -0
- package/lib/api/login.js +33 -0
- package/lib/commands/createAuditCommand.js +28 -0
- package/lib/commands/createCopyCommand.js +24 -0
- package/lib/commands/createCreateCommand.js +29 -0
- package/lib/commands/createDescribeCommand.js +24 -0
- package/lib/commands/createEditCommand.js +25 -0
- package/lib/commands/createEventsCommand.js +54 -0
- package/lib/commands/createGetCommand.js +36 -0
- package/lib/commands/createListCommand.js +35 -0
- package/lib/commands/createLoginCommand.js +17 -0
- package/lib/commands/createModulesCommand.js +33 -0
- package/lib/commands/createProgram.js +63 -0
- package/lib/commands/createStorageCommand.js +16 -0
- package/lib/commands/createStreamCommand.js +45 -0
- package/lib/helpers/addSpacesToCamelCase.js +5 -0
- package/lib/helpers/handleResponseFormatOptions.js +43 -0
- package/lib/helpers/initializeSettings.js +14 -0
- package/lib/helpers/localStorage.js +4 -0
- package/lib/helpers/parseInt.js +9 -0
- package/lib/helpers/saveFile.js +39 -0
- package/lib/models/getCollections.js +15 -0
- package/lib/models/getModelsInModules.js +13 -0
- package/lib/models/getModuleFromCollection.js +7 -0
- package/lib/operations/handleAuditOperation.js +22 -0
- package/lib/operations/handleCollectionOperation.js +27 -0
- package/lib/operations/handleCopySingleOperation.js +22 -0
- package/lib/operations/handleCreateSingleOperation.js +27 -0
- package/lib/operations/handleDescribeSingleOperation.js +20 -0
- package/lib/operations/handleEditOperation.js +51 -0
- package/lib/operations/handleEditRawOperation.js +35 -0
- package/lib/operations/handleGetOperation.js +25 -0
- package/lib/operations/handleGetSingleOperation.js +20 -0
- package/lib/operations/handleListOperation.js +24 -0
- package/lib/operations/handleSingleAuditOperation.js +27 -0
- package/lib/prompts/promptAudits.js +15 -0
- package/lib/prompts/promptCollection.js +13 -0
- package/lib/prompts/promptCollectionInModule.js +13 -0
- package/lib/prompts/promptCollectionWithModule.js +15 -0
- package/lib/prompts/promptDefaultAction.js +13 -0
- package/lib/prompts/promptEntry.js +12 -0
- package/lib/prompts/promptModule.js +19 -0
- package/lib/prompts/promptName.js +11 -0
- package/lib/prompts/promptOperation.js +13 -0
- package/lib/socket/customerSocketClient.js +12 -0
- package/lib/socket/socketClient.js +11 -0
- package/package.json +147 -0
package/index.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
import { Command } from "commander";
|
|
3
|
+
import { createSpinner } from "nanospinner";
|
|
4
|
+
import { initializeSettings } from "./lib/helpers/initializeSettings.js";
|
|
5
|
+
import { createProgram } from "./lib/commands/createProgram.js";
|
|
6
|
+
import autoupdater from "cli-autoupdater";
|
|
7
|
+
import path from "path";
|
|
8
|
+
import { readFile } from "fs/promises";
|
|
9
|
+
import * as process from "process";
|
|
10
|
+
//Create new command instance
|
|
11
|
+
//@ts-ignore
|
|
12
|
+
const pkg = JSON.parse(await readFile(path.join(process.cwd(), "package.json"), "utf8"));
|
|
13
|
+
const program = new Command();
|
|
14
|
+
initializeSettings();
|
|
15
|
+
//Setup spinner
|
|
16
|
+
export const spinner = createSpinner("Getting data...");
|
|
17
|
+
console.log(pkg);
|
|
18
|
+
autoupdater(pkg)
|
|
19
|
+
.then((updated) => {
|
|
20
|
+
if (updated) {
|
|
21
|
+
// The application was updated, so exit
|
|
22
|
+
console.log("You may rerun the last command");
|
|
23
|
+
process.exit();
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
// No update occurred, so just run the application
|
|
27
|
+
createProgram(program);
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
.catch((e) => {
|
|
31
|
+
// An error has occurred so
|
|
32
|
+
console.log(e);
|
|
33
|
+
process.exit();
|
|
34
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { api } from "./apiClient.js";
|
|
2
|
+
import { spinner } from "../../index.js";
|
|
3
|
+
import * as process from "process";
|
|
4
|
+
import { login } from "./login.js";
|
|
5
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
6
|
+
export const apiCallHandler = async (url, method = "get", data = null, params = {}) => {
|
|
7
|
+
var _a, _b, _c;
|
|
8
|
+
try {
|
|
9
|
+
const response = await api({
|
|
10
|
+
method,
|
|
11
|
+
url,
|
|
12
|
+
data,
|
|
13
|
+
params: { ...params, ref: `ayoune-cli@${process.env.AYOUNE_VERSION}` },
|
|
14
|
+
headers: {
|
|
15
|
+
Authorization: `Bearer ${localStorage.getItem("token")}`,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
const apiError = error.response;
|
|
22
|
+
if (apiError.status === 400) {
|
|
23
|
+
spinner.error({
|
|
24
|
+
text: `Bad Request: ${apiError.data.meta.message}`,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (apiError.status === 401) {
|
|
28
|
+
await login();
|
|
29
|
+
}
|
|
30
|
+
if (apiError.status === 403) {
|
|
31
|
+
spinner.error({
|
|
32
|
+
text: `You are not allowed to access this ressource... Required rights are [${(_c = (_b = (_a = apiError.data) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b.requiredRights) === null || _c === void 0 ? void 0 : _c.join(",")}]`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
spinner.error({
|
|
37
|
+
text: `Status ${apiError.status}: ${apiError.statusText} | ${apiError.data.meta.message}`,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return apiError.data;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import axiosRetry from "axios-retry";
|
|
3
|
+
const hosts = {
|
|
4
|
+
api: "https://api.ayoune.app",
|
|
5
|
+
api_dev: "http://localhost:3000",
|
|
6
|
+
audit: "https://audit.ayoune.app",
|
|
7
|
+
};
|
|
8
|
+
const api = axios.create({
|
|
9
|
+
baseURL: hosts.api_dev,
|
|
10
|
+
});
|
|
11
|
+
const audit = axios.create({
|
|
12
|
+
baseURL: hosts.audit,
|
|
13
|
+
});
|
|
14
|
+
axiosRetry(api, { retries: 3 });
|
|
15
|
+
axiosRetry(audit, { retries: 3 });
|
|
16
|
+
export { api, audit };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { audit } from "./apiClient.js";
|
|
2
|
+
import { spinner } from "../../index.js";
|
|
3
|
+
import * as process from "process";
|
|
4
|
+
import { login } from "./login.js";
|
|
5
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
6
|
+
export const auditCallHandler = async (url, method = "get", data = null, params = {}) => {
|
|
7
|
+
var _a, _b, _c;
|
|
8
|
+
try {
|
|
9
|
+
const response = await audit({
|
|
10
|
+
method,
|
|
11
|
+
url,
|
|
12
|
+
data,
|
|
13
|
+
params: { ...params, ref: `ayoune-cli@${process.env.AYOUNE_VERSION}` },
|
|
14
|
+
headers: {
|
|
15
|
+
Authorization: `Bearer ${localStorage.getItem("token")}`,
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
const apiError = error.response;
|
|
22
|
+
if (apiError.status === 400) {
|
|
23
|
+
spinner.error({
|
|
24
|
+
text: `Bad Request: ${apiError.data.meta.message}`,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (apiError.status === 401) {
|
|
28
|
+
await login();
|
|
29
|
+
}
|
|
30
|
+
if (apiError.status === 403) {
|
|
31
|
+
spinner.error({
|
|
32
|
+
text: `You are not allowed to access this ressource... Required rights are [${(_c = (_b = (_a = apiError.data) === null || _a === void 0 ? void 0 : _a.meta) === null || _b === void 0 ? void 0 : _b.requiredRights) === null || _c === void 0 ? void 0 : _c.join(",")}]`,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
spinner.error({
|
|
37
|
+
text: `Status ${apiError.status}: ${apiError.statusText} | ${apiError.data.meta.message}`,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return apiError.data;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
File without changes
|
package/lib/api/login.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { socket } from "../socket/socketClient.js";
|
|
2
|
+
import { randomAsciiString } from "@tolinax/ayoune-models/utils/makeRandomToken.js";
|
|
3
|
+
import { spinner } from "../../index.js";
|
|
4
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
5
|
+
export const login = async () => {
|
|
6
|
+
spinner.start({ text: "Waiting for login\n" });
|
|
7
|
+
const token = randomAsciiString();
|
|
8
|
+
console.log(`___________________________________________________`);
|
|
9
|
+
console.log(`Visit: https://login.ayoune.app/ayoune?cli=${token}`);
|
|
10
|
+
console.log(`___________________________________________________`);
|
|
11
|
+
const credentials = await waitForCredentials(token);
|
|
12
|
+
return credentials;
|
|
13
|
+
};
|
|
14
|
+
const waitForCredentials = async (token) => {
|
|
15
|
+
return new Promise(function (resolve, reject) {
|
|
16
|
+
const _socket = socket(token);
|
|
17
|
+
_socket.on("join:cli:confirmed", async (data) => {
|
|
18
|
+
spinner.update({ text: `Session active` });
|
|
19
|
+
});
|
|
20
|
+
_socket.on("cli-token", async (data) => {
|
|
21
|
+
spinner.success({ text: `Received auth credentials` });
|
|
22
|
+
localStorage.setItem("token", data.token);
|
|
23
|
+
localStorage.setItem("refreshToken", data.refreshToken);
|
|
24
|
+
resolve(data);
|
|
25
|
+
});
|
|
26
|
+
_socket.on("connect_error", async (err) => {
|
|
27
|
+
spinner.error({ text: err.message });
|
|
28
|
+
});
|
|
29
|
+
_socket.on("connect", async () => {
|
|
30
|
+
spinner.update({ text: "Tunnel established. Waiting for credentials" });
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Argument } from "commander";
|
|
2
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
3
|
+
import { handleAuditOperation } from "../operations/handleAuditOperation.js";
|
|
4
|
+
import { promptAudits } from "../prompts/promptAudits.js";
|
|
5
|
+
import { handleSingleAuditOperation } from "../operations/handleSingleAuditOperation.js";
|
|
6
|
+
export function createAuditCommand(program) {
|
|
7
|
+
program
|
|
8
|
+
.command("audit")
|
|
9
|
+
.addArgument(new Argument("[collection]", "The collection to use").default(localStorage.getItem("lastCollection"), "The last used collection"))
|
|
10
|
+
.addArgument(new Argument("[id]", "The id to get the audit from").default(localStorage.getItem("lastId"), "The last used id"))
|
|
11
|
+
.alias("history")
|
|
12
|
+
.description("Get the history of <id> in <collection>")
|
|
13
|
+
.action(async (collection, id, options) => {
|
|
14
|
+
try {
|
|
15
|
+
const opts = { ...program.opts(), ...options };
|
|
16
|
+
const { data, content, result, meta } = await handleAuditOperation(collection, id, {
|
|
17
|
+
...opts,
|
|
18
|
+
});
|
|
19
|
+
const selectedAudit = await promptAudits(result);
|
|
20
|
+
const { singleData, singleContent, singleResult, singleMeta } = await handleSingleAuditOperation(collection, id, selectedAudit, {
|
|
21
|
+
...opts,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
console.error(e);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Argument } from "commander";
|
|
2
|
+
import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
|
|
3
|
+
import { handleCopySingleOperation } from "../operations/handleCopySingleOperation.js";
|
|
4
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
5
|
+
export function createCopyCommand(program) {
|
|
6
|
+
program
|
|
7
|
+
.command("copy")
|
|
8
|
+
.addArgument(new Argument("[collection]", "The collection to use").default(localStorage.getItem("lastCollection"), "The last used collection"))
|
|
9
|
+
.addArgument(new Argument("[id]", "The id to copy").default(localStorage.getItem("lastId"), "The last used id"))
|
|
10
|
+
.alias("cp")
|
|
11
|
+
.description("Copy entry with <id> in <collection>")
|
|
12
|
+
.action(async (collection, id, options) => {
|
|
13
|
+
try {
|
|
14
|
+
const opts = { ...program.opts(), ...options };
|
|
15
|
+
const module = getModuleFromCollection(collection);
|
|
16
|
+
await handleCopySingleOperation(module.module, collection, id, {
|
|
17
|
+
...opts,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
console.error(e);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
|
|
2
|
+
import { promptCollectionWithModule } from "../prompts/promptCollectionWithModule.js";
|
|
3
|
+
import { handleCreateSingleOperation } from "../operations/handleCreateSingleOperation.js";
|
|
4
|
+
import { promptName } from "../prompts/promptName.js";
|
|
5
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
6
|
+
export function createCreateCommand(program) {
|
|
7
|
+
program
|
|
8
|
+
.command("create [collection]")
|
|
9
|
+
.alias("c")
|
|
10
|
+
.description("Create entry in <collection>")
|
|
11
|
+
.action(async (collection, options) => {
|
|
12
|
+
try {
|
|
13
|
+
const opts = { ...program.opts(), ...options };
|
|
14
|
+
if (!collection) {
|
|
15
|
+
collection = await promptCollectionWithModule();
|
|
16
|
+
}
|
|
17
|
+
const module = getModuleFromCollection(collection);
|
|
18
|
+
const name = await promptName();
|
|
19
|
+
await handleCreateSingleOperation(module.module, collection, name, {
|
|
20
|
+
...opts,
|
|
21
|
+
});
|
|
22
|
+
localStorage.setItem("lastModule", module.module);
|
|
23
|
+
localStorage.setItem("lastCollection", collection);
|
|
24
|
+
}
|
|
25
|
+
catch (e) {
|
|
26
|
+
console.error(e);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
|
|
2
|
+
import { handleDescribeSingleOperation } from "../operations/handleDescribeSingleOperation.js";
|
|
3
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
4
|
+
export function createDescribeCommand(program) {
|
|
5
|
+
program
|
|
6
|
+
.command("describe <collection> <id>")
|
|
7
|
+
.alias("d")
|
|
8
|
+
.description("Describe entry with <id> in <collection>")
|
|
9
|
+
.action(async (collection, id, options) => {
|
|
10
|
+
try {
|
|
11
|
+
const opts = { ...program.opts(), ...options };
|
|
12
|
+
const module = getModuleFromCollection(collection);
|
|
13
|
+
localStorage.setItem("lastModule", module.module);
|
|
14
|
+
localStorage.setItem("lastCollection", collection);
|
|
15
|
+
localStorage.setItem("lastId", id);
|
|
16
|
+
await handleDescribeSingleOperation(module.module, collection, id, {
|
|
17
|
+
...opts,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
console.error(e);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
|
|
2
|
+
import { handleGetSingleOperation } from "../operations/handleGetSingleOperation.js";
|
|
3
|
+
import { handleEditOperation } from "../operations/handleEditOperation.js";
|
|
4
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
5
|
+
export function createEditCommand(program) {
|
|
6
|
+
program
|
|
7
|
+
.command("edit <collection> <id>")
|
|
8
|
+
.alias("e")
|
|
9
|
+
.description("Edit entry with <id> in <collection>")
|
|
10
|
+
.action(async (collection, id, options) => {
|
|
11
|
+
try {
|
|
12
|
+
const opts = { ...program.opts(), ...options };
|
|
13
|
+
const module = getModuleFromCollection(collection);
|
|
14
|
+
localStorage.setItem("lastModule", module.module);
|
|
15
|
+
localStorage.setItem("lastCollection", collection);
|
|
16
|
+
localStorage.setItem("lastId", id);
|
|
17
|
+
let result = {};
|
|
18
|
+
result = await handleGetSingleOperation(module.module, collection, id, opts);
|
|
19
|
+
await handleEditOperation(module.module, collection, result.content);
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
console.error(e);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Option } from "commander";
|
|
2
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
3
|
+
import { decodeToken } from "@tolinax/ayoune-core/lib/auth/decodeToken.js";
|
|
4
|
+
import { customerSocket } from "../socket/customerSocketClient.js";
|
|
5
|
+
import { spinner } from "../../index.js";
|
|
6
|
+
import yaml from "js-yaml";
|
|
7
|
+
export function createEventsCommand(program) {
|
|
8
|
+
program
|
|
9
|
+
.command("events")
|
|
10
|
+
.alias("sub")
|
|
11
|
+
.description("Attach to the live event bus")
|
|
12
|
+
.addOption(new Option("-f, --format <format>", "Set the output format")
|
|
13
|
+
.choices(["json", "yaml", "table"])
|
|
14
|
+
.default("json"))
|
|
15
|
+
.addOption(new Option("-c, --category <category>", "Set the category to listen").default("*"))
|
|
16
|
+
.addOption(new Option("-a, --action <action>", "Set the action to listen").default("*"))
|
|
17
|
+
.addOption(new Option("-l, --label <label>", "Set the label to listen").default("*"))
|
|
18
|
+
.addOption(new Option("-v, --value <value>", "Set the value to listen").default("*"))
|
|
19
|
+
.action(async (options) => {
|
|
20
|
+
try {
|
|
21
|
+
const tokenPayload = decodeToken(localStorage.getItem("token"));
|
|
22
|
+
const user = tokenPayload.payload;
|
|
23
|
+
spinner.start({ text: `Starting stream with [${user._customerID}]` });
|
|
24
|
+
const socket = customerSocket(user);
|
|
25
|
+
spinner.update({ text: "Stream active" });
|
|
26
|
+
socket.on("event", (data) => {
|
|
27
|
+
if (options.category !== "*" && options.category !== data.category)
|
|
28
|
+
return;
|
|
29
|
+
if (options.action !== "*" && options.action !== data.action)
|
|
30
|
+
return;
|
|
31
|
+
if (options.label !== "*" && options.label !== data.label)
|
|
32
|
+
return;
|
|
33
|
+
if (options.value !== "*" && options.value !== data.value)
|
|
34
|
+
return;
|
|
35
|
+
spinner.update({
|
|
36
|
+
text: `Received [${data.category}.${data.action}.${data.label}.${data.value}]`,
|
|
37
|
+
});
|
|
38
|
+
if (options.format === "table") {
|
|
39
|
+
console.table(data.evt_data);
|
|
40
|
+
}
|
|
41
|
+
if (options.format === "yaml") {
|
|
42
|
+
console.log(yaml.dump(data.evt_data));
|
|
43
|
+
}
|
|
44
|
+
if (options.format === "json") {
|
|
45
|
+
console.log(JSON.stringify(data.evt_data, null, 2));
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
spinner.error({ text: e.message });
|
|
51
|
+
console.error(e);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { parseInteger } from "../helpers/parseInt.js";
|
|
2
|
+
import { promptCollectionWithModule } from "../prompts/promptCollectionWithModule.js";
|
|
3
|
+
import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
|
|
4
|
+
import { saveFile } from "../helpers/saveFile.js";
|
|
5
|
+
import { handleGetOperation } from "../operations/handleGetOperation.js";
|
|
6
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
7
|
+
export function createGetCommand(program) {
|
|
8
|
+
program
|
|
9
|
+
.command("get [collection]")
|
|
10
|
+
.alias("g")
|
|
11
|
+
.description("Get Data in collection")
|
|
12
|
+
.option("-p, --page <number>", "Page", parseInteger, 1)
|
|
13
|
+
.option("-l, --limit <number>", "Limit", parseInteger, 20)
|
|
14
|
+
.option("-f, --from <date>", "From date")
|
|
15
|
+
.option("-i, --fields <fields...>", "Fields to get")
|
|
16
|
+
.action(async (collection, options) => {
|
|
17
|
+
try {
|
|
18
|
+
const opts = { ...program.opts(), ...options };
|
|
19
|
+
if (!collection) {
|
|
20
|
+
collection = await promptCollectionWithModule();
|
|
21
|
+
}
|
|
22
|
+
const module = getModuleFromCollection(collection);
|
|
23
|
+
localStorage.setItem("lastModule", module.module);
|
|
24
|
+
localStorage.setItem("lastCollection", collection);
|
|
25
|
+
let result = {};
|
|
26
|
+
let entry = "";
|
|
27
|
+
result = await handleGetOperation(module.module, collection, opts);
|
|
28
|
+
if (opts.save) {
|
|
29
|
+
await saveFile("get", opts, result);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
console.error(e);
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { parseInteger } from "../helpers/parseInt.js";
|
|
2
|
+
import { promptCollectionWithModule } from "../prompts/promptCollectionWithModule.js";
|
|
3
|
+
import { getModuleFromCollection } from "../models/getModuleFromCollection.js";
|
|
4
|
+
import { handleListOperation } from "../operations/handleListOperation.js";
|
|
5
|
+
import { saveFile } from "../helpers/saveFile.js";
|
|
6
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
7
|
+
export function createListCommand(program) {
|
|
8
|
+
program
|
|
9
|
+
.command("list [collection]")
|
|
10
|
+
.alias("l")
|
|
11
|
+
.description("List entries in collection")
|
|
12
|
+
.option("-p, --page <number>", "Page", parseInteger, 1)
|
|
13
|
+
.option("-l, --limit <number>", "Limit", parseInteger, 20)
|
|
14
|
+
.option("-f, --from <date>", "From date")
|
|
15
|
+
.action(async (collection, options) => {
|
|
16
|
+
try {
|
|
17
|
+
const opts = { ...program.opts(), ...options };
|
|
18
|
+
if (!collection) {
|
|
19
|
+
collection = await promptCollectionWithModule();
|
|
20
|
+
}
|
|
21
|
+
const module = getModuleFromCollection(collection);
|
|
22
|
+
localStorage.setItem("lastModule", module.module);
|
|
23
|
+
localStorage.setItem("lastCollection", collection);
|
|
24
|
+
let result = {};
|
|
25
|
+
let entry = "";
|
|
26
|
+
result = await handleListOperation(module.module, collection, opts);
|
|
27
|
+
if (opts.save) {
|
|
28
|
+
await saveFile("list", opts, result);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (e) {
|
|
32
|
+
console.error(e);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { login } from "../api/login.js";
|
|
2
|
+
import * as process from "process";
|
|
3
|
+
export function createLoginCommand(program) {
|
|
4
|
+
program
|
|
5
|
+
.command("login")
|
|
6
|
+
.alias("auth")
|
|
7
|
+
.description("Login to your aYOUne account")
|
|
8
|
+
.action(async () => {
|
|
9
|
+
try {
|
|
10
|
+
await login();
|
|
11
|
+
process.exit();
|
|
12
|
+
}
|
|
13
|
+
catch (e) {
|
|
14
|
+
console.error(e);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { promptModule } from "../prompts/promptModule.js";
|
|
2
|
+
import { promptCollectionInModule } from "../prompts/promptCollectionInModule.js";
|
|
3
|
+
import { promptOperation } from "../prompts/promptOperation.js";
|
|
4
|
+
import { handleListOperation } from "../operations/handleListOperation.js";
|
|
5
|
+
import { promptEntry } from "../prompts/promptEntry.js";
|
|
6
|
+
import { handleCollectionOperation } from "../operations/handleCollectionOperation.js";
|
|
7
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
8
|
+
export function createModulesCommand(program) {
|
|
9
|
+
program
|
|
10
|
+
.command("modules")
|
|
11
|
+
.alias("m")
|
|
12
|
+
.description("List modules")
|
|
13
|
+
.action(async (message) => {
|
|
14
|
+
try {
|
|
15
|
+
console.log(message);
|
|
16
|
+
const module = await promptModule();
|
|
17
|
+
const collection = await promptCollectionInModule(module);
|
|
18
|
+
const operation = await promptOperation();
|
|
19
|
+
localStorage.setItem("lastModule", module);
|
|
20
|
+
localStorage.setItem("lastCollection", collection);
|
|
21
|
+
let result = {};
|
|
22
|
+
let entry = "";
|
|
23
|
+
if (operation === "list") {
|
|
24
|
+
result = await handleListOperation(module, collection);
|
|
25
|
+
entry = await promptEntry(result);
|
|
26
|
+
}
|
|
27
|
+
await handleCollectionOperation(module, collection, entry, operation);
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
console.error(e);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
//Defines the main program structure for command line interface
|
|
2
|
+
import { Option } from "commander";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import os from "os";
|
|
5
|
+
import { createModulesCommand } from "./createModulesCommand.js";
|
|
6
|
+
import { createListCommand } from "./createListCommand.js";
|
|
7
|
+
import { createGetCommand } from "./createGetCommand.js";
|
|
8
|
+
import { createEditCommand } from "./createEditCommand.js";
|
|
9
|
+
import { createLoginCommand } from "./createLoginCommand.js";
|
|
10
|
+
import { createDescribeCommand } from "./createDescribeCommand.js";
|
|
11
|
+
import { createCopyCommand } from "./createCopyCommand.js";
|
|
12
|
+
import { createCreateCommand } from "./createCreateCommand.js";
|
|
13
|
+
import { createStorageCommand } from "./createStorageCommand.js";
|
|
14
|
+
import { createAuditCommand } from "./createAuditCommand.js";
|
|
15
|
+
import { createStreamCommand } from "./createStreamCommand.js";
|
|
16
|
+
import { createEventsCommand } from "./createEventsCommand.js";
|
|
17
|
+
export function createProgram(program) {
|
|
18
|
+
program
|
|
19
|
+
.version("2024.2.0")
|
|
20
|
+
.addOption(new Option("-r, --responseFormat <format>", "Set the output format")
|
|
21
|
+
.choices(["json", "csv", "yaml", "table"])
|
|
22
|
+
.default("json"))
|
|
23
|
+
.addOption(new Option("-v, --verbosity <level>", "Set the verbosity level of the returned meta information")
|
|
24
|
+
.choices(["default", "extended", "minimal"])
|
|
25
|
+
.default("default")
|
|
26
|
+
.conflicts("hideMeta"))
|
|
27
|
+
.addOption(new Option("-m, --hideMeta", "Returns only the payload without meta information. ")
|
|
28
|
+
.default(false)
|
|
29
|
+
.conflicts("verbosity"))
|
|
30
|
+
.addOption(new Option("-s, --save", "Saves the response as file"))
|
|
31
|
+
.addOption(new Option("-d, --debug", "Saves the meta information as file"))
|
|
32
|
+
.addOption(new Option("-o, --outPath [filePath]", "Lets you set a path").default(path.join(os.homedir(), "aYOUne")))
|
|
33
|
+
.addOption(new Option("-n, --name [fileName]", "Lets you set a filename"))
|
|
34
|
+
.description("aYOUne - Business as a Service Command Line Interface");
|
|
35
|
+
program.showHelpAfterError();
|
|
36
|
+
createModulesCommand(program);
|
|
37
|
+
createListCommand(program);
|
|
38
|
+
createGetCommand(program);
|
|
39
|
+
createEditCommand(program);
|
|
40
|
+
createCopyCommand(program);
|
|
41
|
+
createDescribeCommand(program);
|
|
42
|
+
createCreateCommand(program);
|
|
43
|
+
createStorageCommand(program);
|
|
44
|
+
createAuditCommand(program);
|
|
45
|
+
createStreamCommand(program);
|
|
46
|
+
createEventsCommand(program);
|
|
47
|
+
createLoginCommand(program);
|
|
48
|
+
program.addHelpText("beforeAll", `
|
|
49
|
+
__ ______ _ _
|
|
50
|
+
\\ \\ / / __ \\| | | |
|
|
51
|
+
__ \\ \\_/ / | | | | | |_ __ ___
|
|
52
|
+
/ _\` \\ /| | | | | | | '_ \\ / _ \\
|
|
53
|
+
| (_| || | | |__| | |__| | | | | __/
|
|
54
|
+
\\__,_||_| \\____/ \\____/|_| |_|\\___|
|
|
55
|
+
`);
|
|
56
|
+
program.configureHelp({
|
|
57
|
+
sortOptions: true,
|
|
58
|
+
sortSubcommands: true,
|
|
59
|
+
showGlobalOptions: true,
|
|
60
|
+
});
|
|
61
|
+
//Parse command line arguments
|
|
62
|
+
program.parse(process.argv);
|
|
63
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
2
|
+
export function createStorageCommand(program) {
|
|
3
|
+
program
|
|
4
|
+
.command("storage")
|
|
5
|
+
.alias("s")
|
|
6
|
+
.description("Show current local storage")
|
|
7
|
+
.action(async () => {
|
|
8
|
+
try {
|
|
9
|
+
const storage = { ...localStorage };
|
|
10
|
+
console.table(storage);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
console.error(e);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { Option } from "commander";
|
|
2
|
+
import { localStorage } from "../helpers/localStorage.js";
|
|
3
|
+
import { decodeToken } from "@tolinax/ayoune-core/lib/auth/decodeToken.js";
|
|
4
|
+
import { customerSocket } from "../socket/customerSocketClient.js";
|
|
5
|
+
import { spinner } from "../../index.js";
|
|
6
|
+
import yaml from "js-yaml";
|
|
7
|
+
export function createStreamCommand(program) {
|
|
8
|
+
program
|
|
9
|
+
.command("stream")
|
|
10
|
+
.alias("listen")
|
|
11
|
+
.description("Attach to the live data stream")
|
|
12
|
+
.addOption(new Option("-f, --format <format>", "Set the output format")
|
|
13
|
+
.choices(["json", "yaml", "table"])
|
|
14
|
+
.default("json"))
|
|
15
|
+
.addOption(new Option("-l, --level <level>", "Set the detail level")
|
|
16
|
+
.choices(["default", "minimal", "extended"])
|
|
17
|
+
.default("default"))
|
|
18
|
+
.action(async (options) => {
|
|
19
|
+
try {
|
|
20
|
+
const tokenPayload = decodeToken(localStorage.getItem("token"));
|
|
21
|
+
const user = tokenPayload.payload;
|
|
22
|
+
console.log(user);
|
|
23
|
+
console.log(`Starting stream with [${user._customerID}]`);
|
|
24
|
+
spinner.start({ text: `Starting stream with [${user._customerID}]` });
|
|
25
|
+
const socket = customerSocket(user);
|
|
26
|
+
spinner.update({ text: "Stream active" });
|
|
27
|
+
socket.onAny((channel, data) => {
|
|
28
|
+
spinner.update({ text: `Received [${channel}]` });
|
|
29
|
+
if (options.format === "table") {
|
|
30
|
+
console.table(data);
|
|
31
|
+
}
|
|
32
|
+
if (options.format === "yaml") {
|
|
33
|
+
console.log(yaml.dump(data));
|
|
34
|
+
}
|
|
35
|
+
if (options.format === "json") {
|
|
36
|
+
console.log(JSON.stringify(data, null, 2));
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
spinner.error({ text: e.message });
|
|
42
|
+
console.error(e);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|