-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
247 lines (228 loc) · 6.84 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
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import inquirer from 'inquirer';
import { spawn } from 'child_process';
import autocompletePrompt from 'inquirer-autocomplete-prompt';
import { formatChoiceOptions, generateTable } from './lib/utils.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
inquirer.registerPrompt('autocomplete', autocompletePrompt);
const filePath = path.join(__dirname, 'data', 'ssh.json');
class SSHManager {
constructor() {
this.commands = {
add: {
func: () => this.addEntry(),
help: 'Adds a new SSH connection entry.',
},
remove: {
func: () => this.removeEntry(),
help: 'Removes an existing SSH connection entry.',
},
modify: {
func: () => this.modifyEntry(),
help: 'Modifies an existing SSH connection entry.',
},
list: {
func: () => this.listConnections(),
help: 'Lists all SSH connections.',
},
'config-location': {
func: () => this.showConfigLocation(),
help: 'Displays the location of the config file.',
},
version: {
func: () => this.showVersion(),
help: 'Displays the version of the application.',
aliases: ['-v'],
},
};
// Bind all methods to this instance
Object.getOwnPropertyNames(SSHManager.prototype).forEach((key) => {
if (typeof this[key] === 'function' && key !== 'constructor') {
this[key] = this[key].bind(this);
}
});
}
ensureDirectoryExists() {
const directory = path.dirname(filePath);
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory, { recursive: true });
}
}
readData(callback) {
this.ensureDirectoryExists();
fs.access(filePath, fs.constants.F_OK, (err) => {
if (err) {
const defaultContent = JSON.stringify({});
fs.writeFile(filePath, defaultContent, 'utf8', (writeErr) => {
if (writeErr) {
console.error(`Error creating file at [${filePath}]: ${writeErr}`);
return;
}
this.readFileContent(callback);
});
} else {
this.readFileContent(callback);
}
});
}
readFileContent(callback) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error(
`An error was encountered while reading the file at [${filePath}]: ${err}`,
);
return;
}
callback(JSON.parse(data));
});
}
writeData(data) {
this.ensureDirectoryExists();
fs.writeFile(filePath, JSON.stringify(data, null, 2), (err) => {
if (err) {
console.error('An error occurred during file writing:', err);
}
});
}
addEntry() {
inquirer
.prompt([
{ type: 'input', name: 'key', message: 'Enter the label:' },
{ type: 'input', name: 'username', message: 'Username:' },
{ type: 'input', name: 'hostname', message: 'Hostname (IP address):' },
])
.then((answers) => {
this.readData((data) => {
data[answers.key] = {
username: answers.username,
hostname: answers.hostname,
};
this.writeData(data);
console.log('The connection was added successfully.');
this.listConnections();
});
});
}
removeEntry() {
this.readData((data) => {
inquirer
.prompt([
{
type: 'autocomplete',
name: 'key',
message: 'Select the connection you wish to remove:',
source: (_answers, input) =>
Promise.resolve(formatChoiceOptions(data, input)),
},
{
type: 'confirm',
name: 'confirm',
message: 'Are you sure you want to remove this connection?',
},
])
.then((answer) => {
if (!answer.confirm) return;
delete data[answer.key];
this.writeData(data);
console.log('The connection was removed successfully.');
this.listConnections();
});
});
}
modifyEntry() {
this.readData((data) => {
inquirer
.prompt([
{
type: 'autocomplete',
name: 'key',
message: 'Select the connection to modify:',
source: (answers, input) =>
Promise.resolve(formatChoiceOptions(data, input)),
},
{
type: 'input',
name: 'newUsername',
message: 'New username:',
default: (answers) => data[answers.key].username,
},
{
type: 'input',
name: 'newHostname',
message: 'New hostname (IP address):',
default: (answers) => data[answers.key].hostname,
},
])
.then((answers) => {
data[answers.key] = {
username: answers.newUsername,
hostname: answers.newHostname,
};
this.writeData(data);
console.log('The connection was modified successfully.');
});
});
}
listConnections() {
this.readData((data) => {
console.log(generateTable(data));
});
}
showConfigLocation() {
console.log(`Config file location: ${filePath}`);
}
showVersion() {
const packageJson = JSON.parse(
fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'),
);
console.log(`sshc version ${packageJson.version}`);
}
sshIntoHost() {
this.readData((data) => {
inquirer
.prompt([
{
type: 'autocomplete',
name: 'key',
message: 'Choose the host connection you wish to SSH into:',
source: (answers, input) =>
Promise.resolve(formatChoiceOptions(data, input)),
},
])
.then((answers) => {
const entry = data[answers.key];
console.log(
`Attempting to connect: ssh ${entry.username}@${entry.hostname}`,
);
spawn('ssh', [`${entry.username}@${entry.hostname}`], {
stdio: 'inherit',
});
});
});
}
mainMenu() {
const args = process.argv.slice(2);
const commandName = args[0]?.replace(/^-+/, '');
if (commandName === 'help' || args.includes('-h')) {
if (args[1] && this.commands[args[1]]) {
console.log(this.commands[args[1]].help);
} else {
console.log('Available commands:');
Object.entries(this.commands).forEach(([name, { help, aliases }]) => {
console.log(
` ${name}${aliases ? ` (${aliases.join(', ')})` : ''}: ${help}`,
);
});
}
} else if (this.commands[commandName]) {
this.commands[commandName].func();
} else {
this.sshIntoHost();
}
}
}
const sshManager = new SSHManager();
export const mainMenu = sshManager.mainMenu.bind(sshManager);