Skip to content

Commit

Permalink
variatic args
Browse files Browse the repository at this point in the history
  • Loading branch information
sylc committed Feb 12, 2021
1 parent 19e03ab commit 1236ea0
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 54 deletions.
16 changes: 8 additions & 8 deletions publish.ts → .release/publish.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { publish } from "https://x.nest.land/eggs@0.3.4/src/commands/publish.ts";
import { version } from "./version.ts";
import { version } from "../version.ts";

const config = {
name: "dkill",
Expand All @@ -10,13 +10,13 @@ const config = {
unstable: true,
unlisted: false,
files: [
"./mod.ts",
"./src/**/*",
"./README.md",
"./deps.ts",
"./cli.ts",
"./version.ts",
"LICENSE",
"../mod.ts",
"../src/**/*",
"../README.md",
"../deps.ts",
"../cli.ts",
"../version.ts",
"../LICENSE",
],
ignore: [".git", ".vscode"],
checkFormat: "deno fmt",
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,32 @@
### Run directly

```
deno run --unstable --allow-run --allow-net https://x.nest.land/dkill@0.3.0/cli.ts
deno run --unstable --allow-run --allow-net https://x.nest.land/dkill@0.4.0/cli.ts
```

### Install

```
deno install --unstable --allow-run --allow-net https://x.nest.land/dkill@0.3.0/cli.ts
deno install --unstable --allow-run --allow-net https://x.nest.land/dkill@0.4.0/cli.ts
```

You can then access use it using command `dkill`

```
$ dkill --help
Usage: dkill <pid_name_port>
Usage: dkill <targets...>
Version: v0.0.1
Description:
Kill any process by
Kill any processes by
- port: add a semicolon in front to define it as a port. ex: 'dkill :3000'
- pid: a valid integer. ex: 'dkill 12654'
- process name: not implemented yet
You can specify multiple targets at once. ex: 'dkill :5000 :3000 164'
Options:
-h, --help - Show this help.
Expand Down
22 changes: 13 additions & 9 deletions cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ await new Command()
.name("dkill")
.version(version)
.description(
`Kill any process by
`Kill any processes by
- port: add a semicolon in front to define it as a port. ex: 'dkill :3000'
- pid: a valid integer. ex: 'dkill 12654'
- process name: not implemented yet`,
- process name: not implemented yet
You can specify multiple targets at once: 'dkill :5000 :3000 164'`,
)
.arguments("<port_proc_pid>")
.arguments("<targets...:string>")
.option("-v, --verbose", "Increase verbosity")
.option(
"-d, --dryrun",
Expand All @@ -20,27 +22,29 @@ await new Command()
.action(
async (
opts: { verbose: boolean; dryrun: boolean },
port_proc_pid: string,
targets: string[],
) => {
const ports: number[] = [];
const pids: number[] = [];
const procs: string[] = [];

targets.forEach(target => {
// Check if port
if (port_proc_pid.startsWith(":")) {
const port = +port_proc_pid.slice(1);
if (target.startsWith(":")) {
const port = +target.slice(1);
if (!Number.isInteger(port)) {
console.log(`Invalid port number "port"`);
return;
}
ports.push(port);
} else if (Number.isInteger(+port_proc_pid)) {
} else if (Number.isInteger(+target)) {
// check if pid
pids.push(+port_proc_pid);
pids.push(+target);
} else {
// must be a string
procs.push(port_proc_pid);
procs.push(target);
}
})

await dkill({ ports, pids, procs }, {
verbose: opts.verbose,
Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { Command } from "https://x.nest.land/cliffy@0.17.0/command/mod.ts";
export { Command } from "https://x.nest.land/cliffy@0.17.2/command/mod.ts";
// export { Input, prompt } from "https://x.nest.land/cliffy@0.17.0/prompt/mod.ts";
46 changes: 22 additions & 24 deletions src/dkill.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
import { KillPids, port2pid } from "../mod.ts";
import { setVerbose, verbose } from "./utils/verbose.ts";

export async function dkill(
targets: {
pids?: number[];
ports?: number[];
procs?: string[];
},
opts?: { verbose?: boolean; dryrun?: boolean },
opts?: { verbose?: boolean; dryrun?: boolean }
) {
let pidsKilled: number[] = [];

function verbose(text: string) {
opts?.verbose ? console.log(text) : null;
}
setVerbose(opts?.verbose);

const { pids, ports, procs } = targets;

// pids
if (pids && pids.length) {
let killed = pids;
if (!opts?.dryrun) {
killed = await KillPids(pids);
}
pidsKilled = pidsKilled.concat(killed);
}
let allPidsToKill = pids || [];

// ports
// Find Pids for ports
if (ports && ports.length) {
let pidsOfAllPorts: number[] = [];
for (const port of ports) {
const pidsOfPort = await port2pid(port);
verbose(`pids for port ${port}: ${pidsOfPort}`);
pidsOfAllPorts = pidsOfAllPorts.concat(pidsOfPort);
}
let killed = pidsOfAllPorts;
if (!opts?.dryrun) {
killed = await KillPids(pidsOfAllPorts);
allPidsToKill = allPidsToKill.concat(pidsOfPort);
}
pidsKilled = pidsKilled.concat(killed);
}

// processes
Expand All @@ -46,10 +30,24 @@ export async function dkill(
return;
}

if (pidsKilled.length) {
// remove duplicates
allPidsToKill = [ ...new Set(allPidsToKill)]

// find names


// Kill them all, or just pretend
let killed = [];
if (!opts?.dryrun) {
killed = await KillPids(allPidsToKill);
} else {
killed = allPidsToKill
}

if (killed.length) {
console.log(
`${opts?.dryrun ? "list of pids target (not killed):" : "pids killed:"}`,
pidsKilled.join(" "),
killed.join(" ")
);
} else {
console.log("No process found");
Expand Down
14 changes: 11 additions & 3 deletions src/killPid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { setVerbose, verbose } from "./utils/verbose.ts";

async function winKill(pid: number) {
const cmdArr = ["cmd", "/c", `taskkill /PID ${pid} /F`]
verbose(cmdArr.join(' '))
const cmd = Deno.run({
cmd: ["cmd", "/c", `taskkill /PID ${pid} /F`],
cmd: cmdArr,
stdout: "piped",
stderr: "piped",
});
Expand All @@ -11,8 +15,10 @@ async function winKill(pid: number) {
}

async function linuxKill(pid: number) {
const cmdArr = ["kill", "-9", `${pid}`]
verbose(cmdArr.join(' '))
const cmd = Deno.run({
cmd: ["kill", "-9", `${pid}`],
cmd: cmdArr,
stdout: "piped",
stderr: "piped",
});
Expand All @@ -22,9 +28,11 @@ async function linuxKill(pid: number) {
cmd.close();
}

export async function KillPids(pids: number[]) {
export async function KillPids(pids: number[], opts?: { verbose?: boolean }) {
setVerbose(opts?.verbose)
const os = Deno.build.os;
const pidKilled: number[] = [];

for (const pid of pids) {
try {
if (os === "windows") {
Expand Down
18 changes: 14 additions & 4 deletions src/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@
*/
import { serve } from "https://deno.land/std@0.86.0/http/server.ts";

const server = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP webserver running on port :8080`, Deno.build.os);
const server1 = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP webserver1 running on port :8080`, Deno.build.os);

for await (const request of server) {
const server2 = serve({ hostname: "0.0.0.0", port: 3000 });
console.log(`HTTP webserver1 running on port :3000`, Deno.build.os);

for await (const request of server1) {
let bodyContent = "Your user-agent is:\n\n";
bodyContent += request.headers.get("user-agent") || "Unknown";

request.respond({ status: 200, body: bodyContent });
}

for await (const request of server2) {
let bodyContent = "Your user-agent is:\n\n";
bodyContent += request.headers.get("user-agent") || "Unknown";

request.respond({ status: 200, body: bodyContent });
}

// deno -A ./src/tests/utils
// deno run -A ./src/tests/utils.ts
9 changes: 9 additions & 0 deletions src/utils/verbose.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let isVerbose = false

export function setVerbose(value: boolean | undefined) {
isVerbose = !!value
}

export function verbose(text: string) {
isVerbose ? console.log(text) : null;
}
2 changes: 1 addition & 1 deletion version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = "0.3.0";
export const version = "0.4.0";

0 comments on commit 1236ea0

Please sign in to comment.