This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
287 lines (258 loc) · 10.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const fs = require('fs');
const { program } = require('commander');
const packageJson = require('./package.json');
const axios = require("axios");
// Check if config.json exists, and if not, create it with blank values
if (!fs.existsSync('config.json')) {
const defaultConfig = {
apiUrl: '',
apiKey: ''
};
fs.writeFileSync('config.json', JSON.stringify(defaultConfig, null, 2));
}
// Load configuration from config.json
const config = JSON.parse(fs.readFileSync('config.json', 'utf8'));
program
.version(packageJson.version) // Sets the version from package.json
program
.command('config')
.description('Set or update API configuration')
.option('-u, --url <url>', 'API URL')
.option('-k, --key <key>', 'API Key')
.action(async (options) => {
const newConfig = {
apiUrl: options.url || config.apiUrl, // Use current apiUrl if not provided
apiKey: options.key || config.apiKey // Use current apiKey if not provided
};
if (newConfig.apiUrl === config.apiUrl && newConfig.apiKey === config.apiKey) {
console.log('Configuration is already up to date:');
console.log('API URL:', config.apiUrl);
console.log('API Key:', config.apiKey);
return;
}
fs.writeFileSync('config.json', JSON.stringify(newConfig, null, 2));
console.log('Configuration updated successfully.');
});
program
.command('reset')
.description('Clears the CLI configuration')
.action(() => {
const defaultConfig = {
apiUrl: '',
apiKey: ''
};
fs.writeFileSync('config.json', JSON.stringify(defaultConfig, null, 2));
console.log('Configuration reset.');
});
program
.command('about')
.description('About Your Lynx Instance')
.action(async () => {
const headers = {
'Authorization': config.apiKey, // Set the API Key from the configuration file
};
try {
const response = await axios.get(`${config.apiUrl}/api/about`, { headers });
if (response.status === 200) {
const result = response.data.result;
console.log('About Your Lynx Instance:');
console.log(`Domain: ${result.domain}`);
console.log(`Demo: ${result.demo}`);
console.log(`Version: ${result.version}`);
console.log(`Accounts: ${result.accounts}`);
if (result.umami) {
console.log('Umami:');
console.log(` Site: ${result.umami.site}`);
console.log(` URL: ${result.umami.url}`);
}
} else {
console.error('Failed to fetch about information:', response.status);
}
} catch (error) {
console.error('An error occurred:', error.message);
}
});
/*
|----------------------------------------------------------------------------------
| Account Commands
|----------------------------------------------------------------------------------
*/
// Define the "account" command
program
.command('account <action>')
.description('Perform account actions')
.action(async (action) => {
const headers = {
'Authorization': config.apiKey, // Set the API Key from the configuration file
};
switch (action) {
case 'get':
try {
const response = await axios.get(`${config.apiUrl}/api/auth/me`, { headers });
if (response.status === 200) {
const accountInfo = response.data.result;
console.log('Account Information:');
console.log(`ID: ${accountInfo.id}`);
console.log(`Username: ${accountInfo.username}`);
console.log(`Email: ${accountInfo.email}`);
console.log(`Role: ${accountInfo.role}`);
console.log(`2FA Enabled: ${accountInfo.totp}`);
} else {
console.error('Failed to fetch account information:', response.status);
}
} catch (error) {
console.error('An error occurred:', error.message);
}
break;
default:
console.error('Invalid action. Use "get"');
}
});
/*
|----------------------------------------------------------------------------------
| Link Commands
|----------------------------------------------------------------------------------
*/
program
.command('link <action>')
.description('Manage links')
.action((action) => {
if (action === 'create') {
console.log('Creating a new link...');
} else if (action === 'update') {
console.log('Updating a link...');
} else if (action === 'delete') {
console.log('Deleting a link...');
} else {
console.error('Invalid action. Use "create", "update", or "delete".');
}
});
// Create a link
program
.command('create <url>')
.description('Create a new link')
.option('-s, --slug <slug>', 'Custom slug')
.action(async (url, options) => {
if (url) {
const headers = {
'Authorization': config.apiKey, // Set the API Key from the configuration file
};
const data = {
destination: url,
slug: options.slug || ""
};
try {
const response = await axios.post(`${config.apiUrl}/api/link`, data, { headers });
if (response.status === 200) {
console.log('Link Successfully Created!');
console.log(`Destination: ${url}`);
console.log(`URL: ${config.apiUrl}/${response.data.result.slug}`);
} else if (response.status === 401) {
console.error('Unauthorized:', response.status);
} else if (response.status === 409) {
console.error('Conflict: A link with that slug/destination already exists');
} else if (response.status === 422) {
console.error('Unprocessable Entity: Destination doesn\'t match URL regex');
} else {
console.error('Failed to create the link:', response.status);
}
} catch (error) {
console.error('An error occurred:', error.response.data ? error.response.data.message : error.message);
}
} else {
console.error('Invalid arguments. Please provide the slug, destination, and author.');
}
});
// Update a link
program
.command('update <id> <slug> <destination>')
.description('Update an existing link')
.requiredOption('-a, --author <author>', 'Author ID')
.action(async (id, slug, destination, options) => {
if (id && slug && destination) {
const headers = {
'Authorization': config.apiKey, // Set the API Key from the configuration file
};
const data = {
id,
slug,
destination,
author: options.author,
};
try {
const response = await axios.patch(`${config.apiUrl}/api/link`, data, { headers });
if (response.status === 200) {
const linkInfo = response.data.result;
console.log('Link Successfully Updated:');
console.log(`ID: ${linkInfo.id}`);
console.log(`Slug: ${linkInfo.slug}`);
console.log(`Destination: ${linkInfo.destination}`);
console.log(`Author: ${linkInfo.author}`);
console.log(`Creation Date: ${linkInfo.creationDate}`);
console.log(`Modified Date: ${linkInfo.modifiedDate}`);
console.log(`Visits: ${linkInfo.visits}`);
console.log(`Account: ${linkInfo.account}`);
} else if (response.status === 401) {
console.error('Unauthorized:', response.status);
} else if (response.status === 403) {
console.error('Forbidden: You do not have permissions to edit this link');
} else if (response.status === 404) {
console.error('Not Found: A link with that id does not exist');
} else if (response.status === 409) {
console.error('Conflict: A link with that slug/destination already exists');
} else if (response.status === 422) {
console.error('Unprocessable Entity: Invalid slug/destination format');
} else {
console.error('Failed to update the link:', response.status);
}
} catch (error) {
console.error('An error occurred:', error.message);
}
} else {
console.error('Invalid arguments. Please provide the link ID, slug, destination, and author.');
}
});
// Delete a link
program
.command('delete <ids...>')
.description('Delete one or more links')
.requiredOption('-a, --author <author>', 'Author ID')
.action(async (ids, options) => {
if (ids.length > 0) {
const headers = {
'Authorization': config.apiKey, // Set the API Key from the configuration file
};
const data = {
ids,
author: options.author,
};
try {
const response = await axios.delete(`${config.apiUrl}/api/link`, { data, headers });
if (response.status === 200) {
console.log('Link(s) Successfully Deleted:');
console.log(response.data.result);
} else if (response.status === 401) {
console.error('Unauthorized:', response.status);
} else if (response.status === 403) {
console.error('Forbidden: You do not have the permissions to delete some or all of the selected links. No links were deleted.');
} else {
console.error('Failed to delete the link(s):', response.status);
}
} catch (error) {
console.error('An error occurred:', error.message);
}
} else {
console.error('Invalid arguments. Please provide at least one link ID to delete.');
}
});
/*
|----------------------------------------------------------------------------------
| a
|----------------------------------------------------------------------------------
*/
program
.configureOutput({
// Custom text for listing commands
getCommands: (commands) => `Available commands: ${commands.map(cmd => cmd.name()).join(', ')}`,
});
program.parse(process.argv);