Skip to content

Commit

Permalink
chore: job command completion and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan committed Sep 25, 2019
1 parent eb545ab commit b8c2a17
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 34 deletions.
6 changes: 6 additions & 0 deletions src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const cli = {
parser.isHelp || args.length === empty || currentCommand === emptyCommand;

const invalidCommand =
parser.isHelp === false &&
parser.stripOptions().length > empty &&
(currentCommand === emptyCommand || currentCommand.run === undefined);

Expand All @@ -41,6 +42,11 @@ const cli = {
.check()
.then(ok => {
if (ok) {
console.log(
`Excuting: router ${chalk.bold(currentCommand.fullName)} ${
currentCommand.args
} ...`
);
currentCommand.run(...currentCommand.args);
}
})
Expand Down
32 changes: 14 additions & 18 deletions src/lib/commandParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
import flexi, { FlexiPath, Path, PathType, until } from "flexi-path";
import flexi, { FlexiPath, Path, until } from "flexi-path";

import { Command, CommandParser } from "../types";
import { Command, CommandParser, CommandDeclaration } from "../types";

const rootCommandPath = flexi.path(__dirname).append("commands/");

Expand Down Expand Up @@ -37,7 +37,7 @@ const mostSpecificCommand = (path: Path): [FlexiPath, ...string[]] => {
};

/* eslint-disable import/no-dynamic-require,global-require,@typescript-eslint/no-var-requires */
const requireContent = (path: FlexiPath): any | undefined =>
const requireContent = (path: FlexiPath): CommandDeclaration =>
require(path.path).default;
/* eslint-enable import/no-dynamic-require,global-require,@typescript-eslint/no-var-requires */

Expand All @@ -46,10 +46,10 @@ export const empty: Command = {
description: "",
fullName: "",
helpName: "",
run: undefined,
run: () => {},
subCommands: [],
name: "empty",
usage: ""
hint: ""
};

const createCommand = (currentPath: Path): Command => {
Expand All @@ -72,19 +72,15 @@ const createCommand = (currentPath: Path): Command => {
parent.isEmpty() || parent.isRoot() ? "" : `${parent.name} `;
const fullName = `${parentName}${name}`;

const command: Command = {
args,
description: content.description,
fullName,
helpName: content.helpname,
name,
run: content.run,
subCommands: path
.children()
.filter(x => x !== undefined && !x.isEmpty() && x.name !== "index")
.map(x => createCommand(x)),
usage: content.usage
};
const subCommands = path
.children()
.filter(x => x !== undefined && !x.isEmpty() && x.name !== "index")
.map(x => createCommand(x));

const command = {
...{ args, fullName, name, subCommands },
...content
} as Command;

return command;
};
Expand Down
4 changes: 1 addition & 3 deletions src/lib/commands/jobs/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ const add = async (...args: string[]) => {
command = command || (await promptly.prompt("Command to execute: "));

ssh.execute(`cru a ${id} "${command}"`);

console.log(`Job with id '${id}' was successfully added`);
};

const declaration: CommandDeclaration = {
description: "Creates a new cron job",
run: (...args) => add(...args),
usage: "<uniquieJobId> <command to schedule>"
hint: "<unique id> <'min hour day month week command'>"
};

export default declaration;
6 changes: 2 additions & 4 deletions src/lib/commands/jobs/remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ const remove = async (...args: string[]) => {
let [id] = args;
id = id || (await promptly.prompt("Id of the job to remove: "));

// ssh.execute(`cru a ${id} "${command}"`);

console.log(`Job with id '${id}' was successfully removed`);
ssh.execute(`cru d ${id}`);
};

const declaration: CommandDeclaration = {
description: "Removes a cron job",
run: remove,
usage: "<id>"
hint: "<unique id>"
};

export default declaration;
2 changes: 1 addition & 1 deletion src/lib/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const help = (command?: Command): void => {
});

const usage =
(!isRootHelp && guardCheckedCommand.usage) || "options [parameters]";
(!isRootHelp && guardCheckedCommand.hint) || "options [parameters]";
const lines = [`Usage: router${commandName} ${usage}`];

const commands = isRootHelp
Expand Down
11 changes: 9 additions & 2 deletions src/lib/ssh/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/usr/bin/env node
import chalk from "chalk";
import { ExecResult } from "../../types";

import sh = require("shelljs");
import config = require("./config");

const execute = (...args: string[]): void => {
const execute = (...args: string[]): ExecResult => {
const sshConfig = config.get() || config.empty;

const ssh = `ssh -i '${sshConfig.privateKey}' ${sshConfig.userName}@${sshConfig.host}`;

sh.exec(`${ssh} "${args.join(" ")}"`);
const command = args.join(" ");

const result: ExecResult = sh.exec(`${ssh} "${command}"`);

return result;
};

const executeInTerminal = (args: any) => {
Expand Down
8 changes: 3 additions & 5 deletions src/types/Command.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#!/usr/bin/env node
export default interface Command {
import { CommandDeclaration } from ".";

export default interface Command extends Required<CommandDeclaration> {
args: string[];
description: string;
fullName: string;
helpName: string;
name: string;
run: any;
subCommands: Command[];
usage: string;
}
2 changes: 1 addition & 1 deletion src/types/CommandDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export default interface CommandDeclaration {
description?: string;
helpName?: string;
run?: (...args: string[]) => any;
usage?: string;
hint?: string;
}
5 changes: 5 additions & 0 deletions src/types/ExecResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default interface ExecResult {
code: number;
stdout: string;
stderr: string;
}
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { default as Command } from "./Command";
export { default as CommandDeclaration } from "./CommandDeclaration";
export { default as CommandParser } from "./CommandParser";
export { default as ConfigCreationData } from "./ConfigCreationData";
export { default as ExecResult } from "./ExecResult";
export { default as PromptType } from "./PromptType";
export { default as PromptBody } from "./PromptBody";
export { default as SshConfig } from "./SshConfig";

0 comments on commit b8c2a17

Please sign in to comment.