Skip to content

Commit

Permalink
feat: config command
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Sep 27, 2019
1 parent b7da675 commit 28b66f0
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 28 deletions.
5 changes: 5 additions & 0 deletions src/lib/commands/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { command } from "../../../types/Command";

const description = "Config commands";

export default command({ description });
40 changes: 40 additions & 0 deletions src/lib/commands/config/ssh/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
ConfigCreationData,
empty
} from "../../../../types/ConfigCreationData";
import { command } from "../../../../types/Command";
import { prompt } from "../../../ssh/config";

const description = "Creates new SSH configuration";

const hint = "<hostName> <userName> <keyFile> <passPhrase> <addToAgent>";

const run = (...args: string[]): void => {
// const data: ConfigCreationData = { ...empty, ...args };
const [
host,
userName,
privateKey,
passPhrase,
addKeyToAgent,
createKeyFile
] = args;

const initialValues: ConfigCreationData = {
...empty,
...{
host,
userName,
privateKey,
passPhrase,
addKeyToAgent: addKeyToAgent === "true",
createKeyFile: createKeyFile === "true"
}
};

prompt(initialValues).then(data => {
console.log(data);
});
};

export default command({ description, hint, run });
5 changes: 5 additions & 0 deletions src/lib/commands/config/ssh/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { command } from "../../../../types/Command";

const description = "SSH Config commands";

export default command({ description });
32 changes: 32 additions & 0 deletions src/lib/commands/config/ssh/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import chalk from "chalk";
import flexi from "flexi-path";

import config, { SshConfig } from "../../../ssh/config";
import { command } from "../../../../types/Command";

interface ConfigOptions extends SshConfig {
keyFileExists: boolean;
keyFileAddedToAgent: boolean;
}

const description = "Displays current SSH configuration";

const run = (): void => {
const currentConfig = config.get();

if (currentConfig === null) {
console.log(chalk.red("SSH configuration does not exist"));
return;
}

const keyFileExists = flexi.exists(currentConfig.privateKey);
const keyFileAddedToAgent = false;

const result: ConfigOptions = {
...currentConfig,
...{ keyFileExists, keyFileAddedToAgent }
};
console.log(JSON.stringify(result, null, 2));
};

export default command({ description, run });
2 changes: 2 additions & 0 deletions src/lib/ssh/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ let file = flexi.path({
path: ".ssh.config.json"
});

export { ConfigCreationData, SshConfig } from "../../../types";

export const exists = () => file.exists();

export const get = (): SshConfig | null => {
Expand Down
71 changes: 49 additions & 22 deletions src/lib/ssh/config/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { homedir } from "os";
import flexi from "flexi-path";

import { ConfigCreationData, PromptBody, PromptType } from "../../../types";
import configCreationData, {
ConfigCreationData
} from "../../../types/ConfigCreationData";
import { PromptBody, PromptType } from "../../../types";
import promptTypes from "./promptTypes";

const line = async <T>(
Expand All @@ -19,36 +21,59 @@ const line = async <T>(
});
};

export const defaults: ConfigCreationData = {
host: "192.168.1.1",
userName: "admin",
privateKey: flexi.path(homedir()).append(".ssh/id_rsa").path,
passPhrase: "",
createKeyFile: true,
addKeyToAgent: true
const promptOrDisplayInitialValue = async <T>(
title: string,
initialValue: T | undefined,
fallbackValue: string,
promptType: PromptType = PromptType.Text
): Promise<T> => {
if (initialValue !== undefined) {
console.log(`${title}: ${initialValue}`);
return Promise.resolve(initialValue);
}

return line<T>(title, fallbackValue, promptType);
};

const prompt = async (): Promise<ConfigCreationData> => {
const prompt = async (
fields?: Partial<ConfigCreationData>
): Promise<ConfigCreationData> => {
const yes = "y";
const yesNo = "Y/n";

const host = await line<string>("Router address", defaults.host);
const userName = await line<string>("User name", defaults.userName);
const privateKey = await line<string>(
const initialValues = fields || {}; // || configCreationData.empty();
const { defaults } = configCreationData;

const host = await promptOrDisplayInitialValue(
"Router address",
initialValues.host,
defaults.host
);

const userName = await promptOrDisplayInitialValue(
"User name",
initialValues.userName,
defaults.userName
);

const privateKey = await promptOrDisplayInitialValue(
"SSH private key file",
initialValues.privateKey,
defaults.privateKey
);
const passPhrase = await line<string>(

const passPhrase = await promptOrDisplayInitialValue(
"Passphrase for private key",
initialValues.passPhrase,
defaults.passPhrase,
PromptType.Password
);

const keyFileExists = flexi.exists(privateKey as string);

let createKeyFile = false;
let createKeyFile = initialValues.createKeyFile || false;

if (!keyFileExists) {
if (!keyFileExists && !createKeyFile) {
createKeyFile = await line(
`The key file "${privateKey}" does not exist. Do you want to create it?`,
yes,
Expand All @@ -57,12 +82,14 @@ const prompt = async (): Promise<ConfigCreationData> => {
);
}

const addKeyToAgent = await line<boolean>(
`Do you want to add the key "${privateKey}" to the SSH Agent?`,
yes,
PromptType.Confirm,
yesNo
);
const addKeyToAgent =
initialValues.addKeyToAgent ||
(await line<boolean>(
`Do you want to add the key "${privateKey}" to the SSH Agent?`,
yes,
PromptType.Confirm,
yesNo
));

const result: ConfigCreationData = {
addKeyToAgent,
Expand Down
29 changes: 27 additions & 2 deletions src/types/ConfigCreationData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import { SshConfig } from ".";
import flexi from "flexi-path";
import { homedir } from "os";
import sshConfig, { SshConfig } from "./SshConfig";

export default interface ConfigCreationData extends SshConfig {
export interface ConfigCreationData extends SshConfig {
passPhrase: string;
createKeyFile: boolean;
addKeyToAgent: boolean;
}

export const empty = (): ConfigCreationData => ({
...sshConfig.empty,
...{
passPhrase: "",
createKeyFile: false,
addKeyToAgent: false
}
});

export const defaults: ConfigCreationData = {
host: "192.168.1.1",
userName: "admin",
privateKey: flexi.path(homedir()).append(".ssh/id_rsa").path,
passPhrase: "",
createKeyFile: true,
addKeyToAgent: true
};

export default {
defaults,
empty
};
6 changes: 3 additions & 3 deletions src/types/SshConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export interface SshConfig {
}

export const empty: SshConfig = {
host: "n/a",
privateKey: "n/a",
userName: "n/a"
host: "",
privateKey: "",
userName: ""
};

export const sshConfig = (fields?: Partial<SshConfig>): SshConfig => ({
Expand Down
5 changes: 4 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export {
CommandDeclaration
} from "./CommandDeclaration";
export { default as CommandParser } from "./CommandParser";
export { default as ConfigCreationData } from "./ConfigCreationData";
export {
default as configCreationData,
ConfigCreationData
} from "./ConfigCreationData";
export { default as ExecResult } from "./ExecResult";
export { default as PromptType } from "./PromptType";
export { default as PromptBody } from "./PromptBody";
Expand Down

0 comments on commit 28b66f0

Please sign in to comment.