Skip to content

Commit

Permalink
feat: create ssh config with promply
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Sep 16, 2019
1 parent ca2b892 commit afdf4fa
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 19 deletions.
37 changes: 30 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@
"debug": "node --nolazy --inspect-brk=9229 node_modules/jest/bin/jest.js -i"
},
"dependencies": {
"@types/promptly": "^3.0.0",
"@types/shelljs": "^0.8.5",
"chalk": "^2.4.2",
"flexi-path": "^0.1.2-beta.7",
"flexi-path": "^0.1.2-beta.8",
"promptly": "^3.0.3",
"shelljs": "^0.8.3",
"winston": "^3.2.1"
},
Expand Down
3 changes: 3 additions & 0 deletions src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import help from "./help";
import commandParser from "./commandParser";
import moduleLogger from "./logger";
import { Command } from "../types";
import createConfig from "./ssh/createConfig";

const cli = {
run: () => {
Expand All @@ -19,6 +20,8 @@ const cli = {
console.log(header());
}

// console.log(createConfig());

const currentCommand = parser.find();

const showHelp =
Expand Down
33 changes: 33 additions & 0 deletions src/lib/ssh/createConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import promptly from "promptly";

const createConfig = async (): Promise<{ host: string; userName: string }> => {
const result = await promptly.confirm(
"Could not found SSH configuration. Do you want to create it now?"
);

if (result) {
const host = await promptly.prompt(
"Enter router IP address (192.168.1.1): ",
{
default: "192.168.1.1"
}
);
const userName = await promptly.prompt("Enter your user name: ");

return { host, userName };
}

return Promise.reject();
};

const createConfigSync = (): { host: string; userName: string } | undefined => {
let config: { host: string; userName: string } | undefined;

createConfig().then(x => {
config = x;
});

return config;
};

export default createConfigSync;
24 changes: 13 additions & 11 deletions src/lib/ssh/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
#!/usr/bin/env node
import { existsSync, readFileSync } from "fs";
import { join } from "path";

import sh from "shelljs";

const getConfig = () => {
const sshConfigFile = join(__dirname, ".ssh.config.json");
import flexi, { TextTransform } from "flexi-path";

import { SshConfig } from "../../types";

let config = {
const getConfig = (): SshConfig => {
const sshConfigFile = flexi.path({
basePath: __dirname,
path: ".ssh.config.json"
});

const emptyConfig = {
host: "n/a",
username: "n/a",
privateKey: "n/a"
};

if (existsSync(sshConfigFile)) {
config = JSON.parse(readFileSync(sshConfigFile, "utf8"));
}

return config;
return sshConfigFile.exists()
? sshConfigFile.read({ transform: TextTransform.JSON })
: emptyConfig;
};

const execute = (...args: string[]): void => {
Expand Down
5 changes: 5 additions & 0 deletions src/types/SshConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface SshConfig {
host: string;
username: string;
privateKey: string;
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./Command";
export * from "./CommandParser";
export * from "./SshConfig";

0 comments on commit afdf4fa

Please sign in to comment.