@eightshone/sshman
Version:
A simple cli ssh manager
127 lines • 5.93 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import input from "@inquirer/input";
import select from "@inquirer/select";
import password from "@inquirer/password";
import number from "@inquirer/number";
import { homedir } from "os";
import saveFile from "../../../utils/saveFile.js";
import { CONFIG_DIR } from "../../../utils/consts.js";
import validateServerName from "../../../utils/validateServerName.js";
function editConnection(config, index, logs) {
return __awaiter(this, void 0, void 0, function* () {
const sshConfig = config.servers[index];
let updatedSshConfig;
let confirmChanges = false;
while (confirmChanges === false) {
const name = yield input({
message: `Server name(${sshConfig.name}):`,
validate: (value) => validateServerName(value, config.servers),
});
const host = yield input({ message: `Hostname(${sshConfig.host})` });
const username = yield input({
message: `Username(${sshConfig.username}):`,
});
const port = yield number({
message: `Port(${sshConfig.port}):`,
default: sshConfig.port,
});
const usePassword = yield select({
message: `Keep authentication method? (Current: ${sshConfig.usePassword ? "password" : "key"}):`,
choices: [
{
name: "Keep current method",
value: sshConfig.usePassword,
description: `Keep using ${sshConfig.usePassword ? "password" : "key"}`,
},
{
name: "Change method",
value: !sshConfig.usePassword,
description: `Use ${!sshConfig.usePassword ? "password" : "key"} instead`,
},
],
});
let auth;
let updateAuthValue;
if (usePassword === sshConfig.usePassword) {
updateAuthValue = yield select({
message: `Update ${usePassword ? "password" : "key"}?:`,
choices: [
{
name: "Keep as is",
value: false,
description: `Keep ${usePassword ? "password" : "key"} as is`,
},
{
name: "Update",
value: true,
description: `Change ${usePassword ? "password" : "key"}`,
},
],
});
}
if (usePassword !== sshConfig.usePassword || updateAuthValue) {
auth = usePassword
? yield password({ message: "Password:" })
: yield input({ message: `Key path(${homedir}/.shh/id_rsa):` });
}
else {
auth =
sshConfig.usePassword === true
? sshConfig.password
: sshConfig.privateKey;
}
updatedSshConfig = Object.assign({ id: sshConfig.id, name: name.length ? name : sshConfig.name, host: host.length ? host : sshConfig.host, username: username.length ? username : sshConfig.username, port: port !== null && port !== void 0 ? port : sshConfig.port }, (usePassword
? { usePassword: true, password: auth }
: {
usePassword: false,
privateKey: (auth === null || auth === void 0 ? void 0 : auth.length) ? auth : `${homedir}/.shh/id_rsa`,
}));
confirmChanges = yield select({
message: "Confirm changes",
choices: [
{
name: "Confirm and save",
value: true,
description: "Exit after saving new configuration",
},
{ name: "No. Redo", value: false, description: "Start over edits" },
{
name: "Exit without saving",
value: null,
description: "Exit without changing configuration",
},
],
});
console.clear();
}
if (confirmChanges) {
config.servers[index] = updatedSshConfig;
// save config to file
yield saveFile(`${CONFIG_DIR}/config.json`, config);
if (updatedSshConfig.name !== sshConfig.name) {
const updatedLogs = logs.map((currentLog) => {
if (currentLog.server === sshConfig.name) {
currentLog.server = updatedSshConfig.name;
}
return currentLog;
});
yield saveFile(`${CONFIG_DIR}/logs.json`, updatedLogs);
}
}
return [
"ssh-display",
[JSON.stringify(confirmChanges ? updatedSshConfig : sshConfig), `${index}`],
config,
];
});
}
export default editConnection;
//# sourceMappingURL=edit.js.map