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

Commit

Permalink
Adding the new alias command
Browse files Browse the repository at this point in the history
  • Loading branch information
josbroers committed Dec 8, 2022
1 parent 3f1374f commit bd42fc5
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 55 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ Note: the arguments are optional

## 1. Node.js

First install the Node.js higher or equal to 14. Use the JavaScript Tool Manager [Volta](https://volta.sh/) or
First install the Node.js higher or equal to 16. Use the JavaScript Tool Manager [Volta](https://volta.sh/) or
the [Node Version Manager](https://github.com/nvm-sh/nvm).

## 2. Configure

Before you can use this package as an executable you need to create a JSON file. Run the following command to create this file:
Before you can use this package as an executable you need to create a JSON file. Run the following command to create
this file:

```bash
ssh-connect configure <path_for_file>
Expand Down Expand Up @@ -64,6 +65,12 @@ ssh-connect remove <path_to_file> <connection_name>
ssh-connect edit <path_to_file> <connection_name>
```

#### To get the IP-address of a listed connection, use:

```bash
ssh-connect get <path_to_file> <connection_name>
```

### 3.2 Aliases

You can use aliases to predefine the type, connection:
Expand All @@ -74,6 +81,7 @@ alias ssh-add="ssh-connect add $HOME/connections.json"
alias ssh-rm="ssh-connect remove $HOME/connections.json"
alias ssh-ls="ssh-connect list $HOME/connections.json"
alias ssh-edit="ssh-connect edit $HOME/connections.json"
alias ssh-get="ssh-connect get $HOME/connections.json"
```

You can take it even further by making aliases for specific connection names:
Expand Down
29 changes: 23 additions & 6 deletions lib/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {availableTypes} from "./utils";
import chalk from "chalk";

export const configureConnection = 'No connections defined. Please configure a connection...'

Expand All @@ -8,14 +9,30 @@ export const passIp = 'Fill-in an IP address'

export const passConnectionName = 'Fill-in a name for the connection'

export const misconfiguredConnection = (connection: string) => `The connection ${connection} isn't configured properly`
export function misconfiguredConnection(connection: string) {
return `The connection ${connection} isn't configured properly`
}

export const missingConnection = (connection: string) => `The connection ${connection} doesn't exists`
export function missingConnection(connection: string) {
return `The connection ${connection} doesn't exists`
}

export const connectionAlreadyExists = (connection: string) => `A connection with the name ${connection} already exists`
export function connectionAlreadyExists(connection: string) {
return `A connection with the name ${connection} already exists`
}

export const createdConfigFile = (path: string) => `Successfully created a configuration file here: ${path}`
export function createdConfigFile(path: string) {
return `Successfully created a configuration file here: ${path}`
}

export const addAliases = "Add the following aliases:"
export function theAlias(path: string, type: string, key: string) {
return `alias ssh-${key}="ssh-connect ${type} ${path}"`
}

export const theAlias = (path: string, type: string, key: string) => `alias ssh-${key}="ssh-connect ${type} ${path}"`
export function removedConnection(connection: string) {
return `Removed the following connection:\n\n${connection}`
}

export function getIPAddress(ip: string) {
return chalk.bold(ip)
}
6 changes: 2 additions & 4 deletions 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 {addAliases, createdConfigFile, theAlias} from "./messages";
import {createdConfigFile, theAlias} from "./messages";
import {homedir} from "os";

const createConfig = async (inquirer: any, type: string, path: string | undefined) => {
Expand All @@ -26,7 +26,7 @@ const createConfig = async (inquirer: any, type: string, path: string | undefine
process.exit(1)
}

const prepareScript = async (inquirer: any) => {
export default async function prepareScript(inquirer: any) {
let type = process.argv.slice(2)[0] ?? undefined
if (!type) {
await inquirer
Expand All @@ -53,5 +53,3 @@ const prepareScript = async (inquirer: any) => {
connectionName: process.argv.slice(2)[2] ?? undefined
}
}

export default prepareScript
46 changes: 23 additions & 23 deletions lib/questions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import {availableTypes} from "./utils";
import {homedir} from "os";

export const chooseConnection = (connections: string[]) => {
return {
type: 'search-list',
name: "connection",
message: "Choose a connection:",
choices: connections,
loop: false,
pageSize: 20
}
}

export const chooseType = {
type: 'list',
name: 'type',
Expand All @@ -27,7 +16,25 @@ export const fillConnectionsPath = {
default: `${homedir()}/connections.json`
}

export const fillConnectionName = (connectionName?: string) => {
export const connectAfterCreation = {
type: 'confirm',
name: "connect",
message: "Connect to the connection after creation?",
default: true,
}

export function chooseConnection(connections: string[]) {
return {
type: 'search-list',
name: "connection",
message: "Choose a connection:",
choices: connections,
loop: false,
pageSize: 20
}
}

export function fillConnectionName(connectionName?: string) {
return {
type: 'input',
name: "connection",
Expand All @@ -36,7 +43,7 @@ export const fillConnectionName = (connectionName?: string) => {
}
}

export const fillIp = (connectionName?: string) => {
export function fillIp(connectionName?: string) {
return {
type: 'input',
name: "ip",
Expand All @@ -45,7 +52,7 @@ export const fillIp = (connectionName?: string) => {
}
}

export const fillOptionalUser = (connectionName?: string) => {
export function fillOptionalUser(connectionName?: string) {
return {
type: 'string',
name: "user",
Expand All @@ -54,7 +61,7 @@ export const fillOptionalUser = (connectionName?: string) => {
}
}

export const fillOptionalPort = (connectionName?: number) => {
export function fillOptionalPort(connectionName?: number) {
return {
type: 'string',
name: "port",
Expand All @@ -63,14 +70,7 @@ export const fillOptionalPort = (connectionName?: number) => {
}
}

export const connectAfterCreation = {
type: 'confirm',
name: "connect",
message: "Connect to the connection after creation?",
default: true,
}

export const configFilePath = (defaultPath: string) => {
export function configFilePath(defaultPath: string) {
return {
type: 'input',
name: "configFilePath",
Expand Down
9 changes: 6 additions & 3 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {readFileSync} from "fs";
import chalk from "chalk";
import {resolve} from "path";
import {homedir} from "os";

export const availableTypes = [
'connect',
'add',
'remove',
'configure',
'list',
'edit'
'edit',
'get',
].sort()

export const aliases = {
Expand All @@ -17,9 +19,10 @@ export const aliases = {
'rm': 'remove',
'ls': 'list',
'edit': 'edit',
'get': 'get',
}

export const renderMessage = (message: string, type?: 'error' | 'info' | 'warning' | 'success', exit?: boolean, suffix?: string) => {
export function renderMessage(message: string, type?: 'error' | 'info' | 'warning' | 'success', exit?: boolean, suffix?: string) {
switch (type) {
case "success":
console.log(`%s: ${message}`, chalk.green.bold(suffix ?? type.toUpperCase()))
Expand All @@ -42,6 +45,6 @@ export const renderMessage = (message: string, type?: 'error' | 'info' | 'warnin
}
}

export const importFile = async (path: string) => {
export async function importFile(path: string) {
return await JSON.parse(readFileSync(resolve(process.cwd(), path), 'utf8'))
}
4 changes: 1 addition & 3 deletions src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {execSync} from "child_process";
import {passIp, passConnectionName, connectionAlreadyExists} from "../lib/messages";
import {renderMessage} from "../lib/utils";

const add = (inquirer: any, connections: object, options: string[], path: string, connectionName: string | undefined) => {
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])
Expand Down Expand Up @@ -41,5 +41,3 @@ const add = (inquirer: any, connections: object, options: string[], path: string
})
.catch(({message}) => renderMessage(message, 'error', true))
}

export default add
4 changes: 1 addition & 3 deletions src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {chooseConnection} from "../lib/questions";
import {misconfiguredConnection} from "../lib/messages";
import {renderMessage} from "../lib/utils";

const connect = async (inquirer: any, connections: object, options: string[], connectionName: string | undefined) => {
export default async function connect(inquirer: any, connections: object, options: string[], connectionName: string | undefined) {
if (!connectionName) {
await inquirer
.prompt([chooseConnection(options)])
Expand All @@ -25,5 +25,3 @@ const connect = async (inquirer: any, connections: object, options: string[], co
execSync(`ssh ${user}${ip} ${port}`, {stdio: 'inherit'})
process.exit(1)
}

export default connect
4 changes: 1 addition & 3 deletions src/edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {missingConnection} from "../lib/messages";
import {renderMessage} from "../lib/utils";
import {writeFileSync} from "fs";

const remove = async (inquirer, connections: object, options: string[], path: string, connectionName: string | undefined) => {
export default async function remove(inquirer, connections: object, options: string[], path: string, connectionName: string | undefined) {
if (!connectionName) {
await inquirer
.prompt([chooseConnection(options)])
Expand Down Expand Up @@ -47,5 +47,3 @@ const remove = async (inquirer, connections: object, options: string[], path: st
})
.catch(({message}) => renderMessage(message, 'error', true))
}

export default remove
21 changes: 21 additions & 0 deletions src/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {writeFileSync} from "fs";
import {chooseConnection} from "../lib/questions";
import {getIPAddress, missingConnection, removedConnection} from "../lib/messages";
import {renderMessage} from "../lib/utils";

export default async function get(inquirer, connections: object, options: string[], path: string, connectionName: string | undefined) {
if (!connectionName) {
await inquirer
.prompt([chooseConnection(options)])
.then(({connection}) => {
connectionName = connection
})
.catch(({message}) => renderMessage(message, 'error', true))
}

if (!connections[connectionName]) {
throw new Error(missingConnection(connectionName))
}

renderMessage(getIPAddress(connections[connectionName].ip), null, true)
}
4 changes: 1 addition & 3 deletions src/list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const list = (connections: object) => {
export default function list(connections: object) {
console.log(connections)
}

export default list
5 changes: 5 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {configureConnection, passType} from "../lib/messages";
import {importFile, renderMessage} from "../lib/utils";
import list from "./list";
import edit from "./edit";
import get from "./get";

const main = async () => {
inquirer.registerPrompt("search-list", searchList)
Expand Down Expand Up @@ -44,6 +45,10 @@ main()
remove(inquirer, connections, options, args.path, args.connectionName)
.catch(({message}) => renderMessage(message, 'error', true))
break;
case 'get':
get(inquirer, connections, options, args.path, args.connectionName)
.catch(({message}) => renderMessage(message, 'error', true))
break;
case 'list':
list(connections)
break;
Expand Down
8 changes: 3 additions & 5 deletions src/remove.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {writeFileSync} from "fs";
import {chooseConnection} from "../lib/questions";
import {missingConnection} from "../lib/messages";
import {missingConnection, removedConnection} from "../lib/messages";
import {renderMessage} from "../lib/utils";

const remove = async (inquirer, connections: object, options: string[], path: string, connectionName: string | undefined) => {
export default async function remove(inquirer, connections: object, options: string[], path: string, connectionName: string | undefined) {
if (!connectionName) {
await inquirer
.prompt([chooseConnection(options)])
Expand All @@ -20,7 +20,5 @@ const remove = async (inquirer, connections: object, options: string[], path: st
const oldConnection = connections[connectionName]
delete connections[connectionName];
writeFileSync(path, JSON.stringify(connections, null, 2));
renderMessage(`Removed the following connection:\n\n${JSON.stringify(oldConnection, null, 2)}`, 'success', true)
renderMessage(removedConnection(JSON.stringify(oldConnection, null, 2)), 'success', true)
}

export default remove

0 comments on commit bd42fc5

Please sign in to comment.