Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
List available aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
josbroers committed Dec 12, 2022
1 parent a8e14e3 commit 4802ee2
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/prepareScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
21 changes: 15 additions & 6 deletions lib/questions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const availableTypes = [
'list',
'edit',
'get',
'aliases'
].sort()

export const aliases = {
Expand All @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions src/add.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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)
Expand All @@ -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
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/aliases.ts
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 12 additions & 3 deletions src/edit.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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];
Expand All @@ -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
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/get.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/list.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {renderMessage} from "../lib/utils";

export default function list(connections: object) {
console.log(connections)
renderMessage(connections, null, true)
}
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 4802ee2

Please sign in to comment.