Skip to content

Commit e925f53

Browse files
committed
fix: codeflare dump should support -f/--follow
1 parent 3fa32c4 commit e925f53

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

plugins/plugin-codeflare-dashboard/src/controller/dashboard/tailf.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function waitTillExists(filepath: string) {
3434
})
3535
}
3636

37-
async function initTail(filepath: string): Promise<Tail> {
37+
async function initTail(filepath: string, split = true): Promise<Tail> {
3838
await waitTillExists(filepath)
3939

4040
return new Promise<Tail>((resolve, reject) => {
@@ -47,7 +47,7 @@ async function initTail(filepath: string): Promise<Tail> {
4747
tail.start()
4848

4949
resolve({
50-
stream: tail.pipe(split2()),
50+
stream: split ? tail.pipe(split2()) : tail,
5151
quit: tail.quit.bind(tail),
5252
})
5353
})
@@ -60,6 +60,11 @@ export async function pathsFor(kind: Kind, profile: string, jobId: string) {
6060
)
6161
}
6262

63-
export default async function tailf(kind: Kind, profile: string, jobId: string): Promise<Promise<Tail>[]> {
64-
return pathsFor(kind, profile, jobId).then((_) => _.map(initTail))
63+
export default async function tailf(
64+
kind: Kind,
65+
profile: string,
66+
jobId: string,
67+
split = true
68+
): Promise<Promise<Tail>[]> {
69+
return pathsFor(kind, profile, jobId).then((_) => _.map((filepath) => initTail(filepath, split)))
6570
}

plugins/plugin-codeflare-dashboard/src/controller/dump.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,65 @@ import { Arguments } from "@kui-shell/core"
1818

1919
import { pathsFor } from "./dashboard/tailf.js"
2020
import { isValidKind } from "./dashboard/kinds.js"
21-
import { Options, jobIdFrom, usage } from "./dashboard/index.js"
21+
import { Options as DashboardOptions, jobIdFrom, usage as dbUsage } from "./dashboard/index.js"
22+
23+
export type Options = DashboardOptions & {
24+
f: boolean
25+
follow: boolean
26+
}
27+
28+
export const flags = {
29+
boolean: ["follow"],
30+
alias: { follow: ["f"] },
31+
}
32+
33+
function usage() {
34+
return dbUsage("dump") + " [-f/--follow]"
35+
}
2236

2337
export default async function dump(args: Arguments<Options>) {
2438
const kind = args.argvNoOptions[args.argvNoOptions.indexOf("dump") + 1]
2539
const { jobId, profile } = await jobIdFrom(args, "dump")
2640

2741
if (!isValidKind(kind)) {
28-
throw new Error(usage("dump"))
42+
throw new Error(usage())
2943
} else if (!jobId) {
30-
throw new Error(usage("dump"))
44+
throw new Error(usage())
3145
}
3246

33-
const { createReadStream } = await import("fs")
34-
await Promise.all(
35-
(
36-
await pathsFor(kind, profile, jobId)
37-
).map(
38-
(filepath) =>
39-
new Promise((resolve, reject) => {
40-
try {
41-
const rs = createReadStream(filepath)
42-
rs.on("close", resolve)
43-
rs.pipe(process.stdout)
44-
} catch (err) {
45-
reject(err)
46-
}
47-
})
47+
if (!args.parsedOptions.f) {
48+
const { createReadStream } = await import("fs")
49+
await Promise.all(
50+
(
51+
await pathsFor(kind, profile, jobId)
52+
).map(
53+
(filepath) =>
54+
new Promise((resolve, reject) => {
55+
try {
56+
const rs = createReadStream(filepath)
57+
rs.on("close", resolve)
58+
rs.pipe(process.stdout)
59+
} catch (err) {
60+
reject(err)
61+
}
62+
})
63+
)
4864
)
49-
)
65+
} else {
66+
const once = async () => {
67+
const tails = await import("./dashboard/tailf.js").then((_) => _.default(kind, profile, jobId, false))
68+
await Promise.all(tails.map((_) => _.then((_) => _.stream.pipe(process.stdout))))
69+
await Promise.all(tails.map((_) => new Promise((resolve) => _.then((_) => _.stream.on("close", resolve)))))
70+
return true
71+
}
72+
73+
try {
74+
return once()
75+
} catch (err) {
76+
await new Promise((resolve) => setTimeout(resolve, 2000))
77+
once()
78+
}
79+
}
5080

5181
return true
5282
}

plugins/plugin-codeflare-dashboard/src/plugin.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { KResponse, Registrar } from "@kui-shell/core"
1818

1919
import { flags } from "./controller/dashboard/options.js"
2020
import type { Options as DashboardOptions } from "./controller/dashboard/index.js"
21+
import { Options as DumpOptions, flags as dumpFlags } from "./controller/dump.js"
2122

2223
/** Register Kui Commands */
2324
export default function registerCodeflareCommands(registrar: Registrar) {
@@ -29,7 +30,9 @@ export default function registerCodeflareCommands(registrar: Registrar) {
2930
)
3031
)
3132

32-
registrar.listen<KResponse, DashboardOptions>("/codeflare/dump", (args) =>
33-
import("./controller/dump.js").then((_) => _.default(args))
33+
registrar.listen<KResponse, DumpOptions>(
34+
"/codeflare/dump",
35+
(args) => import("./controller/dump.js").then((_) => _.default(args)),
36+
{ flags: dumpFlags }
3437
)
3538
}

0 commit comments

Comments
 (0)