From 4802ee235cffc9e2f7c8e1ea387128ea96703d39 Mon Sep 17 00:00:00 2001 From: Jos Broers Date: Mon, 12 Dec 2022 17:52:35 +0100 Subject: [PATCH] List available aliases --- lib/messages.ts | 6 ++++-- lib/prepareScript.ts | 2 +- lib/questions.ts | 21 +++++++++++++++------ lib/utils.ts | 2 ++ src/add.ts | 14 +++++++++++--- src/aliases.ts | 14 ++++++++++++++ src/edit.ts | 15 ++++++++++++--- src/get.ts | 3 +-- src/list.ts | 4 +++- src/main.ts | 5 +++++ 10 files changed, 68 insertions(+), 18 deletions(-) create mode 100644 src/aliases.ts diff --git a/lib/messages.ts b/lib/messages.ts index f059f6b..61ed744 100644 --- a/lib/messages.ts +++ b/lib/messages.ts @@ -9,6 +9,8 @@ export const passIp = 'Fill-in an IP address' export const passConnectionName = 'Fill-in a name for the connection' +export const addAliases = "Add the following aliases:" + export function misconfiguredConnection(connection: string) { return `The connection ${connection} isn't configured properly` } @@ -25,8 +27,8 @@ export function createdConfigFile(path: string) { return `Successfully created a configuration file here: ${path}` } -export function theAlias(path: string, type: string, key: string) { - return `alias ssh-${key}="ssh-connect ${type} ${path}"` +export function theAlias(path: string, type: string, key: string, connection?: string) { + return `alias ssh-${key}="ssh-connect ${type} ${path}${connection ? ` ${connection}` : ''}"` } export function removedConnection(connection: string) { diff --git a/lib/prepareScript.ts b/lib/prepareScript.ts index 01e38a0..c2920cf 100644 --- a/lib/prepareScript.ts +++ b/lib/prepareScript.ts @@ -2,7 +2,7 @@ import {resolve} from "path"; import {chooseType, configFilePath, fillConnectionsPath} from "./questions"; import {aliases, renderMessage} from "./utils"; import {writeFileSync} from "fs"; -import {createdConfigFile, theAlias} from "./messages"; +import {addAliases, createdConfigFile, theAlias} from "./messages"; import {homedir} from "os"; const createConfig = async (inquirer: any, type: string, path: string | undefined) => { diff --git a/lib/questions.ts b/lib/questions.ts index 56b0837..1892f61 100644 --- a/lib/questions.ts +++ b/lib/questions.ts @@ -43,30 +43,39 @@ export function fillConnectionName(connectionName?: string) { } } -export function fillIp(connectionName?: string) { +export function fillIp(Ip?: string) { return { type: 'input', name: "ip", message: "What is the IP address?", - default: connectionName ?? null, + default: Ip ?? null, } } -export function fillOptionalUser(connectionName?: string) { +export function fillOptionalUser(user?: string) { return { type: 'string', name: "user", message: "Optional: Who is the user?", - default: connectionName ?? null, + default: user ?? null, } } -export function fillOptionalPort(connectionName?: number) { +export function fillOptionalPort(port?: number) { return { type: 'string', name: "port", message: "Optional: What is the port?", - default: connectionName ?? null, + default: port ?? null, + } +} + +export function fillOptionalAlias(alias?: number) { + return { + type: 'string', + name: "alias", + message: "Optional: What is the alias?", + default: alias ?? null, } } diff --git a/lib/utils.ts b/lib/utils.ts index d1e8034..a95a1a2 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -11,6 +11,7 @@ export const availableTypes = [ 'list', 'edit', 'get', + 'aliases' ].sort() export const aliases = { @@ -20,6 +21,7 @@ export const aliases = { 'ls': 'list', 'edit': 'edit', 'get': 'get', + 'alias': 'aliases' } export function renderMessage(message: string, type?: 'error' | 'info' | 'warning' | 'success', exit?: boolean, suffix?: string) { diff --git a/src/add.ts b/src/add.ts index 761272b..00cfc8b 100644 --- a/src/add.ts +++ b/src/add.ts @@ -1,4 +1,11 @@ -import {connectAfterCreation, fillIp, fillOptionalPort, fillOptionalUser, fillConnectionName} from "../lib/questions"; +import { + connectAfterCreation, + fillIp, + fillOptionalPort, + fillOptionalUser, + fillConnectionName, + fillOptionalAlias +} from "../lib/questions"; import {writeFileSync} from "fs"; import {execSync} from "child_process"; import {passIp, passConnectionName, connectionAlreadyExists} from "../lib/messages"; @@ -7,7 +14,7 @@ import {renderMessage} from "../lib/utils"; export default function add(inquirer: any, connections: object, options: string[], path: string, connectionName: string | undefined) { console.log(connectionName) inquirer - .prompt([fillConnectionName(connectionName), fillIp(), fillOptionalUser(), fillOptionalPort(), connectAfterCreation]) + .prompt([fillConnectionName(connectionName), fillIp(), fillOptionalUser(), fillOptionalPort(), fillOptionalAlias(), connectAfterCreation]) .then((answers) => { if (!answers.connection) throw new Error(passConnectionName) if (!answers.ip) throw new Error(passIp) @@ -17,7 +24,8 @@ export default function add(inquirer: any, connections: object, options: string[ [answers.connection]: { "ip": answers.ip, "user": answers.user, - "port": answers.port + "port": answers.port, + "alias": answers.alias } } diff --git a/src/aliases.ts b/src/aliases.ts new file mode 100644 index 0000000..e7bf274 --- /dev/null +++ b/src/aliases.ts @@ -0,0 +1,14 @@ +import {renderMessage} from "../lib/utils"; +import {addAliases, theAlias} from "../lib/messages"; + +export default async function aliases(connections: object, path: string) { + renderMessage(addAliases, 'info') + + Object.entries(connections).forEach(([key, value]) => { + if (value.alias) { + renderMessage(theAlias(path, 'connect', value.alias.toLowerCase(), key), null) + } + }) + + process.exit(1) +} diff --git a/src/edit.ts b/src/edit.ts index d602cde..2b4b4cb 100644 --- a/src/edit.ts +++ b/src/edit.ts @@ -1,4 +1,11 @@ -import {chooseConnection, fillIp, fillOptionalPort, fillOptionalUser, fillConnectionName} from "../lib/questions"; +import { + chooseConnection, + fillIp, + fillOptionalPort, + fillOptionalUser, + fillConnectionName, + fillOptionalAlias +} from "../lib/questions"; import {missingConnection} from "../lib/messages"; import {renderMessage} from "../lib/utils"; import {writeFileSync} from "fs"; @@ -24,7 +31,8 @@ export default async function remove(inquirer, connections: object, options: str fillConnectionName(connection), fillIp(connections[connectionName].ip), fillOptionalUser(connections[connectionName].user), - fillOptionalPort(connections[connectionName].port) + fillOptionalPort(connections[connectionName].port), + fillOptionalAlias(connections[connectionName].alias) ]) .then((answers) => { delete connections[connection]; @@ -33,7 +41,8 @@ export default async function remove(inquirer, connections: object, options: str [connectionName]: { "ip": answers.ip, "user": answers.user, - "port": answers.port + "port": answers.port, + "alias": answers.alias } } diff --git a/src/get.ts b/src/get.ts index babfd00..ad394fd 100644 --- a/src/get.ts +++ b/src/get.ts @@ -1,6 +1,5 @@ -import {writeFileSync} from "fs"; import {chooseConnection} from "../lib/questions"; -import {getIPAddress, missingConnection, removedConnection} from "../lib/messages"; +import {getIPAddress, missingConnection} from "../lib/messages"; import {renderMessage} from "../lib/utils"; export default async function get(inquirer, connections: object, options: string[], path: string, connectionName: string | undefined) { diff --git a/src/list.ts b/src/list.ts index 784a38f..095ce6b 100644 --- a/src/list.ts +++ b/src/list.ts @@ -1,3 +1,5 @@ +import {renderMessage} from "../lib/utils"; + export default function list(connections: object) { - console.log(connections) + renderMessage(connections, null, true) } diff --git a/src/main.ts b/src/main.ts index d25cead..ed5820e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,6 +11,7 @@ import {importFile, renderMessage} from "../lib/utils"; import list from "./list"; import edit from "./edit"; import get from "./get"; +import aliases from "./aliases"; const main = async () => { inquirer.registerPrompt("search-list", searchList) @@ -56,6 +57,10 @@ main() edit(inquirer, connections, options, args.path, args.connectionName) .catch(({message}) => renderMessage(message, 'error', true)) break; + case 'aliases': + aliases(connections, args.path) + .catch(({message}) => renderMessage(message, 'error', true)) + break; default: throw new Error(passType) }